简体   繁体   English

从c#中读取优秀,有什么新东西吗?

[英]reading excel from c#, anything new?

My question is very simillar to the one asked here: Reading Excel files from C# 我的问题与这里提到的问题非常相似: 从C#读取Excel文件

My question is whether there is a new better way of doing this now (2 years later), if not does anybody have some examples of using the ACE OLEDB 12.0 witch seem to be the best way to go with it? 我的问题是现在是否有一种新的更好的方法(2年后),如果没有人有一些使用ACE OLEDB 12.0的例子似乎是最好的方式吗?

What I need to do is an application that reads an xlsx file every night, wrap the data in the excel sheet, and then save it to the database, any other tips? 我需要做的是一个应用程序,每晚读取一个xlsx文件,将数据包装在excel表中,然后将其保存到数据库,任何其他提示?

Thanks 谢谢

Using ExcelPackage you can iterate all sheets and columns like this: 使用ExcelPackage,您可以迭代所有工作表和列,如下所示:

public class ExcelRead
{
    public void ReadExcel()
    {
        FileInfo existingFile = new FileInfo(@"C:\temp\book1.xlsx");
        using (ExcelPackage xlPackage = new ExcelPackage(existingFile))
        {
            foreach (ExcelWorksheet worksheet in xlPackage.Workbook.Worksheets)
            {
                var dimension = worksheet.Dimension();
                for (int row = dimension.StartRow; row <= dimension.EndRow; row++)
                {
                    for (int col = dimension.StartColumn; col <= dimension.EndColumn; col++)
                    {
                        Console.WriteLine(row + ":" + col + " - " + worksheet.Cell(row, col));
                    }
                }
            }
        }
    }
}

public class Dimension
{
    public int StartRow { get; set; }
    public int StartColumn { get; set; }
    public int EndRow { get; set; }
    public int EndColumn { get; set; }
}

public static class ExcelHelper
{
    private static readonly char[] _numbers = new[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    public static Dimension Dimension(this ExcelWorksheet worksheet)
    {
        string range =
            worksheet.WorksheetXml.SelectSingleNode("//*[local-name()='dimension']").Attributes["ref"].Value;
        string[] rangeCoordinates = range.Split(':');

        int idx = rangeCoordinates[0].IndexOfAny(_numbers);
        int startRow = int.Parse(rangeCoordinates[0].Substring(idx));
        int startCol = ConvertFromExcelColumnName(rangeCoordinates[0].Substring(0, idx));

        if (rangeCoordinates.Length == 1)
            return new Dimension
                       {StartRow = startRow, StartColumn = startCol, EndRow = startRow, EndColumn = startCol};

        idx = rangeCoordinates[1].IndexOfAny(_numbers);
        int endRow = int.Parse(rangeCoordinates[1].Substring(idx));
        int endCol = ConvertFromExcelColumnName(rangeCoordinates[1].Substring(0, idx));
        return new Dimension {StartRow = startRow, StartColumn = startCol, EndRow = endRow, EndColumn = endCol};
    }

    public static int ConvertFromExcelColumnName(string name)
    {
        name = name.ToUpper();
        int result = 0;
        for (int i = 0; i < name.Length - 1; i++)
        {
            int val = name[i] - 64;
            int columnVal = (int) Math.Pow(26, name.Length - i - 1);
            result += val*columnVal;
        }
        result += name[name.Length - 1] - 64;
        return result;
    }
}

I don't think it was listed in the other thread. 我不认为它是在其他线程中列出的。 I work for Infragistics and we have a library for reading and writing excel files. 我为Infragistics工作,我们有一个用于读取和编写excel文件的库。 You can download a trial here 您可以在此处下载试用版

我已经好好利用EPPlus来写出excel文件了,我认为它也可以很好地阅读它们。

Apache POI is a free Java library for accessing MS Office documents without having Excel installed. Apache POI是一个免费的Java库,用于在不安装Excel的情况下访问MS Office文档。 According to this post , it is possible to use IKVM.NET to make POI available for .NET programs. 根据这篇文章 ,可以使用IKVM.NET使POI可用于.NET程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM