最近项目中病人检验报告单生成pdf,选择了EasyPOI.根据纸质报告单预先画好对应的excel模板,采用libreOffice转换成pdf.
1、环境配置
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
2、模板设置
3、模板指令介绍
空格分割
三目运算 {{test ? obj:obj2}}
n: 表示 这个cell是数值类型 {{n:}}
le: 代表长度{{le:()}} 在if/else 运用{{le:() > 8 ? obj1 : obj2}}
fd: 格式化时间 {{fd:(obj;yyyy-MM-dd)}}
fn: 格式化数字 {{fn:(obj;###.00)}}
fe: 遍历数据,创建row
!fe: 遍历数据不创建row
$fe: 下移插入,把当前行,下面的行全部下移.size()行,然后插入
#fe: 横向遍历
v_fe: 横向遍历值
!if: 删除当前列 {{!if:(test)}}
单引号表示常量值 '' 比如'1' 那么输出的就是 1
&NULL& 空格
]] 换行符 多行遍历导出
sum:统计数据
4、demo
String url = "templates/甲流核酸.xls";
TemplateExportParams exportParams = new TemplateExportParams(url,"甲流核酸",0);
Map<String,Object> dataMap = new HashMap<String,Object>();
List<Map<String,Object>> tempList = new ArrayList();
Map<String,Object> tempMap = new HashMap<String,Object>();
tempMap.put("lab_item_name","新型冠状病毒核酸");
tempMap.put("lab_item_en","test");
tempMap.put("lab_result","阴性");
tempMap.put("result_ref","350Copies/ml");
tempMap.put("unit_name","ml");
tempMap.put("result_type_name","↑");
tempMap.put("sort_no","1");
tempList.add(tempMap);
tempMap = new HashMap<String,Object>();
tempMap.put("lab_item_name","新型冠状病毒核酸12");
tempMap.put("lab_item_en","test");
tempMap.put("lab_result","阴性");
tempMap.put("result_ref","350Copies/ml");
tempMap.put("unit_name","ml");
tempMap.put("result_type_name","↑");
tempMap.put("sort_no","2");
tempList.add(tempMap);
dataMap.put("name", "张三");
dataMap.put("pat_dept_name", "感染一科");
dataMap.put("pat_ward_name", "感染区");
dataMap.put("app_doctor_name","李四");
dataMap.put("sample_code","1465");
dataMap.put("sex","男");
dataMap.put("pat_bed_no", "6-02");
dataMap.put("sampletype_name","咽拭子");
dataMap.put("barcode","321081832209");
dataMap.put("age","30");
dataMap.put("local_id","21023300");
dataMap.put("diagnose_name","");
dataMap.put("sample_datetime","2021-08-18 06:56:34");
dataMap.put("sample_recievetime","2021-08-18 07:26:34");
dataMap.put("release_datetime","2021-08-20 16:56:44");
dataMap.put("details",tempList);
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, dataMap);
File savefile = new File("/Users/yaoyao/Desktop");
if (!savefile.exists()) {
boolean result = savefile.mkdirs();
System.out.println("目录不存在,进行创建,创建" + (result ? "成功!" : "失败!"));
}
FileOutputStream fos = new FileOutputStream("/Users/yaoyao/Desktop/test.xls");
workbook.write(fos);
fos.close();