如何导入数据从Excel中的数据集在asp.net页面中的c#?

我有一个Excel表格,其中包含超过20,000条logging,我想要以一种非常有效和快速的方式将这些数据存入一个数据集或一个数据表中,以便处理我的数据。 请做任何人知道如何使用C#代码…

public static DataSet exceldata(string filelocation) { DataSet ds = new DataSet(); OleDbCommand excelCommand = new OleDbCommand();OleDbDataAdapter excelDataAdapter = new OleDbDataAdapter(); string excelConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filelocation + ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""; OleDbConnection excelConn = new OleDbConnection(excelConnStr); excelConn.Open(); DataTable dtPatterns = new DataTable();excelCommand = new OleDbCommand("SELECT `PATTERN` as PATTERN, `PLAN` as PLAN FROM [PATTERNS$]", excelConn); excelDataAdapter.SelectCommand = excelCommand; excelDataAdapter.Fill(dtPatterns); "dtPatterns.TableName = Patterns"; ds.Tables.Add(dtPatterns); return ds; } 

我自己find了答案。 有一个DLL名为Excel.dll [2.0.1.0] ExcelDataReader 。 我没有与我的联系,因为我从我的朋友之一。 我想你可以从网上search并下载exceldatareader.dll

 IExcelDataReader iExcelDataReader = null; FileInfo fileInfo = new FileInfo(FpdUnConLoanUpload.PostedFile.FileName); string file = fileInfo.Name; if (file.Split('.')[1].Equals("xls")) { iExcelDataReader = ExcelReaderFactory.CreateBinaryReader(oStream); } else if (file.Split('.')[1].Equals("xlsx")) { iExcelDataReader = ExcelReaderFactory.CreateOpenXmlReader(oStream); } iExcelDataReader.IsFirstRowAsColumnNames = true; DataSet dsUnUpdated = new DataSet(); dsUnUpdated = iExcelDataReader.AsDataSet(); iExcelDataReader.Close(); if (dsUnUpdated != null) { } else { lblCmnMesage.Text = "No Data Found In The Excel Sheet!"; } 

我想你对20000这个数字感到惊讶。这不算什么。 您可以使用http://openxmldeveloper.org/来高效地导入和处理,而不依赖interop。 只有约束是你需要有xlsm格式的Excel(Excelmacros)