简体   繁体   中英

reading excel from c#, anything new?

My question is very simillar to the one asked here: Reading Excel files from C#

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?

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?

Thanks

Using ExcelPackage you can iterate all sheets and columns like this:

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. You can download a trial here

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

Apache POI is a free Java library for accessing MS Office documents without having Excel installed. According to this post , it is possible to use IKVM.NET to make POI available for .NET programs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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