Private voidFilterdata () {List<Task> tasks =NewList<task>(); Iworkbook Workbook=Cshap.Common.ExcelUnit.LoadWorkbook (Excelfile_); for(intSheetindex =0; Sheetindex < workbook. Numberofsheets; sheetindex++) {Rowcount_+=workbook. Getsheetat (Sheetindex). Physicalnumberofrows; } for(intSheetindex =0; Sheetindex < workbook. Numberofsheets; sheetindex++) {Isheet sheet=workbook. Getsheetat (Sheetindex); Tasks. ADD (Task.Factory.StartNew (Filtersheet, sheet)); } task.waitall (Tasks. ToArray ()); MRe. WaitOne (); } voidFiltersheet (ObjectState ) {Isheet sheet= State asIsheet; for(introwindex =0; rowindex < sheet. Physicalnumberofrows; rowindex++) {ThreadPool.QueueUserWorkItem (NewWaitCallback (filterrow), sheet. GetRow (rowindex)); } } voidFilterrow (ObjectState ) {IRow row= State asIRow; varRowentity =Newrowentity {sheetname=row. Sheet.sheetname, RowData=Row ,//row cell data is not available through rowdata.cells[#] because when the cell is null, the number of columns is inconsistent IsBlank=false, Cells=NewList<icell>()//must get cell data through here}; for(inti =0; I < row. Sheet.getrow (0). Cells.count; i++)//Header column cell must not be null {Icell c=row. Getcell (i); ROWENTITY.CELLS.ADD (c);//Whether it is null or not, ensure that the number of columns is consistent}if(Row. cells[0]. Celltype = = celltype.string && row. cells[0]. Stringcellvalue! =NULL&& row. cells[0]. Stringcellvalue.toupper () = ="123456") { Lock(lockobject) { stringPattern =@"(\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 (refIncrement_); if(Increment_ = =Rowcount_) mre. Set (); }
Use Npoi and thread pools to quickly load Excel data