当前位置:网站首页>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();
}
}边栏推荐
- Digital twin solutions inject new momentum into the construction of chemical parks
- Scope, execution process and life cycle of bean
- 使用navicat或plsql导出csv格式,超过15位数字后面变成000(E+19)的问题
- Edge calculation kubeedge+edgemash
- 【CVPR2022】On the Integration of Self-Attention and Convolution
- The most detailed installation of windows10 virtual machine, install virtual machine by hand, and solve the problem that the Hyper-V option cannot be found in the home version window
- SimpleDateFormat线程不安全和DateTimeFormatter线程安全
- 【ARXIV2205】EdgeViTs: Competing Light-weight CNNs on Mobile Devices with Vision Transformers
- Database date types are all 0
- Antd setfieldsvalue warning problem cannot use 'setfieldsvalue' until you use 'getfielddecorator' or
猜你喜欢

多线程进阶:volatile的作用以及实现原理

Mysql基本查询

Struct模块到底有多实用?一个知识点立马学习

IDEA配置 service(Run Dashboard) 服务,多模块同时启动

【CVPR2022】On the Integration of Self-Attention and Convolution

类和对象【中】

ByteBuffer. Position throws exception illegalargumentexception

11.< tag-动态规划和子序列, 子数组>lt.115. 不同的子序列 + lt. 583. 两个字符串的删除操作 dbc

【CVPR2022】Multi-Scale High-Resolution Vision Transformer for Semantic Segmentation

Video twins: the starting point of informatization upgrading of smart Parks
随机推荐
关于swagger中的localDateTime
First acquaintance with C language (1)
How does Alibaba use DDD to split microservices?
FusionGAN代码学习(一)
【SLAM】LVI-SAM解析——综述
多模块打包:程序包:xxx不存在
BeanUtils. Copyproperties cannot copy different list sets problem solving lists.transform function
注册中心服务eureka 切换到 nocas遇到的问题
I interviewed a 38 year old programmer and refused to work overtime
List < long >, list < integer > convert each other
【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation
C language characters and strings
New methods and features of ES6 built-in objects
About localdatetime in swagger
7.<tag-字符串和API的取舍>补充: 剑指 Offer 05. 替换空格
docker 部署 mysql5.7.35
Personal summary of restful interface use
JUC笔记
Mabtis(一)框架的基本使用
2021CSDN博客之星评选,互投