当前位置:网站首页>Export excel, generate multiple sheet pages, and name them
Export excel, generate multiple sheet pages, and name them
2022-07-28 05:29:00 【wang0112233】
demand :
Export list data for analysis , And according to different dimensions, it is displayed in different sheet in
because hutool Provided sheet Ambiguous methods multiple sheet But the data is only in one page, so you can package one by yourself utils
hutool:Hutool Reference documents
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.shands.mod.quality.service.v2.SheetDTO;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
/**
* @ClassName HuExcelUtils
* @Description TODO
* @Author feng
* @Date 2022/6/1 15:32
* @Version 1.0
*/
public class HuExcelUtils {
/**
* Export multiple Sheet page
*
* @param response
* @param sheetList Page data
* @param fileName file name
*/
public static void exportExcel(HttpServletResponse response, List<SheetDTO> sheetList,
String fileName) {
ExcelWriter bigWriter = ExcelUtil.getBigWriter();
// Rename... First Sheet The name of , Otherwise, there will be one more by default Sheet1 Page of
bigWriter.renameSheet(0, sheetList.get(0).getSheetName());
for (SheetDTO sheet : sheetList) {
// Specify what to write Sheet page
bigWriter.setSheet(sheet.getSheetName());
Integer[] columnWidth = sheet.getColumnWidth();
if (columnWidth == null || columnWidth.length != sheet.getFieldAndAlias().size()) {
// Set the default width
for (int i = 0; i < sheet.getFieldAndAlias().size(); i++) {
bigWriter.setColumnWidth(i, 25);
}
} else {
// Set custom width
for (int i = 0; i < columnWidth.length; i++) {
bigWriter.setColumnWidth(i, columnWidth[i]);
}
}
// Set fields and aliases
bigWriter.setHeaderAlias(sheet.getFieldAndAlias());
// Set to export only fields with aliases
bigWriter.setOnlyAlias(true);
// Set the default row height
bigWriter.setDefaultRowHeight(18);
// Set freeze line
//bigWriter.setFreezePane(1);
// Write it all at once , Use the default style , Forced output title
bigWriter.write(sheet.getCollection(), true);
// Set all columns to automatic width , Do not consider merging cells
// bigWriter.autoSizeColumnAll();
}
ServletOutputStream out = null;
try {
//response by HttpServletResponse object
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition",
"attachment;filename=" +
URLEncoder.encode(fileName + DateUtil.today() + ".xlsx", "UTF-8"));
out = response.getOutputStream();
bigWriter.flush(out, true);
} catch (IOException e) {
e.printStackTrace();
} finally {
// close writer, Free memory
bigWriter.close();
}
// Remember to turn off the output here Servlet flow
IoUtil.close(out);
}
}Controller
@PostMapping("/export")
@ApiOperation(" export ")
public ResultVO export(HttpServletResponse response,@RequestBody Map<String,Integer> map) {
log.info(" The report )- Request parameters :{}", JSONObject.toJSONString(map));
patrolReportService.exportBlocPreviewReport(response,map.get("checkId"));
return ResultVO.success();
}sericeImpl
@Override
public void export(HttpServletResponse response, Integer checkId) {
List<PatrolTopicVo> patrolTopicVo = qtCheckDetailsMapper.selecteachTopic(checkId);
AtomicInteger index = new AtomicInteger();
List<SheetDTO> sheetList = new ArrayList<>(2);
Integer size = 2;
for (int a = 0; a < size; a++) {
if (a == 0) {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
// Set to export only fields with aliases
map.put("id", " Serial number ");
map.put("hotelCode", " Hotel code ");
map.put("hotelName", " Hotel name ");
map.put("cooperationName", " It belongs to the business department ");
map.put("ownershipName", " Management style ");
map.put("taskStartTime", " Mission time ");
map.put("checkPeople", " Inspector ");
map.put("passRate", " Inspection qualification rate ");
map.put("totalScore", " Report score ");
SheetDTO sheet = new SheetDTO();
sheet.setFieldAndAlias(map);
sheet.setCollection(patrolTopicVo);
sheet.setSheetName(" Overall situation ");
sheetList.add(sheet);
index.getAndIncrement();
}
if (a == 1) {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
// Set to export only fields with aliases
map.put("id", " Serial number ");
map.put("departmentName", " department ");
map.put("dimensionName", " dimension ");
map.put("areaName", " Area ");
map.put("projectName", " subject ");
map.put("className", " classification ");
map.put("qualifiedSize", " Number of qualified questions ");
map.put("failedSize", " Number of unqualified questions ");
map.put("notApplication", " Number of inapplicable topics ");
map.put("passRate", " Pass rate ");
SheetDTO sheet = new SheetDTO();
sheet.setFieldAndAlias(map);
sheet.setCollection(patrolTopicVo);
sheet.setSheetName(" Item details ");
sheetList.add(sheet);
index.getAndIncrement();
}
}
try {
HuExcelUtils.exportExcel(response, sheetList, " Detail style ");
} catch (Exception e) {
e.printStackTrace();
}
}边栏推荐
- Database date types are all 0
- Eccv2022 | 29 papers of Tencent Youtu were selected, including face security, image segmentation, target detection and other research directions
- 数据库日期类型全部为0
- 2021csdn blog star selection, mutual investment
- First acquaintance with C language (1)
- 【CVPR2022】On the Integration of Self-Attention and Convolution
- There is no crossover in the time period within 24 hours
- Google browser cannot open localhost:3000. If you open localhost, you will jump to the test address
- New arrow function in ES6
- 导出excel,生成多个sheet页,并命名
猜你喜欢

FusionGAN代码学习(一)

Mysql基本查询

repackag failed: Unable to find main class

【ARXIV2204】Simple Baselines for Image Restoration

How practical is the struct module? Learn a knowledge point immediately

Digital twin solutions inject new momentum into the construction of chemical parks

Redis 之布隆过滤器
![[slam] lvi-sam analysis - Overview](/img/66/f15f6f574807b9a783e9321c71d422.png)
[slam] lvi-sam analysis - Overview

个人写的一个文件上传工具网站

集合框架的操作使用
随机推荐
Oracle用sql查询某张表的字段信息(字段类型、长度等)
子父线程交互
JMeter related knowledge sorting
在ruoyi生成的对应数据库的代码 之后我该怎么做才能做出下边图片的样子
Struct模块到底有多实用?一个知识点立马学习
Long和Integer如何进行比较,为什么报错
分享几种管理C程序中标志位的方法
regular expression
MySQL date and time function, varchar and date are mutually converted
Interpreting the source code of cfrunloopref
框架一步一步方便使用的流程
Response<T>类
repackag failed: Unable to find main class
Antd setfieldsvalue warning problem cannot use 'setfieldsvalue' until you use 'getfielddecorator' or
ByteBuffer.position 抛出异常 IllegalArgumentException
多线程进阶:锁的策略
[slam] lvi-sam analysis - Overview
【MySQL】MySQL时区问题、数据库时间相差8小时问题解决
How to successfully test php7.1 connecting to sqlserver2008r2
[internal mental skill] - creation and destruction of function stack frame (C implementation)