protected virtual SheetImportResult ImportSheet(ISheet sheet, SheetFormat format) { SheetImportResult result = new SheetImportResult(); result.Name = format.Name; result.Index = format.Index; int dataColumnEnd; List <string> headerData = this.GetHeaderData(sheet, format, out dataColumnEnd); int rowIndex = format.DataRowStart; bool isEnd = false; IRow dataRow = sheet.GetRow(rowIndex); while (!isEnd && dataRow != null) { List <object> rowData = this.GetRowData(dataRow, format, rowIndex, dataColumnEnd); ImportRowEventArg arg = new ImportRowEventArg(); arg.RowData = rowData; arg.HeaderData = headerData; arg.Format = format; arg.RowIndex = rowIndex; if (this.OnDataRowImporting != null) { this.OnDataRowImporting(this, arg); } if (arg.IsSkip) { result.IncreaseSkiped(); } else if (!string.IsNullOrEmpty(arg.Error)) { result.AddError(string.Format("第{0}处理失败:{1}", rowIndex, arg.Error)); } else if (arg.IsSuccess) { result.IncreaseSuccess(); } isEnd = arg.IsEnd; if (!isEnd && format.AllEmptyIsEnd && rowData == null) { isEnd = true; } if (!isEnd) { rowIndex++; } dataRow = sheet.GetRow(rowIndex); } return(result); }
/// <summary> /// 根据文件流得到 ImportResult /// </summary> /// <param name="stream"></param> /// <returns></returns> public ImportResult DoImport(Stream stream) { ImportResult result = new ImportResult(); try { this.workbook = WorkbookFactory.Create(stream, ImportOption.All); foreach (SheetFormat one in this.Format.Sheets) { if (one != null) { ISheet sheet = null; if (!string.IsNullOrEmpty(one.Name)) { sheet = this.workbook.GetSheet(one.Name); } if (sheet == null) { sheet = this.workbook.GetSheetAt(one.Index); } if (sheet == null) { throw new ApplicationException("无法找到指定的sheet,请检查格式定义是否正确."); } SheetImportResult oneResult = this.ImportSheet(sheet, one); result.AddSheetResult(oneResult); } } } catch (Exception ep) { result.SetError(string.Format("导入失败:{0}", ep.Message)); result.IsSuccess = false; } return(result); }
public ImportResult AddSheetResult(SheetImportResult sheet) { this.Sheets.Add(sheet); return(this); }