public string RunExport(ExportTemplateEntity helpEntity, params Tuple <DynamicEntity, IEnumerable <DynamicEntity> >[] tuples) { string errorMessage = string.Empty; IEnumerable <CellHelpEntity> list = null; try { list = BeforeBuild(helpEntity); if (list == null || list.Any() == false) { return(errorMessage = "配置模板为空"); } } catch (Exception ex) { errorMessage = ex.Message; } if (!string.IsNullOrEmpty(errorMessage)) { return(errorMessage); } switch (helpEntity.TemplateType) { case TemplateType.Bill: BuildBillWorkbook(helpEntity, list, tuples); break; } workBook.HttpWrite(string.Concat(helpEntity.FileName, ".", helpEntity.Suffix)); return(errorMessage); }
private string BuildBillWorkbook(ExportTemplateEntity helpEntity, IEnumerable <CellHelpEntity> list, params Tuple <DynamicEntity, IEnumerable <DynamicEntity> >[] tuples) { string errorMessage = string.Empty; List <ISheet> listSheet = new List <ISheet>(); ISheet sheet = list.FirstOrDefault().Cell.Sheet; int sheetNum = workBook.GetSheetIndex(sheet.SheetName); listSheet.Add(sheet); int len = tuples.Length; if (len > 1) { for (var i = 0; i < len - 1; i++) { ISheet tempSheet = workBook.CloneSheet(sheetNum); listSheet.Add(tempSheet); } } int index = 0; foreach (var tupe in tuples) { BuildBillSheet(listSheet[index], list, tupe.Item1, tupe.Item2); index++; } return(errorMessage); }
private string BuildSingleWorkbook(ExportTemplateEntity helpEntity, IEnumerable <CellHelpEntity> list, IEnumerable <DynamicEntity> entitys) { string errorMessage = string.Empty; ISheet sheet = list.FirstOrDefault().Cell.Sheet; IRow row = null; ICell cell = null; int index = 0; foreach (var entity in entitys) { foreach (var dimension in list) { row = sheet.GetRow(dimension.FirstRowIndex + index); if (row == null) { row = sheet.CreateRow(dimension.FirstRowIndex + index); } cell = row.GetCell(dimension.FirstColIndex); if (cell == null) { cell = row.CreateCell(dimension.FirstColIndex); } var value = entity.GetPropertyValue(dimension.Name).ToString(); cell.SetCellValue(value); } index++; } return(errorMessage); }
private IEnumerable <CellHelpEntity> BeforeBuild(ExportTemplateEntity helpEntity) { string path = helpEntity.Path; switch (helpEntity.PathType) { case PathType.Http: //path = HttpContext.Current.Server.MapPath(path); byte[] bytes = GetHttpUrlData(path); using (MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length)) { switch (helpEntity.Suffix) { case ExportExcelSuffix.xls: workBook = new HSSFWorkbook(ms); break; case ExportExcelSuffix.xlsx: workBook = new XSSFWorkbook(ms); break; } } break; case PathType.File: path = helpEntity.Path; using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { switch (helpEntity.Suffix) { case ExportExcelSuffix.xls: workBook = new HSSFWorkbook(fs); break; case ExportExcelSuffix.xlsx: workBook = new XSSFWorkbook(fs); break; } } break; } ISheet sheet = workBook.GetSheetAt(0); List <CellHelpEntity> list = new List <CellHelpEntity>(); CellHelpEntity entity = null; for (int rowIndex = sheet.FirstRowNum; rowIndex <= sheet.LastRowNum; rowIndex++) { IRow row = sheet.GetRow(rowIndex); for (short colIndex = row.FirstCellNum; colIndex < row.LastCellNum; colIndex++) { ICell cell = row.GetCell(colIndex); if (cell.IsMergedCell && string.IsNullOrEmpty(cell.ToString())) { continue; } CellDimension dimension = cell.GetSpan(); entity = ProcessCell(dimension); entity.SorceRow = row; if (entity != null) { list.Add(entity); } } } return(list); }