java
/**
* 生成多sheet Excel
* 带自定义页眉页脚
*
* @param templatePath
* @param sheetList
* @return
* @throws IOException
*/
public static byte[] generateMultiSheet(String templatePath, List<JxlsHelper2.SheetContext> sheetList) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//设置页眉页脚等布局
try (InputStream is = new ClassPathResource(templatePath).getInputStream()) {
XSSFWorkbook wb = new XSSFWorkbook(is);
for (JxlsHelper2.SheetContext sheetContext : sheetList) {
if (StringUtils.isEmpty(sheetContext.getName())) {
continue;
}
XSSFSheet sheet = wb.getSheet(sheetContext.getName());
if (sheet != null) {
translateHeaderTemplate(sheet, sheetContext.getContext());
}
}
wb.write(baos);
//渲染数据
ByteArrayOutputStream targetStream = new ByteArrayOutputStream();
try (InputStream templateStream = new ByteArrayInputStream(baos.toByteArray())) {
JxlsHelper2 jxlsHelper = JxlsHelper2.getInstance();
Transformer transformer = jxlsHelper.createTransformer(templateStream, targetStream);
jxlsHelper.processTemplate(sheetList, transformer);
}
return targetStream.toByteArray();
}
}
重写 package org.jxls.util.JxlsHelper;
java
/**
* jx:area(lastCell="R3" multisheet ="sheets")
*
* @param sheetList
* @param transformer
* @throws IOException
*/
public void processTemplate(List<SheetContext> sheetList, Transformer transformer) throws IOException {
areaBuilder.setTransformer(transformer);
List<Area> xlsAreaList = areaBuilder.build();
for (Area xlsArea : xlsAreaList) {
String sheetName = xlsArea.getStartCellRef().getSheetName();
Optional<SheetContext> optional = sheetList.stream().filter(s -> Objects.equals(sheetName, s.getName())).findAny();
if (optional.isPresent()) {
xlsArea.applyAt(new CellRef(xlsArea.getStartCellRef().getCellName()), optional.get().getContext());
}
}
if (processFormulas) {
for (Area xlsArea : xlsAreaList) {
setFormulaProcessor(xlsArea);
xlsArea.processFormulas();
}
}
if (isHideTemplateSheet()) {
List<String> sheetNameTemplate = getSheetsNameOfMultiSheetTemplate(xlsAreaList);
for (String sheetName : sheetNameTemplate) {
transformer.setHidden(sheetName, true);
}
}
if (isDeleteTemplateSheet()) {
List<String> sheetNameTemplate = getSheetsNameOfMultiSheetTemplate(xlsAreaList);
for (String sheetName : sheetNameTemplate) {
transformer.deleteSheet(sheetName);
}
}
transformer.write();
}
增加DTO
java
@Data
@AllArgsConstructor
public static class SheetContext {
/**
* sheet name
*/
private String name;
private Context context;
}
调用:
java
List<JxlsHelper2.SheetContext> sheetList = new ArrayList<>();
Context context = new Context();
for (String key : contextMap.keySet()) {
context.putVar(key, contextMap.get(key));
}
sheetList.add(new JxlsHelper2.SheetContext("Sheet1", context));
//
Map<String, Object> contextMap2 = defaultExcelReportProvider.getContextMap("7777", "333", "测试", data);
Context context2 = new Context();
for (String key : contextMap2.keySet()) {
context2.putVar(key, contextMap2.get(key));
}
sheetList.add(new JxlsHelper2.SheetContext("Sheet2", context2));
byte[] bytes = JxlsUtil.generateMultiSheet(getExcelTemplatePath(), sheetList);