简体   繁体   中英

C#: XLS library needed after switching from 32BIT to 64BIT

Currently i wanted to switch from 32 bit builds to 64 bit, but i have trouble with finiding any library to extract data from xls. I can work with others like (xlsx, xlsm) on ClosedXML, unfortunately xls is not supported.

so right now this connexction will not stand.

    string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=NO,IMEX=1'";
    string excelQuery = "SELECT * FROM [monthly$A:G]";

Installing 64 bit drivers for OLE is out of a question. So maybe someone has stumble upon it, or have any ideas.

I'd like to build data into datatable with headers similar to HDR=YES and possibility to switch it off like in excample above.

Additionaly, due to excel formats, dapper is out of the question:(

i Tried to find some questions but all I could find would be to install OLEDB Driver for 64 bits.

You can check out my library Sylvan.Data.Excel . It has no external dependencies, is cross plat, open-source, MIT licensed, and also the fastest Excel data reader for .NET . It supports both.xls and.xlsx. It does not support.xlsb, however. It is not as fully featured as some other libraries, it only supports reading (not writing), but it should be a pretty direct replacement for ACE, because it implements DbDataReader . If you run into any issues/questions feel free to open them in the GitHub repo.

Using it is pretty straightforward:


var edr = ExcelDataReader.Create("data.xls");

while(edr.Read()) {
  for(int i = 0; i < edr.FieldCount; i++) {
    var value = edr.GetString(i);
  }
}

By default, it will expect the first row of each sheet to contain headers. This can be disabled however. When disabled, columns can only be accessed by ordinal.

var opts = new ExcelDataReaderOptions { Schema = ExcelSchema.NoHeaders };
var edr = ExcelDataReader.Create("data.xls", opts);

Since it implements DbDataReader , loading it into a data table is a one-liner:

var dt = new DataTable();
dt.Load(edr);

First, don't use XLS to begin with. That format was replaced by xlsx in 2007 - that's 15 years ago. All Excel versions use xlsx and so do all applications. There are no compatibility concerns - in fact, it's the ancient xls that causes compatibility problems. Google Sheets for example doesn't support xls files unless you pay. The only reason xls persists is inertia.

If you insist on using xls (why?) you can use ExcelDataReaader . That library can read both xls and xlsx files and return either an DbDataReader or a DataSet . The NuGet package has 17M downloads making it the 2nd most popular Excel library after EPPlus.

The example in the repository's pages shows how to use it in the simplest case - ExcelReaderFactory.CreateReader(stream) returns a DbDataReader :

using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
    // Auto-detect format, supports:
    //  - Binary Excel files (2.0-2003 format; *.xls)
    //  - OpenXml Excel files (2007 format; *.xlsx, *.xlsb)
    using (var reader = ExcelReaderFactory.CreateReader(stream))
    {
        // Choose one of either 1 or 2:

        // 1. Use the reader methods
        do
        {
            while (reader.Read())
            {
                // reader.GetDouble(0);
            }
        } while (reader.NextResult());

        // 2. Use the AsDataSet extension method
        var result = reader.AsDataSet();

        // The result of each spreadsheet is in result.Tables
    }
}

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