您的位置:首页 > 编程语言 > Java开发

Spring Boot--POI导出excel文件下载

2017-12-05 22:03 686 查看
1. 依赖

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>


2. 代码

2.1 写入excel

/**
* 导出excel
* @param list 数据集合
* @param column 列名
* @param templatePath 模板路径
* @param os  输出流
*/
public static <T
4000
> void exportExcel(List<T> list, String[] column, String templatePath, OutputStream os) {

// 获取列名map
Map<String, String> map = XmlParser.getColumnName(templatePath);

// 声明一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
// 声明一个单子并命名
HSSFSheet sheet = wb.createSheet("1");
// 给单子名称一个长度
sheet.setDefaultColumnWidth((short) 15);
// 生成一个样式
HSSFCellStyle style = wb.createCellStyle();
// 创建第一行(也可以称为表头)
HSSFRow row = sheet.createRow(0);
// 样式字体居中
style.setAlignment(HorizontalAlignment.CENTER);
// 给表头第一行一次创建单元格
if (column == null || column.length == 0)
return ;
for (int index = 0; index < column.length; index++) {
HSSFCell cell = row.createCell((short) index);
// 名称..
cell.setCellValue(map.get(column[index]));
cell.setCellStyle(style);
}

// 将集合转成list
// 有可能报错,json序列化死循环
JSONArray jsonarray = JSONArray.fromObject(list);

// 向单元格里填充数据
for (short i = 0; i < jsonarray.size(); i++) {
row = sheet.createRow(i + 1);
JSONObject jsonObject = jsonarray.getJSONObject(i);
for (int index = 0; index < column.length; index++) {
row.createCell(index).setCellValue(jsonObject.get(column[index]) + "");
}
}

try {
wb.write(os);
System.out.println("导出成功");
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
}


2.2 控制层调用

/**
* 下载
*
* @throws IOException
*/
@RequestMapping("/export")
public void export(String templatePath,
HttpServletResponse response) throws IOException {
// 查询所有的数据
List<Student> findAll = studentService.findAll();
String[] str = { "id", "name"};//列名
// 写入文件,得到文件
Date date = new Date(System.currentTimeMillis());
// 转换提日期输出格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String name =  dateFormat.format(date) + ".xls";
response.addHeader("Content-Disposition",
"attachment;filename=" + new String(name.getBytes("UTF-8"), "ISO8859-1"));
response.setContentType("application/octet-stream");
OutputStream toClient = null;
toClient = new BufferedOutputStream(response.getOutputStream());
ExcelUtil.exportExcel(findAll, str, templatePath, response.getOutputStream());
toClient.flush();
response.getOutputStream().close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: