认证-报库-自定义导出excel模板-导出历史

This commit is contained in:
liyawei
2022-08-01 18:04:02 +08:00
parent 6387cea86b
commit 8b87b177c8
2 changed files with 47 additions and 3 deletions
@@ -829,7 +829,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
InputStream excelTemplate = CosBootUtil.download(templateUrl);
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.load(wordTemplate);
Workbook workbook = WorkbookFactory.create(excelTemplate);
ExcelUtil.replacePictureBatchPOI(workbook, pkg, imgListAll);
ExcelUtil.replacePictureBatchPOI(workbook, pkg, imgListAll, params);
ExcelUtil.variableReplace(pkg, params);
pkg.save(wordOS);
@@ -862,6 +862,19 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
os.write(len2);
}
// 添加导出历史
String uploadFileName = fileOriName + ".zip";
InputStream uploadFileio = new FileInputStream(new File(fileNowPath+".zip"));
MultipartFile mFile = new MockMultipartFile(uploadFileName, uploadFileName, "text/plain", uploadFileio); // 用于上传
OSSFile ossFile = ossFileService.uploadLocalOfCos(mFile, "/report", "", CutEnum.CN.getValue()); // 上传导出的压缩包
ParamsReportExportHistoryEO paramsReportExportHistoryEO = new ParamsReportExportHistoryEO();
paramsReportExportHistoryEO.setExportType(ExportTypeEnum.CUSTOM.getValue());
paramsReportExportHistoryEO.setExportFileId(ossFile.getId());
paramsReportExportHistoryEO.setParamsManifestId(paramsReportDetailVO.getParamsManifestId());
paramsReportExportHistoryEO.setExportTime(new Date());
paramsReportExportHistoryEOService.add(paramsReportExportHistoryEO);
uploadFileio.close();
os.flush();
os.close(); // 后开先关
fis.close(); // 先开后关
@@ -70,7 +70,22 @@ public class ExcelUtil {
System.out.println("mmmmmmm");
}
public static void replacePictureBatchPOI(Workbook workbook, SpreadsheetMLPackage pkg, List<Map<String, Object>> imgListAll) throws Exception {
public static void setRowHeight(WorksheetPart worksheetPart, int rIndex, double height, int cIndex, String text) {
SheetData sheetData = worksheetPart.getJaxbElement().getSheetData();
org.xlsx4j.sml.Row r = sheetData.getRow().get(rIndex);
if (r.getHt() == null || (r.getHt() != null && r.getHt() < height)) {
r.setHt(height);
r.setCustomHeight(true);
}
Cell c = r.getC().get(cIndex);
if (text == null) {
c.setV(null);
}
}
public static void replacePictureBatchPOI(Workbook workbook, SpreadsheetMLPackage pkg, List<Map<String, Object>> imgListAll, HashMap<String, String> params) throws Exception {
//获取全部sheet页中表格
Sheet sheet=null;
int partNameNum = 0;
@@ -83,6 +98,7 @@ public class ExcelUtil {
int totalRowNum = sheet.getLastRowNum();
//获得所有数据
int k = 0;
for (int x = 0; x < totalRowNum; x++) {
//获得第x行对象
Row row = sheet.getRow(x);
@@ -90,12 +106,14 @@ public class ExcelUtil {
//获得总列数
int cellLength = row.getLastCellNum();
//如果x行里的所有单元格
int j = 0;
for (int y = 0; y < cellLength; y++) {
org.apache.poi.ss.usermodel.Cell cell = row.getCell(y);
if(ObjectUtil.isNull(cell)) {
continue;
}
String cellValue = cell.getStringCellValue();
for (Map<String, Object> imgMap : imgListAll) {
@@ -103,10 +121,23 @@ public class ExcelUtil {
String text = (String) imgMap.get("text");
byte[] bytes = (byte[]) imgMap.get("bytes");
if (("${" + key + "}").equals(cellValue)) {
// 根据图片大小调整行高
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
BufferedImage bufferedImage = ImageIO.read(in);
setRowHeight(worksheetPart, k, bufferedImage.getHeight(), j, text);
if (text != null) {
params.put(key, text);
}
// 插入图片到表格
putImagePOI(worksheetPart, pkg, cell, bytes, sheet.getSheetName().toLowerCase(), String.valueOf(partNameNum));
}
}
j++; // 单元格非空才++
}
k++; // 行非空才++
}
}
@@ -173,7 +204,7 @@ public class ExcelUtil {
anchor.getFrom().setCol(newCell.getColumnIndex());
anchor.getFrom().setRow(newCell.getRowIndex());
anchor.setTo(new CTMarker());
anchor.getTo().setCol(newCell.getColumnIndex() + 1);
anchor.getTo().setCol(newCell.getColumnIndex() + 1); // TODO
anchor.getTo().setRow(newCell.getRowIndex() + 1);
CTPicture picture = new CTPicture();