Java使用POI导出excel记录

eg:

@Override
public void exportExcel(HttpServletResponse response) throws Exception{
// 创建Excel文档
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("设备厂商");

// 创建表头
XSSFRow header = sheet.createRow(0);
header.createCell(0).setCellValue("ID");
header.createCell(1).setCellValue("厂商名称");
header.createCell(2).setCellValue("信用编码");
header.createCell(3).setCellValue("厂商简称");
header.createCell(4).setCellValue("地址");
header.createCell(5).setCellValue("联系人");
header.createCell(6).setCellValue("所在城市");
header.createCell(7).setCellValue("联系人手机号");
header.createCell(8).setCellValue("设备类型名称");

// 填充数据
List<ManufacturerVo> list = recManufacturerDao.findAll();
int rowIndex = 1;
for (ManufacturerVo manufacturer : list) {
XSSFRow row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(manufacturer.getId());
row.createCell(1).setCellValue(manufacturer.getName());
row.createCell(2).setCellValue(manufacturer.getCredit());
row.createCell(3).setCellValue(manufacturer.getAbbreviation());
row.createCell(4).setCellValue(manufacturer.getAddress());
row.createCell(5).setCellValue(manufacturer.getContactPerson());
row.createCell(6).setCellValue(manufacturer.getDescription());
row.createCell(7).setCellValue(manufacturer.getContactPhone());
row.createCell(8).setCellValue(manufacturer.getDeviceCategoryName());
}

String percentEncodedFileName = percentEncode("设备厂商");
StringBuilder contentDispositionValue = new StringBuilder();
contentDispositionValue.append("attachment; filename=")
.append(percentEncodedFileName)
.append(";")
.append("filename*=")
.append("utf-8''")
.append(percentEncodedFileName);
// 设置响应头信息
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", contentDispositionValue.toString());
response.setHeader("download-filename", percentEncodedFileName);
// response.setContentType("application/vnd.ms-excel");
// response.setHeader("Content-Disposition", "attachment; filename=users.xlsx");

// Excel文档写入响应流中
ServletOutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
}

/**
* 百分号编码工具方法
*
* @param s 需要百分号编码的字符串
* @return 百分号编码后的字符串
*/
public static String percentEncode(String s) throws UnsupportedEncodingException {
String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
return encode.replaceAll("\\+", "%20");
}
posted @   sensen~||^_^|||&  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
历史上的今天:
2021-08-06 Consider defining a bean of type 'com.baizhi.service.EmpService' in your configuration.
2021-08-06 Navicat15 注册机安装步骤网址链接
2021-08-06 Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project ems: There are test failures.
2021-08-06 Cannot resolve taglib with uri http://java.sun.com/jsp/jstl/cor
2021-08-06 Request method 'GET' not supported.
点击右上角即可分享
微信分享提示