使用NPOI和线程池快速加载EXCEL数据
Posted MessageBox
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用NPOI和线程池快速加载EXCEL数据相关的知识,希望对你有一定的参考价值。
private void FilterData() { List<Task> tasks = new List<Task>(); IWorkbook workbook = Cshap.Common.ExcelUnit.LoadWorkbook(excelfile_); for (int sheetIndex = 0; sheetIndex < workbook.NumberOfSheets; sheetIndex++) { rowCount_ += workbook.GetSheetAt(sheetIndex).PhysicalNumberOfRows; } for (int sheetIndex = 0; 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 = 0; 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 = 0; i < row.Sheet.GetRow(0).Cells.Count; i++)//标题列单元格一定不为null { ICell c = row.GetCell(i); rowEntity.Cells.Add(c);//无论是否为null,保证列数一致 } if (row.Cells[0].CellType == CellType.String && row.Cells[0].StringCellValue != null && row.Cells[0].StringCellValue.ToUpper() == "123456") { 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[6]; 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(); }
以上是关于使用NPOI和线程池快速加载EXCEL数据的主要内容,如果未能解决你的问题,请参考以下文章
newCacheThreadPool()newFixedThreadPool()newScheduledThreadPool()newSingleThreadExecutor()自定义线程池(代码片段