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导出的更多相关文章

  1. 使用POI导出Word(含表格)的实现方式及操作Word的工具类

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...

  2. poi导出excel

    Java使用poi组件导出excel报表,能导出excel报表的还可以使用jxl组件,但jxl想对于poi功能有限,jxl应该不能载excel插入浮动层图片,poi能很好的实现输出excel各种功能, ...

  3. POI导出excel的简单demo

    目前使用过两种导出excel的方式,一种是如题所示的使用POI的方式进行数据的导出,这种方式一般只有在处理比较多的数据或者说需要导出的excel表格中有图片之类的需要特殊处理的文件的时候使用:还有一种 ...

  4. [转载]poi导出excel,可以自定义保存路径

    poi导出excel比js导出excel安全性更好,在使用poi导出excel时,先要导入poi-3.5-FINAL-20090928.jar包到你项目的lib目录下,我这里选择是3.5版的 1.ac ...

  5. POI导出EXCEL经典实现

    1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...

  6. Java POI 导出EXCEL经典实现 Java导出Excel

    转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...

  7. poi导出word

    最近做了个poi导出word的功能 下面是代码: 一个可以参考的例子: package com.lzb.crm.web; import java.io.FileOutputStream; import ...

  8. POI导出大量数据的简单解决方案(附源码)-Java-POI导出大量数据,导出Excel文件,压缩ZIP(转载自iteye.com)

    说明:我的电脑 2.0CPU 2G内存 能够十秒钟导出 20W 条数据 ,12.8M的excel内容压缩后2.68M 我们知道在POI导出Excel时,数据量大了,很容易导致内存溢出.由于Excel ...

  9. java中使用poi导出excel表格数据并且可以手动修改导出路径

    在我们开发项目中,很多时候会提出这样的需求:将前端的某某数据以excel表格导出,今天就给大家写一个简单的模板. 这里我们选择使用poi导出excel: 第一步:导入需要的jar包到 lib 文件夹下

随机推荐

  1. 【SoapUI】http接口测试

    一.接口介绍 API(Application Programming Interface,应用程序编程接口) 1.硬件接口 USB接口 硬盘接口 SD卡接口 LAN口和WAN口 CONSOLE口 .. ...

  2. python的用户输入和while循环

    1.函数input()工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. (1)获取数值可以用 int()函数 (2)求 ...

  3. 【数据库】8.0 MySQL入门学习(八)——创建并使用数据库、获得数据库和表的信息

    1.0 使用SHOW语句找出服务器上当前存在什么数据库: mysql> SHOW DATABASES; 每台机器上的数据库列表是不同的,但是很可能有mysql和test数据库.mysql是必需的 ...

  4. SQL Server ->> 字符串对比

    今天同事问我关于SQL Server在字符串尾随着空格时进行字符串对比的做法.关于这个问题正好在这里讲一下,就是SQL Server是按照ANSI/ISO SQL-92中的定义做字符串对比的. 在KB ...

  5. linux中启动网卡报错:Bringing up interface eth1: Error: Connection activation failed

    在重启linux网络服务的时候提示: Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/2 并且产生报错 ...

  6. ORACLE_DELETE

    SQL DELETE Statement The SQL DELETE Statement The DELETE statement is used to delete existing record ...

  7. WAKE-WIN10-SOFT-GITHUB

    1,GITHUB 官网:https://github.com/ 2,软件工具 ,,,,,,

  8. 【Spring实战】—— 2 构造注入

    本文讲解了构造注入以及spring的基本使用方式,通过一个杂技演员的例子,讲述了依赖注入属性或者对象的使用方法. 如果想要使用spring来实现依赖注入,需要几个重要的步骤: 1 定义主要的类和需要分 ...

  9. 配置xtrabackup备份mysql数据库

    下载地址:https://www.percona.com/downloads/XtraBackup/LATEST/ 为了方便起见本次安装使用yum源安装方式 1    安装yum源:yum insta ...

  10. 关于GitHubGit

    一.Github项目地址:https://github.com/gyguyt/Helloworld123 二.什么是Github? Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或 ...