当前位置:网站首页>根据自定义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);
}
}
边栏推荐
- Shell script controls the startup and shutdown of services - with detailed cases
- GoLand永久激活
- Oracle的CTAS能不能将约束等属性带到新表?
- Go from entry to practice - multiple selection and timeout control (notes)
- Full record of 2022 open source moment at Huawei partners and Developers Conference
- Acwing周赛57-数字操作-(思维+分解质因数)
- 系统自带的karsonzhang/fastadmin-addons报错
- squid代理服務器
- Codeforces Round #716 (Div. 2)
- Love number experiment | Issue 7 - Financial Crisis Analysis Based on random forest
猜你喜欢

白嫖红队goby&POC,叫你如何白嫖?
![Unleash the innovative power of open source database | [Gansu] opengauss meetup has come to a successful conclusion](/img/21/9c5f5122270adea9444ff5f2d199ed.jpg)
Unleash the innovative power of open source database | [Gansu] opengauss meetup has come to a successful conclusion

SQL必需掌握的100个重要知识点:使用函数处理数据

Go从入门到实战——行为的定义和实现(笔记)

CORBA 架构体系指南(通用对象请求代理体系架构)

Flood fighting and disaster relief, overcoming difficulties, and City United premium products rushed to the aid of Yingde to donate loving materials

DO280OpenShift访问控制--security policy和章节实验

Show the comprehensive strength of strong products, and make the first show of 2022 Lincoln aviator in Southwest China

SQL必需掌握的100个重要知识点:过滤数据

Go从入门到实战——package(笔记)
随机推荐
Codeforces Round #717 (Div. 2)
Go從入門到實戰——接口(筆記)
Let Ma Huateng down! Web3.0, hopeless
100 important knowledge points that SQL must master: retrieving data
Prospects for enterprise digitalization (38/100)
Navicat premium connection problem --- host 'XXXXXXXX' is not allowed to connect to this MySQL server
覆盖接入2w+交通监测设备,EMQ 为深圳市打造交通全要素数字化新引擎
100 important knowledge points for SQL: in operator
AI 绘画极简教程
图解基于AQS队列实现的CountDownLatch和CyclicBarrier
Very comprehensive dolphin scheduler installation and use documents
Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)
SQL server for circular usage
How to participate in openharmony code contribution
Codeforces Round #723 (Div. 2)
Go从入门到实战——仅需任意任务完成(笔记)
100 important knowledge points that SQL must master: creating calculation fields
DO280OpenShift访问控制--security policy和章节实验
Galaxy Kirin system LAN file sharing tutorial
Go从入门到实战—— 多路选择和超时控制(笔记)