23.POI导出
POI导出
XSSFWorkbook 对应Excel2007版本及以上
HSSFWorkbook 对应Excel2003版本
还要注意一点,不要用Swagger-ui测试导出的表格,这样的表格文件都是损坏的。
1.导入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
2.建表
在这里我有个习惯,就是喜欢把自己做好表头的Excel文件读取到程序中,然后再装填数据。
所以,我很少写更改表格样式的代码。
@Override
public XSSFWorkbook createMemberExcel(List idList, XSSFWorkbook excel) {
List<Member> memberList= (List<Member>) this.listByIds(idList);
int sheetNum = 0;
XSSFSheet sheet = excel.getSheetAt(sheetNum);
int rowNum = 1;
for(Member member:memberList) {
XSSFRow row = sheet.createRow(rowNum++);
int cellNum = 0;
XSSFCell cell = row.createCell(cellNum++);
cell.setCellValue(member.getNum());
cell = row.createCell(cellNum++);
cell.setCellValue(member.getName());
cell = row.createCell(cellNum++);
String sex = member.getSex()== true ? "女" : "男";
cell.setCellValue(sex);
cell = row.createCell(cellNum++);
if(null != member.getBirth()) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
cell.setCellValue(member.getBirth().format(formatter));
}
cell = row.createCell(cellNum++);
String type = member.getType()==true ? "教职工" : "学生";
cell.setCellValue(type);
cell = row.createCell(cellNum++);
cell.setCellValue(member.getDep());
cell = row.createCell(cellNum++);
cell.setCellValue(member.getPro());
cell = row.createCell(cellNum++);
cell.setCellValue(member.getTeam());
cell = row.createCell(cellNum++);
cell.setCellValue(member.getNational());
cell = row.createCell(cellNum++);
cell.setCellValue(member.getOrigin());
cell = row.createCell(cellNum++);
cell.setCellValue(member.getIdCard());
cell = row.createCell(cellNum++);
cell.setCellValue(member.getJoinOrganization());
cell = row.createCell(cellNum++);
if(null != member.getJoinDate()) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
cell.setCellValue(member.getJoinDate().format(formatter));
}
}
return excel;
}
3.文件流导出
private void download(String filename, XSSFWorkbook excel,HttpServletResponse response) throws IOException {
response.setHeader("Access-Control-Expose-Headers","Content-Disposition");
response.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml");
response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(filename, "utf-8"));
OutputStream outputStream = response.getOutputStream();
excel.write(outputStream);
outputStream.flush();
outputStream.close();
/*FileOutputStream fos = new FileOutputStream("1.xlxs");
excel.write(fos);
fos.flush();
fos.close();*/
}
23.POI导出的更多相关文章
- 使用POI导出Word(含表格)的实现方式及操作Word的工具类
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- poi导出excel
Java使用poi组件导出excel报表,能导出excel报表的还可以使用jxl组件,但jxl想对于poi功能有限,jxl应该不能载excel插入浮动层图片,poi能很好的实现输出excel各种功能, ...
- POI导出excel的简单demo
目前使用过两种导出excel的方式,一种是如题所示的使用POI的方式进行数据的导出,这种方式一般只有在处理比较多的数据或者说需要导出的excel表格中有图片之类的需要特殊处理的文件的时候使用:还有一种 ...
- [转载]poi导出excel,可以自定义保存路径
poi导出excel比js导出excel安全性更好,在使用poi导出excel时,先要导入poi-3.5-FINAL-20090928.jar包到你项目的lib目录下,我这里选择是3.5版的 1.ac ...
- POI导出EXCEL经典实现
1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...
- Java POI 导出EXCEL经典实现 Java导出Excel
转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...
- poi导出word
最近做了个poi导出word的功能 下面是代码: 一个可以参考的例子: package com.lzb.crm.web; import java.io.FileOutputStream; import ...
- POI导出大量数据的简单解决方案(附源码)-Java-POI导出大量数据,导出Excel文件,压缩ZIP(转载自iteye.com)
说明:我的电脑 2.0CPU 2G内存 能够十秒钟导出 20W 条数据 ,12.8M的excel内容压缩后2.68M 我们知道在POI导出Excel时,数据量大了,很容易导致内存溢出.由于Excel ...
- java中使用poi导出excel表格数据并且可以手动修改导出路径
在我们开发项目中,很多时候会提出这样的需求:将前端的某某数据以excel表格导出,今天就给大家写一个简单的模板. 这里我们选择使用poi导出excel: 第一步:导入需要的jar包到 lib 文件夹下
随机推荐
- Java原生隐藏字符-工具类
package com.seesun2012.common.util; /** 隐藏字符-工具类 @author seesun2012@163.com */ public class HiddenCh ...
- 8、列表:ion-list
1.基本样式 no-lines 属性 隐藏列表项之间的分割符 inset 属性 去掉 ion-list的 外边框. 默认 的 ion-list 是有外边框的. /* ---示例代码----*/ & ...
- windows使用bat文件定时备份文件
遇到一个需求,需要备份Access数据库,Access生成的数据都保存在xx.mdb文件中,所以考虑使用windows任务 定时执行一个备份文件的bat文件来解决这个问题. backup.bat文件代 ...
- python的if语句
1.条件测试 (1)概念: 每条if语句的核心都是一个值为True或False的表达式,这种表达式被称为条件测试.Python 根据条件测试的值为True还是False来决定是否执行if语句中的代码. ...
- Dubbo与Zookeeper、Spring整合使用 maven+springmvc+dubbo+zookeeper
为什么要用dubbo? 还是让官方来解释吧: http://dubbo.io/User+Guide-zh.htm http://dubbo.io/ 一般 nginx+tomcat | - ...
- .NET开源工作流RoadFlow-表单设计-隐藏域
隐藏域即<input type="hidden" value=""/>标签:
- PopupWindow简单使用
如图是效果图 当点击 “点我”的按钮是 会弹出 如图的 弹窗 补充为PopupWindow设置一个显示动画和消失的动画 先在anim的文件下分别设置显示和消失的动画 <?xml versio ...
- Anaconda教程
python虚拟环境 当安装新的外部python包时,为了保证原版python的纯净,避免其他项目调试时出现错误,可使用Anaconda创建虚拟python进行调试和操作 创建新的虚拟环境(Win ...
- 6 - 常用模块(os,sys,time&datetime,random,json&picle,shelve,hashlib)
导入模块 想使用 Python 源文件,只需在另一个源文件里执行 import 语句 import module1[, module2[,... moduleN] from语句让你从模块中导入一个指定 ...
- 四、获取远程URL图片
#!/usr/bin/python # -*- coding: UTF-8 -*- import re import urllib def getHtml(url): page = urllib.ur ...