zoukankan      html  css  js  c++  java
  • 使用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();
                }
  • 相关阅读:
    poj 3243 Clever Y(BabyStep GiantStep)
    poj 2417 Discrete Logging
    poj 3481 Double Queue
    hdu 4046 Panda
    hdu 2896 病毒侵袭
    poj 1442 Black Box
    hdu 2815 Mod Tree
    hdu 3065 病毒侵袭持续中
    hdu 1576 A/B
    所有控件
  • 原文地址:https://www.cnblogs.com/yipeng-yu/p/5345666.html
Copyright © 2011-2022 走看看