当前位置:网站首页>快速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);
}
}
边栏推荐
猜你喜欢

GoLand permanently activated

划重点!国产电脑上安装字体小技巧

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

Here are 12 commonly used function formulas for you. All used ones are good

华为伙伴暨开发者大会2022开源时刻全纪录

SQL server for circular usage

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

Go从入门到实战——共享内存并发机制(笔记)

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!"

本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
随机推荐
微服务之远程调用
Animal breeding production virtual simulation teaching system | Sinovel interactive
Tiktok's interest in e-commerce has hit the traffic ceiling?
MySQL usage notes 1
Go从入门到实战——package(笔记)
空指针异常
Kirin V10 installation font
体验Navicat Premium 16,无限重置试用14天方法(附源码)
MySQL速成——第一天--基础入门
100 important knowledge points that SQL must master: filtering data
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!"
送你12个常用函数公式,用过的都说好
Love math experiment | phase VI - Financial anti fraud case study
Codeforces Round #722 (Div. 2)
小王的面试训练任务
创建对象时JVM内存结构
CEPH distributed storage
麒麟V10安装字体
Codeforces Round #723 (Div. 2)
MYSQL 性能优化 index 函数,隐藏,前缀,hash 索引 使用方法(2)