当前位置:网站首页>Quick excel export
Quick excel export
2022-06-27 21:44:00 【sorghum】
Fast excel export
Write a tool class To prevent multithreading poi save threadLocal
/** * Calling method */
data = POIUtils4Obj.getSingleSheetData(resultList,Arrays.asList(" user name ", " Age "),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>();
/** * The assembly data is returned to the export stream * @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();
}
/** * Set up title The style of * @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));
}
/** * Create cells and set styles * @return */
private static CellStyle createCellStyle() {
CellStyle cellStyle = threadLocal.get().createCellStyle();
// threadLocal.remove();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
return cellStyle;
}
/** * Set content and style * @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);
}
}
/** * Set line content style * @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);
}
}
/** * Determine whether it is a number type , Including decimals , Integers * */
private static boolean isNumber(String string){
Pattern pattern = Pattern.compile("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
Matcher isNum = pattern.matcher(string);
return isNum.matches();
}
/** * Set up title Content and style * @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();
// Set column width
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));
}
}
/** * establish String Format cell * @param row * @param column * @return */
private static Cell createStringCell(Row row,int column) {
return row.createCell(column, CellType.STRING);
}
/** * establish Row * @param sheet * @param rownum * @return */
private static Row createRow(Sheet sheet, int rownum) {
return sheet.createRow(rownum);
}
/** * establish workBook * @return * @throws IOException */
private static Workbook createWorkBook() throws IOException {
Workbook workBook = new XSSFWorkbook();
return workBook;
}
/** * establish sheet page * @param workbook * @param sheetName * @return */
private static Sheet createSheet(Workbook workbook,String sheetName) {
return workbook.createSheet(sheetName);
}
}
边栏推荐
猜你喜欢
随机推荐
数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
Serveur mandataire SQUID
Goldfish rhca memoirs: do447 managing projects and carrying out operations -- creating job templates and starting jobs
洛谷P5706 再分肥宅水
String类的常用方法
Go从入门到实战——协程机制(笔记)
数组作业题
鲜为人知的mysql导入数据
How to delete "know this picture" on win11 desktop
Common methods of string class
Go从入门到实战——接口(笔记)
快递e栈——数组篇小型项目
专题教程——选队长游戏
The difference between scrum and Kanban
集合代码练习
After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"
Codeforces Global Round 14
关于异常处理的知识整理
Codeforces Round #719 (Div. 3)
MySQL performance optimization index function, hidden, prefix, hash index usage (2)









