代码拉取完成,页面将自动刷新
同步操作将从 紫色枫林/XPOI 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
基于Java POI开源组件,开发更适用于项目开发的开源组件
一、基于特定excel模板,导出excel文档(目前只支持.xls后缀的模板)
CS结构excel导出
// 数据源
List<JSONObject> list = new LinkedList<JSONObject>();
JSONObject jsonObj = new JSONObject();
jsonObj.put("id", 1);
jsonObj.put("recipientName", 2);
jsonObj.put("recipientEmail", 3);
jsonObj.put("emailTitle", 4);
jsonObj.put("emailContent", 5);
jsonObj.put("state", 6);
jsonObj.put("date", 7);
JSONObject jsonObj1 = new JSONObject();
jsonObj1.put("id", 11);
jsonObj1.put("recipientName", 22);
jsonObj1.put("recipientEmail", 33);
jsonObj1.put("emailTitle", 44);
jsonObj1.put("emailContent", 55);
jsonObj1.put("state", 66);
jsonObj1.put("date", 77);
list.add(jsonObj);
list.add(jsonObj1);
WorkbookDesigner designer = new WorkbookDesigner();
// 模板路径
String templatePath = "D:\\Template.xls";
designer.open(templatePath);
// 添加数据源
designer.setDataSource(list);
// 添加自定义字段
designer.setDataSource("SenderEmail", "888888@qq.com");
designer.setDataSource("SenderDate", "2017-12-08");
designer.process();
// 导出excel
String saveFilePath = "D:\\Template11.xls";
designer.saveFile(saveFilePath);
BS结构excel下载
// 数据源
List<JSONObject> list = new LinkedList<JSONObject>();
JSONObject jsonObj = new JSONObject();
jsonObj.put("id", 1);
jsonObj.put("recipientName", 2);
jsonObj.put("recipientEmail", 3);
jsonObj.put("emailTitle", 4);
jsonObj.put("emailContent", 5);
jsonObj.put("state", 6);
jsonObj.put("date", 7);
JSONObject jsonObj1 = new JSONObject();
jsonObj1.put("id", 11);
jsonObj1.put("recipientName", 22);
jsonObj1.put("recipientEmail", 33);
jsonObj1.put("emailTitle", 44);
jsonObj1.put("emailContent", 55);
jsonObj1.put("state", 66);
jsonObj1.put("date", 77);
list.add(jsonObj);
list.add(jsonObj1);
WorkbookDesigner designer = new WorkbookDesigner();
// 模板路径
String templatePath = "D:\\Template.xls";
designer.open(templatePath);
// 添加数据源
designer.setDataSource(list);
// 添加自定义字段
designer.setDataSource("SenderEmail", "888888@qq.com");
designer.setDataSource("SenderDate", "2017-12-08");
designer.process();
ByteArrayOutputStream os = designer.getOs();
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
response.reset();// response 为 HttpServletResponse对象
String title = "ceshi";
String fileName = new String((title + ".xls").getBytes(), "iso-8859-1");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.setContentLength(content.length);
ServletOutputStream outputStream = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
byte[] buff = new byte[8192];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
outputStream.flush();
outputStream.close();
详细介绍及相关文件下载:XPOI——特定模板excel文件导出
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。