从OleDbDataAdapter填充DataSet

时间:2013-03-31 22:13:57

标签: c# asp.net oledbdataadapter

我正在使用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结果分成多个数据表?

1 个答案:

答案 0 :(得分:1)

我做了@dash建议并使用了Excel Data Reader并且它正常工作。

这是代码

        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();