1 Star 0 Fork 8

樂樂 / XPOI

forked from 紫色枫林 / XPOI 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

XPOI

基于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文件导出

空文件

简介

基于Java POI,开发更多、更便捷的开源组件 展开 收起
Java
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/coca_le_cola/XPOI.git
git@gitee.com:coca_le_cola/XPOI.git
coca_le_cola
XPOI
XPOI
master

搜索帮助