当前位置:网站首页>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();
}
}边栏推荐
- 凛冬已至,程序员该怎么取暖
- From the basic concept of micro services to core components - explain and analyze through an example
- MySQL date and time function, varchar and date are mutually converted
- Implementation of simple upload function in PHP development
- lamda 获取当前循环数,AtomicInteger
- You must configure either the server or JDBC driver (via the ‘serverTimezone)
- 数据库面试
- 21 day SQL punch in summary
- repackag failed: Unable to find main class
- docker 部署 mysql5.7.35
猜你喜欢

Framework step by step easy-to-use process

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

FreeRTOS personal notes - task notification

多模块打包:程序包:xxx不存在

ES6 new variable modifiers let and const, new basic data type symbol
Printf function of input and output function in C language

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

You must configure either the server or JDBC driver (via the ‘serverTimezone)

Tomato timing dimming table lamp touch chip-dlt8t10s-jericho

【ARIXV2204】Neighborhood attention transformer
随机推荐
项目中问题合集
Class class added in ES6
DELL远程控制卡 使用ipmitools设置ipmi
SimpleDateFormat线程不安全和DateTimeFormatter线程安全
Digital twin technology creates visual application of smart mine
regular expression
Dell remote control card uses ipmitools to set IPMI
mybaties foreach多选查询,index循环,取消and/or标签
Using RAC to realize the sending logic of verification code
sql 查询list时两次的数据不一致,自动加上了limit
关于swagger中的localDateTime
多系统架构设计思考
Framework step by step easy-to-use process
类和对象【中】
oracle查看锁表语句、解锁方法
【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation
JUC笔记
Google browser cannot open localhost:3000. If you open localhost, you will jump to the test address
Test Development - UI testing in automated testing
解决Oracle使用in语句不能超过1000问题