当前位置:网站首页>Quick excel export according to customized excel Title Template
Quick excel export according to customized excel Title Template
2022-06-27 21:45:00 【sorghum】
According to custom excel Title Template quick excel export
try (InputStream fis = this.getClass().getResourceAsStream("/templates/ Templates .xlsx");
XSSFWorkbook book = new XSSFWorkbook(fis);)
{
XSSFSheet sheet = book.getSheetAt(0);
CellStyle style = createStyle(book);
// Build the data content of each row
int rowNum = 2;
for (Iterator<Map<String, Object>> it = resultList.iterator(); it.hasNext();)
{
Map<String, Object> data = it.next();
if (data == null) continue;
// Output row data
Row row = sheet.createRow(rowNum++);
convertDataToRow(data, row, style);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
book.write(out);
DownloadUtils.downLoadFile("excel.xlsx", response, out.toByteArray());
}
/** * Style centered */
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;
}
/** * Fill in the data */
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 {
/** * Download the file * @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);
}
}
边栏推荐
- GFS distributed file system
- 根据自定义excel标题模板快速excel导出
- Go从入门到实战——协程机制(笔记)
- excel读取文件内容方法
- Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen
- 猜拳游戏专题训练
- 数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
- GoLand permanently activated
- TreeSet details
- Save method of JPA stepping pit series
猜你喜欢
随机推荐
Go从入门到实战——接口(笔记)
win11桌面出現“了解此圖片”如何删除
Acwing周赛57-数字操作-(思维+分解质因数)
Can Oracle's CTAs bring constraints and other attributes to the new table?
集合代码练习
MySQL performance optimization index function, hidden, prefix, hash index usage (2)
互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?
TypeScript学习
Go from entry to practice - multiple selection and timeout control (notes)
Codeforces Round #719 (Div. 3)
Go從入門到實戰——接口(筆記)
图解基于AQS队列实现的CountDownLatch和CyclicBarrier
Go从入门到实战——Panic和recover(笔记)
Go from introduction to practice - polymorphism (note)
Tiktok's interest in e-commerce has hit the traffic ceiling?
Go从入门到实战——协程机制(笔记)
What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?
oss上传调用的是哪个方法
空指针异常
Go from starting to Real - Interface (note)









