当前位置:网站首页>POI export excle
POI export excle
2022-06-27 06:48:00 【Mr_ ZhangAdd】
Originally wanted to use someone else's wheel , Think or use poi, After all, others are also encapsulated poi.
Process is :
Create Workbook 、 Create sheet 、 According to the data transmitted Create lines 、 In creating cells , And the assignment .
Last encapsulation response, Then write the output stream 、 Then refresh and output 、 When the output is finished, close the output
/**
* Encode the Chinese file name downloaded from the file stream output Shield the differences of various browser versions
*
* @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<>();
// Set file name
String fileName = customers.stream().collect(Collectors.joining("-"));
// Create a workbook
HSSFWorkbook wb = new HSSFWorkbook();
for (Map.Entry<String, CustomerOrderExortExcleDataDTO> entry : listMap.entrySet()) {
CustomerOrderExortExcleDataDTO customerOrderExortExcleDataDTO = entry.getValue();
List<List<String>> dataList = customerOrderExortExcleDataDTO.getDataList();
// Create a sheet
HSSFSheet sheet = wb.createSheet(entry.getKey());
// Create header , If you don't skip
int headerrow = 0;
List<String> headers = customerOrderExortExcleDataDTO.getColTitleList();//{" Order ID"," Delivery date "," Number "};
if (customerOrderExortExcleDataDTO != null && customerOrderExortExcleDataDTO.getHeadTitle() != null) {
HSSFRow row = sheet.createRow(headerrow);
// Header style
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);
// Header style
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++) { // Row number
HSSFRow row = sheet.createRow(headerrow);
List<String> rowDataList = dataList.get(i);
for (int j = 0; j < rowDataList.size(); j++) { // Number of columns
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();
}边栏推荐
- [getting started] regular expression Basics
- Proxy reflect usage details
- 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
- Visual Studio VS 快捷键使用大全
- 快速实现蓝牙iBeacn功能详解
- Scala advanced_ Member access modifier
- multiprocessing.pool详解
- Altium designer 19 device silk screen label position shall be placed uniformly in batches
- Scala函数柯里化(Currying)
- Cloud-Native Database Systems at Alibaba: Opportunities and Challenges
猜你喜欢

Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer

Idea one click log generation

快速实现蓝牙iBeacn功能详解

thrift

Fast realization of Bluetooth communication between MCU and mobile phone

主动学习(active learning)

第 299 场周赛 第四题 6103. 从树中删除边的最小分数

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

面试官:用分库分表如何做到永不迁移数据和避免热点问题?

Assembly language - Wang Shuang Chapter 9 Principles of transfer instructions - Notes
随机推荐
卷积神经网络---CNN模型的应用(找矿预测)
Spark sql 常用时间函数
[QT] use structure data to generate read / write configuration file code
可扩展哈希
面试官:请你介绍一下缓存穿透、缓存空值、缓存雪崩、缓存击穿的,通俗易懂
TiDB 中的视图功能
一线大厂面试官问:你真的懂电商订单开发吗?
2018年数学建模竞赛-高温作业专用服装设计
TiDB的事务概览
Easyexcel: read Excel data into the list set
写一个 goroutine 实例, 同时练习一下 chan
云服务器配置ftp、企业官网、数据库等方法
Us camera cloud service scheme: designed for lightweight video production scenes
Unrecognized VM option ‘‘
【OpenAirInterface5g】RRC NR解析之RrcSetupComplete
Caldera installation and simple use
Scala之偏函数Partial Function
SQL injection bypass (I)
Altium designer 19 device silk screen label position shall be placed uniformly in batches
Tidb database Quick Start Guide