2

我正在使用 OleDbDataAdapter 读取数据集中 Excel 工作表的内容。Excel 表由 20391 行组成,数据集在我的本地计算机上运行时读取总行数,但在 IIS7.5 服务器上运行代码时,它只读取 FIRST 12463!!

我的连接字符串:

switch (strFileType.Trim())
       {
           case ".xls":
               connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath +
                            ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
               break;
           case ".xlsx":
               connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath +
                            ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
               break;
       }

var query = "SELECT * FROM [" + excelSheets[0] + "]";

//Create the connection object
var conn = new OleDbConnection(connString);
//Open connection
if (conn.State == ConnectionState.Closed) conn.Open();
//Create the command object
var cmd = new OleDbCommand(query, conn);
var da = new OleDbDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds);

有没有办法将 da.Fill 结果划分为多个数据表?

4

1 回答 1

1

我做了@dash 的建议并使用了Excel 数据阅读器,它工作正常。

这是代码

        FileStream stream = File.Open(strNewPath , FileMode.Open, FileAccess.Read);            
        //1. Reading from a binary Excel file ('97-2003 format; *.xls)
        //IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
        //...
        //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);;                      
        excelReader.IsFirstRowAsColumnNames = true;
        DataSet result = excelReader.AsDataSet();
于 2013-04-02T11:56:28.590 回答