使用NPOI和线程池快速加载EXCEL数据

  private void FilterData()
{
List<Task> tasks = new List<Task>();
IWorkbook workbook = Cshap.Common.ExcelUnit.LoadWorkbook(excelfile_);
for (int sheetIndex = ; sheetIndex < workbook.NumberOfSheets; sheetIndex++)
{
rowCount_ += workbook.GetSheetAt(sheetIndex).PhysicalNumberOfRows;
}
for (int sheetIndex = ; sheetIndex < workbook.NumberOfSheets; sheetIndex++)
{
ISheet sheet = workbook.GetSheetAt(sheetIndex);
tasks.Add(Task.Factory.StartNew(FilterSheet, sheet));
}
Task.WaitAll(tasks.ToArray());
mre.WaitOne();
} void FilterSheet(object state)
{
ISheet sheet = state as ISheet;
for (int rowindex = ; rowindex < sheet.PhysicalNumberOfRows; rowindex++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(FilterRow), sheet.GetRow(rowindex));
}
} void FilterRow(object state)
{
IRow row = state as IRow; var rowEntity = new RowEntity
{
SheetName = row.Sheet.SheetName,
RowData = row, //取行的单元格数据时,不可通过RowData.Cells[#]获取,因为当cell为null,列数会不一致
IsBlank = false,
Cells=new List<ICell>()//必须通过这里获取单元格数据
}; for (int i = ; i < row.Sheet.GetRow().Cells.Count; i++)//标题列单元格一定不为null
{
ICell c = row.GetCell(i);
rowEntity.Cells.Add(c);//无论是否为null,保证列数一致
} if (row.Cells[].CellType == CellType.String && row.Cells[].StringCellValue != null
&& row.Cells[].StringCellValue.ToUpper() == "")
{
lock (lockObject)
{
string pattern = @"(\s*\d{1,2}/\d{1,2}/\d{4}\s*)-(\s*\d{1,2}/\d{1,2}/\d{4}\s*)"; ICell cellOfG = rowEntity.Cells[]; if (cellOfG!=null && (cellOfG.CellType != CellType.Blank && cellOfG.CellType != CellType.String
|| !string.IsNullOrEmpty(cellOfG.StringCellValue) && !Regex.IsMatch(cellOfG.StringCellValue, pattern)))
{
rowEntity.Message = "Date Format Exception";
InvalidRows.Add(rowEntity);
}
else
{
ValidRows.Add(rowEntity);
}
}
} Interlocked.Increment(ref increment_); if (increment_ == rowCount_)
mre.Set();
}
上一篇:mysql 存储过程 事务; mysql的事务中包含一个存储过程


下一篇:删除链表的中间节点和a/b处节点