C#2.0parsingExcel电子表格的最快方法

可能重复:
从C#读取Excel文件

什么是从Csharp的Excel中读取大量数据的最快方法。 示例代码会很好。 。

在我们的桌面环境中,我已经通过使用COM通过Excel达到了性能,灵活性和稳定性之间的最佳组合。

访问Excel总是通过相同的线程。

我使用后期绑定(在VB.Net)使我的应用程序版本独立。

其余的应用程序是用C#开发的,只有这个部分和其他一些小部分在VB中,因为它们在VB.Net中更容易。

Dim workBook As Object = GetObject(fileName) Dim workSheet As Object = workBook.WorkSheets.Item(WorkSheetNr) Dim range As Object = workSheet.Cells.Item(1, 1) Dim range2 As Object = range.CurrentRegion Dim rrow As Integer = range2.Row ' For XL97, first convert to integer. XL97 will generate an error ' Dim rcolumn As Integer = range2.Column Dim top As Object = workSheet.Cells.Item(rrow, rcolumn) Dim bottom As Object = top.Offset(range2.Rows.Count - 1, range2.Columns.Count - 1) range = workSheet.Range(top, bottom) Dim values As Object(,) values = range.Value 

这里有一个包含来自Excel的值的2维数组。 最后一条语句从Excel获取数据到.Net。

由于Excel工作表的大小限制,这些不能变得非常大,所以内存不应该是一个问题。

我们已经在多个系统上对性能进行了一些testing。 它被优化以创build尽可能less的(进程外)COM调用。

这种方式给了我们最好的性能,特别是因为数据直接在一个数组中,访问这些数据的速度比数据集快。

慢解决scheme是启动Excel。 但是,如果您需要处理多个文件,则只需一次,即可启动Excel。

另外我不会在服务器环境中使用这个解决scheme。

 public class ExcelHeaderValues { public static string CUSIP = "CUSIP"; public static string ORIG = "ORIG"; public static string PRICE = "PRICE"; public static int COLUMNCOUNT = 3; } public class ExcelParser { private ArrayList collOutput = null; string sSheetName = String.Empty; string[] strValidColumn; int validRowCnt = 0; string sColumnPositions = String.Empty; OleDbCommand ExcelCommand; OleDbDataAdapter ExcelAdapter; OleDbConnection ExcelConnection = null; DataSet dsSheet = null; string path = string.Empty; string identifier = string.Empty; public ExcelParser() { collOutput = new ArrayList(); } public void Extract() { bool headermatch = false; string strCusip = string.Empty, strOrig = string.Empty, strPrice = string.Empty, strData = string.Empty; string strCusipPos = string.Empty, strPricePos = string.Empty, strOrigPos = string.Empty; string strColumnHeader = String.Empty; int reqColcount = 0; string[] strTemp; bool bTemp = false; bool validRow = false; DataTable schemaTable = GetSchemaTable(); validRowCnt = 0; foreach (DataRow dr in schemaTable.Rows) { if (dsSheet != null) { dsSheet.Reset(); dsSheet = null; } strCusipPos = string.Empty; strOrigPos = string.Empty; strPricePos = string.Empty; if (isValidSheet(dr)) { sColumnPositions = string.Empty; validRowCnt = 0; foreach (DataRow dataRow in dsSheet.Tables[0].Rows) { sColumnPositions = string.Empty; if (headermatch == false) { sColumnPositions = string.Empty; foreach (DataColumn column in dsSheet.Tables[0].Columns) { strColumnHeader = dataRow[column.ColumnName].ToString().ToUpper().Trim(); strColumnHeader = strColumnHeader.ToUpper(); if (strColumnHeader == ExcelHeaderValues.ORIG.ToUpper() || strColumnHeader == ExcelHeaderValues.CUSIP.ToUpper() || strColumnHeader == ExcelHeaderValues.PRICE.ToUpper()) { bTemp = true; validRow = true; reqColcount = ExcelHeaderValues.COLUMNCOUNT; } if (bTemp) { bTemp = false; sColumnPositions += column.ColumnName + "^" + strColumnHeader + ";"; } } strValidColumn = sColumnPositions.Trim().Split(';'); if (validRow == true && reqColcount == strValidColumn.Length - 1) { headermatch = true; break; } validRowCnt++; } } if (headermatch == true) { try { if (dsSheet.Tables[0].Rows.Count > 0) { if (strValidColumn.Length > 0) { for (int i = 0; i < strValidColumn.Length - 1; i++) { if (strValidColumn[i].ToUpper().Contains("CUSIP")) { strTemp = strValidColumn[i].ToString().Split('^'); strCusipPos = strTemp[0].ToString(); strTemp = null; } if (strValidColumn[i].ToUpper().Contains("PRICE")) { strTemp = strValidColumn[i].ToString().Split('^'); strPricePos = strTemp[0].ToString(); strTemp = null; } if (strValidColumn[i].ToUpper().Contains("ORIG")) { strTemp = strValidColumn[i].ToString().Split('^'); strOrigPos = strTemp[0].ToString(); strTemp = null; } } } for (int iData = validRowCnt; iData < dsSheet.Tables[0].Rows.Count; iData++) { if (strCusipPos.Trim() != "") strCusip = dsSheet.Tables[0].Rows[iData][strCusipPos].ToString().Trim(); if (strOrigPos.Trim() != "") strOrig = dsSheet.Tables[0].Rows[iData][strOrigPos].ToString().Trim(); if (strPricePos.Trim() != "") strPrice = dsSheet.Tables[0].Rows[iData][strPricePos].ToString().Trim().ToUpper(); strData = ""; if (strCusip.Length == 9 && strCusip != "" && strPrice != "" && strOrig != "" && !strPrice.ToUpper().Contains("SOLD")) strData = strCusip + "|" + Convert.ToDouble(strOrig) * 1000000 + "|" + strPrice + "

||"; if (strData != null && strData != "") collOutput.Add(strData); strCusip = string.Empty; strOrig = string.Empty; strPrice = string.Empty; strData = string.Empty; } } } catch (Exception ex) { throw ex; } } headermatch = false; sColumnPositions = string.Empty; strColumnHeader = string.Empty; } } } private bool isValidSheet(DataRow dr) { bool isValidSheet = false; sSheetName = dr[2].ToString().ToUpper(); if (!(sSheetName.Contains("$_")) && !(sSheetName.Contains("$'_")) && (!sSheetName.Contains("Print_Titles".ToUpper())) && (dr[3].ToString() == "TABLE" && ((!sSheetName.Contains("Print_Area".ToUpper())))) && !(sSheetName.ToUpper() == "DLOFFERLOOKUP")) { if (sSheetName.Trim().ToUpper() != "Disclaimer$".ToUpper() && sSheetName.Trim().ToUpper() != "Summary$".ToUpper() && sSheetName.Trim().ToUpper() != "FORMULAS$" ) { string sQry = string.Empty; sQry = "SELECT * FROM [" + sSheetName + "]"; ExcelCommand = new OleDbCommand(sQry, ExcelConnection); dsSheet = new DataSet(); ExcelAdapter = new OleDbDataAdapter(ExcelCommand); isValidSheet = false; try { ExcelAdapter.Fill(dsSheet, sSheetName); isValidSheet = true; } catch (Exception ex) { isValidSheet = false; throw new Exception(ex.Message.ToString()); } finally { ExcelAdapter = null; ExcelCommand = null; } } } return isValidSheet; } private DataTable GetSchemaTable() { DataTable dt = null; string connectionString = String.Empty; connectionString = GetConnectionString(); ExcelConnection = new OleDbConnection(connectionString); try { ExcelConnection.Open(); dt = ExcelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); } catch (Exception ex) { throw ex; } return dt; } private string GetConnectionString() { string connStr = String.Empty; try { if (path.ToLower().Contains(".xlsx")) { connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source='" + path + "';" + "Extended Properties='Excel 12.0;HDR=No;IMEX=1;'"; } else if (path.ToLower().Contains(".xlsm")) { connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source='" + path + "';" + "Extended Properties='Excel 12.0 Macro;HDR=No;IMEX=1;'"; } else if (path.ToLower().Contains(".xls")) { connStr = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + path + "';Extended Properties='Excel 8.0;HDR=No;IMEX=1;'"; } else {connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source='" + path + "';" + "Extended Properties='HTML Import;IMEX=1;HDR=No;'"; } } catch (Exception ex) { throw ex; } return connStr; } }