我正在使用 SSIS 包清理 .Xlsx 文件中的数据并将其加载到 SQL Server 表中。
我还必须突出显示 .Xlsx 文件中包含错误数据的单元格,为此我必须根据列名称和行 ID(我在数据电子表格中拥有)获取列和行索引。为此,我比较第一个电子表格中的每个列名称(Error_Sheet
)与我在第二个电子表格中添加的列的行并对行执行相同的操作,如果我具有相同的单元格值,我会返回数据电子表格的列和行索引,并突出显示基于该列的单元格和行索引。该脚本运行良好,但在尝试从服务器运行它后,我遇到了内存异常,并且在我的工作站上也运行良好,之前它运行良好。
我尝试缩小从中获取数据的范围:AC1:AC10000
to 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
请代替数据集和 DataTable 从 xlsx 文件中获取单元格值、列和行索引?
我没有展示完整的代码,因为它很长。如果需要更多信息,请随时通知我。
当尝试从具有大量行的 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
参考
- 查询结果分页
- 填充(数据集,Int32,Int32,字符串)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)