当前位置:网站首页>快速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);
}
}
边栏推荐
- Can Oracle's CTAs bring constraints and other attributes to the new table?
- 100 important knowledge points that SQL must master: retrieving data
- AI 绘画极简教程
- SQL必需掌握的100个重要知识点:创建计算字段
- Oracle的CTAS能不能将约束等属性带到新表?
- Full record of 2022 open source moment at Huawei partners and Developers Conference
- 猜拳游戏专题训练
- Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)
- Focus! Tips for installing fonts on domestic computers
- MySQL Express - day 1 - basic introduction
猜你喜欢

Animal breeding production virtual simulation teaching system | Sinovel interactive

AI painting minimalist tutorial

抖音的兴趣电商已经碰到流量天花板?

KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"

JPA踩坑系列之save方法

关于异常处理的知识整理

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

Go从入门到实战—— 多路选择和超时控制(笔记)

Codeforces Round #716 (Div. 2)

快递e栈——数组篇小型项目
随机推荐
Share an experience of self positioning + problem solving
100 important knowledge points for SQL: in operator
流程控制任务
Love math experiment | Issue 8 - building of Singapore house price prediction model
AI painting minimalist tutorial
Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
VMware vSphere ESXi 7.0安装教程
Go从入门到实战——Context与任务取消(笔记)
SQL必需掌握的100个重要知识点:组合 WHERE 子句
01-Golang-环境搭建
创建对象时JVM内存结构
Flexible IP network test tool -- x-launch
ARCS模型介绍
Shell command used in actual work - sed
Codeforces Round #723 (Div. 2)
Acwing周赛57-最长连续子序列-(二分or树状数组)
通过CE修改器修改大型网络游戏
Go from introduction to practice - Interface (notes)
uniapp拦截请求
请教CMS小程序首页的幻灯片在哪里设置?