1.导入easypoi依赖
<!-- 导入导出 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.0.3</version>
</dependency>
2.设置导出的数据传输对象
@Data
public class FillData {
private String name;
private double number;
}
3.设置导出的excel模板
4.将模板存放在resource中
5.编写导出的方法
/**
* 根据模板导出excel
*/
@Test
public void test(){
FillData fillData = new FillData();
String caseNumber=null;
if (disEventInfoModel.getCaseNumber()!=null&&!disEventInfoModel.getCaseNumber().equals("")){
caseNumber = disEventInfoModel.getCaseNumber();
}
if (caseNumber==null||caseNumber.equals("")) {
caseNumber = UUID.randomUUID().toString().replace("-", "");
}
// TemplateExportParams params = new TemplateExportParams("exceltemplate/模板.xls");
TemplateExportParams params = new TemplateExportParams(ExcelExportUtils.convertTemplatePath("exceltemplate/模板.xls"));
if (params!=null){
objectToMap(fillData);
Workbook workbook = ExcelExportUtil.exportExcel(params, objectToMap(fillData));
export(response, workbook, URLEncoder.encode(caseNumber + ".xls", "UTF-8"));
}
// Excel 导出 通过浏览器下载的形式
public static void export(HttpServletResponse response, Workbook workbook, String fileName) throws IOException {
response.setHeader("Content-Disposition",
"attachment;filename=" + new String(fileName.getBytes("UTF-8"), "iso8859-1"));
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(response.getOutputStream());
workbook.write(bufferedOutPut);
bufferedOutPut.flush();
bufferedOutPut.close();
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/64398.html