当前位置:网站首页>poi导出excle
poi导出excle
2022-06-27 06:30:00 【Mr_ZhangAdd】
原本想使用别人的轮子,想想还是使用poi,毕竟别人也是封装poi。
过程就是:
创建工作簿、创建工作表、根据传来的数据 创建行、在创建单元格,并赋值 。
最后 封装response,然后写入输出流、然后刷新并输出、输出完毕关闭输出
/**
* 对文件流输出下载的中文文件名进行编码 屏蔽各种浏览器版本的差异性
*
* @throws UnsupportedEncodingException
*/
public static String encodeChineseDownloadFileName(
HttpServletRequest request, String pFileName) throws Exception {
String filename = null;
String agent = request.getHeader("USER-AGENT");
if (null != agent) {
if (-1 != agent.indexOf("Firefox")) {//Firefox
filename = "=?UTF-8?B?" + (new String(org.apache.commons.codec.binary.Base64.encodeBase64(pFileName.getBytes("UTF-8")))) + "?=";
} else if (-1 != agent.indexOf("Chrome")) {//Chrome
filename = new String(pFileName.getBytes(), "ISO8859-1");
} else {//IE7+
filename = java.net.URLEncoder.encode(pFileName, "UTF-8");
filename = filename.replace("+", "%20");
}
} else {
filename = pFileName;
}
return filename;
} public static void exportCustomerGoodsTotalByMonth(HttpServletRequest request,
HttpServletResponse response,
Company company,
List<String> customers,
Map<String, CustomerOrderExortExcleDataDTO> listMap
) throws IOException {
List<Map<String, Object>> list = new ArrayList<>();
//设置文件名
String fileName = customers.stream().collect(Collectors.joining("-"));
//创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
for (Map.Entry<String, CustomerOrderExortExcleDataDTO> entry : listMap.entrySet()) {
CustomerOrderExortExcleDataDTO customerOrderExortExcleDataDTO = entry.getValue();
List<List<String>> dataList = customerOrderExortExcleDataDTO.getDataList();
//创建一个sheet
HSSFSheet sheet = wb.createSheet(entry.getKey());
//创建表头,如果没有跳过
int headerrow = 0;
List<String> headers = customerOrderExortExcleDataDTO.getColTitleList();//{"订单ID","发货日期","数量"};
if (customerOrderExortExcleDataDTO != null && customerOrderExortExcleDataDTO.getHeadTitle() != null) {
HSSFRow row = sheet.createRow(headerrow);
//表头样式
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontName(excelfont);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
HSSFCell cell = row.createCell(0);
cell.setCellValue(customerOrderExortExcleDataDTO.getHeadTitle());
cell.setCellStyle(style);
HSSFCell cell2 = row.createCell(headers.size() - 1);
cell2.setCellStyle(style);
sheet.addMergedRegion(new CellRangeAddress(
0, //first row (0-based)
0, //last row (0-based)
0, //first column (0-based)
headers.size() - 1 //last column (0-based)
));
headerrow++;
}
if (headers != null) {
HSSFRow row = sheet.createRow(headerrow);
//表头样式
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontName(excelfont);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
for (int i = 0; i < headers.size(); i++) {
if (i == 0) {
sheet.setColumnWidth(i, 450 * 10);
} else {
sheet.setColumnWidth(i, 350 * 10);
}
HSSFCell cell = row.createCell(i);
cell.setCellValue(headers.get(i));
cell.setCellStyle(style);
}
headerrow++;
}
if (!ObjectUtils.isEmpty(dataList)) {
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setFontName(excelfont);
font.setFontHeightInPoints((short) 10);
style.setFont(font);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
for (int i = 0; i < dataList.size(); i++) { //行数
HSSFRow row = sheet.createRow(headerrow);
List<String> rowDataList = dataList.get(i);
for (int j = 0; j < rowDataList.size(); j++) { //列数
HSSFCell cell = row.createCell(j);
cell.setCellValue(rowDataList.get(j) + "");
cell.setCellStyle(style);
}
headerrow++;
}
}
}
fileName = fileName + ".xls";
String filename = "";
try {
filename = encodeChineseDownloadFileName(request, fileName);
} catch (Exception e) {
e.printStackTrace();
}
response.reset();
response.setHeader("Content-disposition", filename);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + filename);
response.setHeader("Pragma", "No-cache");
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}边栏推荐
- Cloud-Native Database Systems at Alibaba: Opportunities and Challenges
- 研究生数学建模竞赛-无人机在抢险救灾中的优化应用
- Crawler learning 5--- anti crawling identification picture verification code (ddddocr and pyteseract measured effect)
- 线程间等待与唤醒机制、单例模式、阻塞队列、定时器
- 426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary
- Tar: /usr/local: cannot find tar in the Archive: due to the previous error, it will exit in the last error state
- Using CSDN to develop cloud and build navigation websites
- 建模竞赛-光传送网建模与价值评估
- JVM garbage collection mechanism
- 2022 CISP-PTE(一)文件包含
猜你喜欢

Winow10 installation nexus nexus-3.20.1-01

研究生数学建模竞赛-无人机在抢险救灾中的优化应用

AHB2APB桥接器设计(2)——同步桥设计的介绍

JVM object composition and storage

Centos7.9 install MySQL 5.7 and set startup

426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary

Distribution gaussienne, régression linéaire, régression logistique

Redis cache penetration, cache breakdown, cache avalanche

The fourth question of the 299th weekly match 6103 Minimum fraction of edges removed from the tree

多线程带来的的风险——线程安全
随机推荐
解决 Win10 Wsl2 IP 变化问题
TiDB的使用限制
JVM对象组成和存储
When there are multiple El select, the selected value is filtered by El select, and the last selected value is filtered by the second El select
Crawler learning 5--- anti crawling identification picture verification code (ddddocr and pyteseract measured effect)
HTAP Quick Start Guide
论文阅读技巧
2022 CISP-PTE(一)文件包含
机 器 学 习
Assembly language - Wang Shuang Chapter 9 Principles of transfer instructions - Notes
Basic SQL operations in tidb
Assembly language - Wang Shuang Chapter 8 two basic problems in data processing - Notes
427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
An Empirical Evaluation of In-Memory Multi-Version Concurrency Control
AHB2APB桥接器设计(2)——同步桥设计的介绍
Assembly language - Wang Shuang Chapter 11 flag register - Notes
HTAP in depth exploration Guide
Compatibility comparison between tidb and MySQL
下载cuda和cudnn
Using CSDN to develop cloud and build navigation websites