当前位置:网站首页>快速excel导出
快速excel导出
2022-06-27 19:23:00 【高粱】
快速excel导出
编写了一个工具类 为了防止多线程操作poi存threadLocal
/** * 调用方法 */
data = POIUtils4Obj.getSingleSheetData(resultList,Arrays.asList("用户名", "年龄"),Arrays.asList("username", "age");
package xxx;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class POIUtils4Obj {
private static ThreadLocal<Workbook> threadLocal = new ThreadLocal<Workbook>();
/** * 组装数据返回导出流 * @param contents * @param titles * @param containValuesOrderly * @param sheetName * @return * @throws Exception */
public static byte[] getSingleSheetData(List<Map<String, Object>> contents,List<String> titles, List<String> containValuesOrderly,String sheetName) throws IOException{
Workbook workBook = createWorkBook();
threadLocal.set(workBook);
Sheet sheet = createSheet(workBook, sheetName);
CellStyle cellStyle = createCellStyle();
cellStyle.setBorderBottom(BorderStyle.THICK);
cellStyle.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
cellStyle.setBorderTop(BorderStyle.THICK);
cellStyle.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
cellStyle.setBorderLeft(BorderStyle.THICK);
cellStyle.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
cellStyle.setBorderRight(BorderStyle.THICK);
cellStyle.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
setTitle(sheet, 0, titles, cellStyle);
setContent(sheet, 1, contents, containValuesOrderly);
ByteArrayOutputStream out = new ByteArrayOutputStream();
workBook.write(out);
workBook.close();
return out.toByteArray();
}
/** * 设置title的样式 * @param sheet * @param i * @param titles * @param cellStyle */
private static void setTitleCustom(Sheet sheet, int i, List<String> titles,CellStyle cellStyle) {
Row titleRow = createRow(sheet, i);
Cell cell0 = titleRow.createCell(0);
cell0.setCellStyle(cellStyle);
cell0.setCellValue(titles.get(0));
Cell cell1 = titleRow.createCell(1);
cell1.setCellStyle(cellStyle);
cell1.setCellValue(titles.get(1));
Cell cell4 = titleRow.createCell(4);
cell4.setCellStyle(cellStyle);
cell4.setCellValue(titles.get(2));
}
/** * 创建单元格并设置样式 * @return */
private static CellStyle createCellStyle() {
CellStyle cellStyle = threadLocal.get().createCellStyle();
// threadLocal.remove();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
return cellStyle;
}
/** * 设置内容及样式 * @param sheet * @param rowStart * @param contents * @param containValuesOrderly */
private static void setContent(Sheet sheet,int rowStart,List<Map<String, Object>> contents,List<String> containValuesOrderly) {
int length = contents.size();
for (int i = 0; i < length; i++) {
Row row = createRow(sheet,rowStart++);
setRow(row, contents.get(i), containValuesOrderly);
}
}
/** * 设置行内容样式 * @param row * @param map * @param containValuesOrderly */
private static void setRow(Row row, Map<String, Object> map, List<String> containValuesOrderly) {
int i = 0;
CellStyle cellStyle = createCellStyle();
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());
for (String key : containValuesOrderly) {
Cell cell = createStringCell(row, i++);
String value = map.get(key) == null ? "" : String.valueOf(map.get(key));
cell.setCellStyle(cellStyle);
cell.setCellValue(value);
}
}
/** * 判断是否是数字类型,包括小数,整数 * */
private static boolean isNumber(String string){
Pattern pattern = Pattern.compile("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
Matcher isNum = pattern.matcher(string);
return isNum.matches();
}
/** * 设置title内容及样式 * @param sheet * @param titlenum * @param titles * @param cellStyle */
private static void setTitle(Sheet sheet,int titlenum,List<String> titles,CellStyle cellStyle) {
int length = titles.size();
// 设置列宽
for (int i = 0; i < length; i++) {
sheet.setColumnWidth(i, titles.get(i).getBytes().length*256);
}
Row titleRow = createRow(sheet, titlenum);
for (int i = 0; i < length; i++) {
Cell title = createStringCell(titleRow, i);
title.setCellStyle(cellStyle);
title.setCellValue(titles.get(i));
}
}
/** * 创建 String 格式的 cell * @param row * @param column * @return */
private static Cell createStringCell(Row row,int column) {
return row.createCell(column, CellType.STRING);
}
/** * 创建 Row * @param sheet * @param rownum * @return */
private static Row createRow(Sheet sheet, int rownum) {
return sheet.createRow(rownum);
}
/** * 创建 workBook * @return * @throws IOException */
private static Workbook createWorkBook() throws IOException {
Workbook workBook = new XSSFWorkbook();
return workBook;
}
/** * 创建 sheet 页 * @param workbook * @param sheetName * @return */
private static Sheet createSheet(Workbook workbook,String sheetName) {
return workbook.createSheet(sheetName);
}
}
边栏推荐
- Tutorial | fNIRS data processing toolkit homer2 download and installation
- Flexible IP network test tool -- x-launch
- Unleash the innovative power of open source database | [Gansu] opengauss meetup has come to a successful conclusion
- 农产品期货怎么做怎么开户,期货开户手续费多少,找谁能优惠手续费?
- Codeforces Round #721 (Div. 2)
- Codeforces Round #723 (Div. 2)
- Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen
- Go从入门到实战——接口(笔记)
- Experiment of love number lesson | phase V - emotion judgment of commodity review based on machine learning method
- SQL必需掌握的100个重要知识点:排序检索数据
猜你喜欢

快递e栈——数组篇小型项目

Flexible IP network test tool -- x-launch

GoLand永久激活

MySQL performance optimization index function, hidden, prefix, hash index usage (2)

让马化腾失望了!Web3.0,毫无希望

White whoring red team goby & POC, how do you call white whoring?

Can Oracle's CTAs bring constraints and other attributes to the new table?

Go from introduction to practice - Interface (notes)

强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”

银河麒麟系统局域网文件共享教程
随机推荐
让马化腾失望了!Web3.0,毫无希望
Love math experiment | phase 9 - intelligent health diagnosis using machine learning method
非常全面的DolphinScheduler(海豚调度)安装使用文档
Squid proxy server
Go from entry to practice - dependency management (notes)
抖音的兴趣电商已经碰到流量天花板?
Tutorial | fNIRS data processing toolkit homer2 download and installation
SQL必需掌握的100个重要知识点:组合 WHERE 子句
Can Oracle's CTAs bring constraints and other attributes to the new table?
互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?
SQL必需掌握的100个重要知识点:检索数据
String类的常用方法
Ceph分布式存储
安装gatewayworker之后启动start.php
Modify large online games through CE modifier
富文本 考试 填空题
请教CMS小程序首页的幻灯片在哪里设置?
划重点!国产电脑上安装字体小技巧
Go从入门到实战——仅需任意任务完成(笔记)
Focus! Tips for installing fonts on domestic computers