当前位置:网站首页>根据自定义excel标题模板快速excel导出
根据自定义excel标题模板快速excel导出
2022-06-27 19:23:00 【高粱】
根据自定义excel标题模板快速excel导出
try (InputStream fis = this.getClass().getResourceAsStream("/templates/模板.xlsx");
XSSFWorkbook book = new XSSFWorkbook(fis);)
{
XSSFSheet sheet = book.getSheetAt(0);
CellStyle style = createStyle(book);
// 构建每行的数据内容
int rowNum = 2;
for (Iterator<Map<String, Object>> it = resultList.iterator(); it.hasNext();)
{
Map<String, Object> data = it.next();
if (data == null) continue;
// 输出行数据
Row row = sheet.createRow(rowNum++);
convertDataToRow(data, row, style);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
book.write(out);
DownloadUtils.downLoadFile("excel.xlsx", response, out.toByteArray());
}
/** * 样式居中 */
private CellStyle createStyle(XSSFWorkbook book)
{
CellStyle cellStyle = book.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
return cellStyle;
}
/** * 填充数据 */
private void convertDataToRow(Map<String, Object> data, Row row, CellStyle style)
{
int cellNum = 0;
Cell cell;
//
cell = row.createCell(cellNum++);
cell.setCellStyle(style);
cell.setCellValue(null == data.get("username") ? "" : CommonFunc.parseStr(data.get("username")));
//
cell = row.createCell(cellNum++);
cell.setCellStyle(style);
cell.setCellValue(null == data.get("age") ? "" : CommonFunc.parseStr(data.get("age")));
}
public class DownloadUtils {
/** * 下载文件 * @param fileName * @param response * @param data * @throws IOException */
public static void downLoadFile(String fileName, HttpServletResponse response, byte[] data) throws IOException {
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.getOutputStream().write(data);
}
}
边栏推荐
- GoLand permanently activated
- Shell script controls the startup and shutdown of services - with detailed cases
- 如何将队列里面的内容没秒钟执行一次优先级
- Tutorial | fNIRS data processing toolkit homer2 download and installation
- 互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?
- 数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
- Save method of JPA stepping pit series
- 小王的面试训练任务
- SQL必需掌握的100个重要知识点:用通配符进行过滤
- 01-Golang-环境搭建
猜你喜欢
划重点!国产电脑上安装字体小技巧
MYSQL 性能优化 index 函数,隐藏,前缀,hash 索引 使用方法(2)
squid代理服務器
BTC and eth recapture the lost land! Leading the market recovery? Encryption will enter the "ice age"!
VMware vSphere esxi 7.0 installation tutorial
覆盖接入2w+交通监测设备,EMQ 为深圳市打造交通全要素数字化新引擎
AI painting minimalist tutorial
Go from entry to practice - dependency management (notes)
Go从入门到实战——行为的定义和实现(笔记)
Go从入门到实战——Context与任务取消(笔记)
随机推荐
Go从入门到实战——依赖管理(笔记)
Codeforces Round #723 (Div. 2)
覆盖接入2w+交通监测设备,EMQ 为深圳市打造交通全要素数字化新引擎
空指针异常
Let Ma Huateng down! Web3.0, hopeless
Experiment of love number lesson | phase V - emotion judgment of commodity review based on machine learning method
Original translation | comparison of machine learning model service tools: kserve, Seldon core and bentoml
A set of system to reduce 10 times the traffic pressure in crowded areas
100 important knowledge points for SQL: in operator
互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?
String类的常用方法
Go从入门到实战——协程机制(笔记)
Love number experiment | Issue 7 - Financial Crisis Analysis Based on random forest
数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
Ceph分布式存储
Share an experience of self positioning + problem solving
非常全面的DolphinScheduler(海豚调度)安装使用文档
GFS distributed file system
Shell script controls the startup and shutdown of services - with detailed cases
Icml2022 | scalable depth Gaussian Markov random field