目前用的还是历史版本,对原来的历史版本底层复制做了一定的重构
List<Map<String, Object>> maplist = invoiceDao.queryInviceList(invoiceNo, null, null);
List<List<Map<String, Object>>> listPartition = Lists.partition(maplist, 5);
Map<String, Object> bankinfo = templateDao.queryBankInfo(template.getBankCode());// 获取模版信息
map.put("bankinfo", bankinfo);
// 把两个list 数据 put到对应的模板KEY 下面去
Map<Integer, Map<String, Object>> realMap = new HashMap<>();
Integer[] sheetNum = new Integer[listPartition.size()];
String[] sheetName = new String[listPartition.size()];
int i=0;
for (List<Map<String, Object>> list : listPartition) {
List<Map<String, Object>> newList=new ArrayList<>();
Map<String, Object> newMap =new HashMap<>();
sheetNum[i]=i;
sheetName[i]="IN"+i;
BigDecimal totalAmount = BigDecimal.ZERO;
int j = 0;
for (Map<String, Object> map2 : list) {
j++;
totalAmount = totalAmount.add(BigDecimalUtils.isNull(map2.get("amount")));
map2.put("index", j);
newList.add(map2);
}
map.put("page", i+1+" of "+listPartition.size());
map.put("totalAmount", totalAmount);
map.put("maplist", newList);
newMap.putAll(map);
realMap.put(i, newMap);
i++;
}
params.setSheetNum(sheetNum);
params.setSheetName(sheetName);
Workbook workbook = ExcelExportUtil.exportExcel(realMap, params);
ByteArrayOutputStream fos;
try {
fos = new ByteArrayOutputStream();
workbook.write(fos);
File htmlFile = File.createTempFile("IN_" + invoiceNo+"_", ".xls", destFile);
org.apache.commons.io.FileUtils.writeByteArrayToFile(htmlFile, fos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
Workbook workbook = ExcelExportUtil.exportExcel(realMap, params);
注意realMap的数据架构 然后我发一下我重构的克隆文件
原easypoi代码
/**
* 导出文件通过模板解析只有模板,没有集合
* 每个sheet对应一个map,导出到处,key是sheet的NUM
* @param params
* 导出参数类
* @param map
* 模板集合
* @return
*/
public static Workbook exportExcel(Map<Integer, Map<String, Object>> map,
TemplateExportParams params) {
return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, map);
}
public Workbook createExcleByTemplate(TemplateExportParams params, Map<Integer, Map<String, Object>> map) {
// step 1. 判断模板的地址
if (params == null || map == null || StringUtils.isEmpty(params.getTemplateUrl())) {
throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
}
Workbook wb = null;
// step 2. 判断模板的Excel类型,解析模板
try {
this.teplateParams = params;
wb = getCloneWorkBook();
// 创建表格样式
setExcelExportStyler ((IExcelExportStyler) teplateParams.getStyle ()
.getConstructor (Workbook.class).newInstance (wb));
// step 3. 解析模板
for (int i = 0, le = params.isScanAllsheet () ? wb.getNumberOfSheets ()
: params.getSheetNum ().length; i < le; i++) {
tempCreateCellSet.clear ();
// 根据模板创建一个新的sheet
Sheet sheet = wb.cloneSheet (0);
parseTemplate (sheet, map.get (i), params.isColForEach ());
if (params.getSheetName () != null && params.getSheetName ().length > i
&& StringUtils.isNotEmpty (params.getSheetName ()[i])) {
wb.setSheetName (i+1, params.getSheetName ()[i]);
}
}
// 删除第一个sheet 第一个为模板
wb.removeSheetAt (0);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
return null;
}
return wb;
}
// 删除第一个sheet 第一个为模板
wb.removeSheetAt (0);
这里是因为第一个sheet名称错乱需要删掉模板
接下来放模板
导出效果