官方代码示例:
@Test
public void fe_map() throws Exception {
TemplateExportParams params = new TemplateExportParams(
"WEB-INF/doc/专项支出用款申请书_map.xls");
Map<String, Object> map = new HashMap<String, Object>();
map.put("date", "2014-12-25");
map.put("money", 2000000.00);
map.put("upperMoney", "贰佰万");
map.put("company", "执笔潜行科技有限公司");
map.put("bureau", "财政局");
map.put("person", "JueYue");
map.put("phone", "1879740****");
List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
for (int i = 0; i < 4; i++) {
Map<String, String> lm = new HashMap<String, String>();
lm.put("id", i + 1 + "");
lm.put("zijin", i * 10000 + "");
lm.put("bianma", "A001");
lm.put("mingcheng", "设计");
lm.put("xiangmumingcheng", "EasyPoi " + i + "期");
lm.put("quancheng", "开源项目");
lm.put("sqje", i * 10000 + "");
lm.put("hdje", i * 10000 + "");
listMap.add(lm);
}
map.put("maplist", listMap);
Workbook workbook = ExcelExportUtil.exportExcel(params, map);
File savefile = new File("D:/excel/");
if (!savefile.exists()) {
savefile.mkdirs();
}
//下载到本地
FileOutputStream fos = new FileOutputStream("D:/excel/专项支出用款申请书_map.xls");
workbook.write(fos);
fos.close();
//输出文件流
// ByteArrayOutputStream os = new ByteArrayOutputStream();
// ByteArrayInputStream is = null;
//
// try {
// try {
// wb.write(os);
// } catch (IOException var21) {
// var21.printStackTrace();
// }
//
// byte[] content = os.toByteArray();
// is = new ByteArrayInputStream(content);
// } finally {
// try {
// os.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// }
//
// OutputStream out = null;
// InputStream in = is;
// try {
// //设置文件MIME类型
// String fileName = (String) map.get("fileName");
// response.setContentType("application/octet-stream");
// try {
// fileName = new String((fileName).getBytes("gb2312"), "ISO8859-1");
// } catch (UnsupportedEncodingException e1) {
// e1.printStackTrace();
// }
// //设置Content-Disposition
// response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
// out = response.getOutputStream();
// int b;
// while ((b = in.read()) != -1) {
// out.write(b);
// }
// out.flush();
// in.close();
// out.close();
// } finally {
// try {
// if (null != in) {
// in.close();
// }
// if (null != out) {
// out.close();
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
}
坑坑坑坑坑坑坑坑坑坑坑坑
1.fe的写法 fe标志 冒号 list数据 单个元素数据(默认t,可以不写) 第一个元素
{{KaTeX parse error: Expected 'EOF', got '}' at position 20: …maplist t t.id }̲}--------------…fe: maplist t.id }}
2.如果遍历的数据下面没有内容,用{{fe: maplist t.id }},不用{{$fe: maplist t.id }}
3.遍历数据后可能存在样式问题,如模板单元格合并了,但是导出来的excel并没有合并,需要手动处理
//发票sheet
Sheet fpSheet = wb.getSheetAt(1);
for (int i = 15; i < 15 + size; i++) {
if(!isMergedRegion(fpSheet, i, 1)) {
CellRangeAddress nsTwelveFpSpmc = new CellRangeAddress(i, i, 0, 1); //合并商品名称单元格
fpSheet.addMergedRegion(nsTwelveFpSpmc);
}
}
/**
* 判断指定的单元格是否是合并单元格
* @param sheet
* @param row 行下标
* @param column 列下标
* @return
*/
private boolean isMergedRegion(Sheet sheet,int row ,int column) {
int sheetMergeCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetMergeCount; i++) {
CellRangeAddress range = sheet.getMergedRegion(i);
int firstColumn = range.getFirstColumn();
int lastColumn = range.getLastColumn();
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
if(row >= firstRow && row <= lastRow){
if(column >= firstColumn && column <= lastColumn){
return true;
}
}
}
return false;
}