我使用SSIS包将数据从.Xlsx文件中清理并加载到Server表中。我还必须突出显示.Xlsx文件中包含错误数据的单元格,为此,我必须根据列名和行id(在我的数据电子表格中)获取回列和行索引。为此,我将第一个电子表格(Error_Sheet)中的每个列名与我在第二个电子表格中添加的列行进行比较,并对行执行相同的操作;如果单元格的值相同,则返回数据电子表格的列和行索引,并根据该列和行索引突出显示单元格。脚本运行得很好,但是在试图从服务器运行它之后,我得到了一个内存异常,并且在我的工作站上,它以前运行得很好。
我试图缩小从:AC1:AC10000到AC1:AC100的数据范围,它只在第一次编译之后才能工作,但它继续抛出异常。
string strSQLErrorColumns = "Select * From [" + Error_Sheet + "AC1:AC100]";
OleDbConnection cn = new OleDbConnection(strCn);
OleDbDataAdapter objAdapterErrorColumns = new OleDbDataAdapter(strSQLErrorColumns, cn);
System.Data.DataSet dsErrorColumns = new DataSet();
objAdapterErrorColumns.Fill(dsErrorColumns, Error_Sheet);
System.Data.DataTable dtErrorColumns = dsErrorColumns.Tables[Error_Sheet];
dsErrorColumns.Dispose();
objAdapterErrorColumns.Dispose();
foreach (DataColumn ColumnData in dtDataColumns.Columns){
ColumnDataCellsValue = dtDataColumns.Columns[iCntD].ColumnName.ToString();
iCntE = 0;
foreach (DataRow ColumnError in dtErrorColumns.Rows){
ColumnErrorCellsValue = dtErrorColumns.Rows[iCntE].ItemArray[0].ToString();
if (ColumnDataCellsValue.Equals(ColumnErrorCellsValue)){
ColumnIndex = ColumnData.Table.Columns[ColumnDataCellsValue].Ordinal;
iCntE = iCntE + 1;
break;
}
}
iCntD = iCntD + 1;
}
ColumnIndexHCell = ColumnIndex + 1;
RowIndexHCell = RowIndex + 2;
Range rng = xlSheets.Cells[RowIndexHCell, ColumnIndexHCell] as Excel.Range;
rng.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);还有其他方法可以在DataTable中加载数据以获得列和行索引而不需要占用大量内存,或者使用Excel.Range.Cell代替dataset和DataTable从xlsx文件中获取单元格值、列和行索引?
我没有显示全部代码,因为它很长。如果需要更多的信息,请随时通知我。
发布于 2019-02-15 21:07:04
当尝试从具有大量行数的Excel中读取数据时,最好按块读取数据(在OleDbDataAdapter中,您可以使用分页选项来实现这一点)。
int result = 1;
int intPagingIndex = 0;
int intPagingInterval = 1000;
while (result > 0){
result = daGetDataFromSheet.Fill(dsErrorColumns,intPagingIndex, intPagingInterval , Error_Sheet);
System.Data.DataTable dtErrorColumns = dsErrorColumns.Tables[Error_Sheet];
//Implement your logic here
intPagingIndex += intPagingInterval ;
}这将防止OutOfMemory异常。不再需要指定像AC1:AC10000这样的范围
参考资料
https://stackoverflow.com/questions/54713652
复制相似问题