Use Npoi and thread pools to quickly load Excel data

Source: Internet
Author: User

  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.