当前位置:网站首页>快速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);
}
}
边栏推荐
- Cortical traceability analysis of ERP
- Love math experiment | phase 9 - intelligent health diagnosis using machine learning method
- 于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
- Share how I take notes
- Go从入门到实战——多态(笔记)
- Love math experiment | phase VI - Financial anti fraud case study
- How to participate in openharmony code contribution
- TypeScript学习
- 集合代码练习
- GoLand permanently activated
猜你喜欢
随机推荐
String类的常用方法
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 Round #721 (Div. 2)
MySQL速成——第一天--基础入门
DO280OpenShift访问控制--security policy和章节实验
Codeforces Round #717 (Div. 2)
OpenSSL 编程 二:搭建 CA
Wechat applet based service management system for college party members' Home System applet graduation design, Party members, activists, learning, punch in, forum
微服务之远程调用
uniapp拦截请求
体验Navicat Premium 16,无限重置试用14天方法(附源码)
Codeforces Round #716 (Div. 2)
VMware vSphere esxi 7.0 installation tutorial
数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
100 important knowledge points that SQL must master: sorting and retrieving data
Go从入门到实战——CSP并发机制(笔记)
Go从入门到实战——依赖管理(笔记)
非常全面的DolphinScheduler(海豚调度)安装使用文档
Cerebral cortex: predicting children's mathematical skills from task state and resting state brain function connections








