POI版本
org.apache.poi:poi:3.17
org.apache.poi:poi-ooxml:3.17
样式定制
public static Map<String, CellStyle> createStyles(Workbook wb)
{
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
Font dataFont = wb.createFont();
dataFont.setFontName("Arial");
dataFont.setFontHeightInPoints((short) 10);
style.setFont(dataFont);
styles.put("data", style);
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"));
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = wb.createFont();
headerFont.setFontName("Arial");
headerFont.setFontHeightInPoints((short) 10);
headerFont.setBold(true);
headerFont.setColor(IndexedColors.WHITE.getIndex());
style.setFont(headerFont);
styles.put("header", style);
return styles;
}
列宽自适应
针对XSSFSheet
for (int i = 0; i < columnLen; i++) {
sheet.autoSizeColumn(i);
sheet.setColumnWidth(i,sheet.getColumnWidth(i)*17/10);
}
针对大数据量的SXSSFSheet
sheet.trackAllColumnsForAutoSizing();
for (int i = 0; i < columnLen; i++) {
s.autoSizeColumn(i);
int width = titleColWidth.get(i);
int max = Math.max(sheet.getColumnWidth(i) * 17 / 10, width);
s.setColumnWidth(i, Math.min(max, 255 * 256));
}
完整代码
package com.incar.base.util;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
public class ExcelUtils {
private static void inputValue(Cell cell, Object val){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
if(val==null){
cell.setCellValue("");
return;
}
Class clazz= val.getClass();
if(String.class.isAssignableFrom(clazz)){
cell.setCellValue((String)val);
}else if(Double.class.isAssignableFrom(clazz)){
cell.setCellValue((Double)val);
}else if(Date.class.isAssignableFrom(clazz)){
String formatDate = simpleDateFormat.format(((Date) val));
cell.setCellValue(formatDate);
}else if(Boolean.class.isAssignableFrom(clazz)){
cell.setCellValue((Boolean)val);
}else if(Calendar.class.isAssignableFrom(clazz)){
String formatDate = simpleDateFormat.format(((Calendar) val).getTime());
cell.setCellValue(formatDate);
}else if(RichTextString.class.isAssignableFrom(clazz)){
cell.setCellValue((RichTextString)val);
}else if(Float.class.isAssignableFrom(clazz)){
DecimalFormat format = new DecimalFormat("#0.000") ;
cell.setCellValue(format.format(val));
}else if(Byte.class.isAssignableFrom(clazz)){
cell.setCellValue((Byte)val);
}else if(Short.class.isAssignableFrom(clazz)){
cell.setCellValue((Short)val);
}else if(Integer.class.isAssignableFrom(clazz)){
cell.setCellValue((Integer)val);
}else if(Long.class.isAssignableFrom(clazz)){
cell.setCellValue(val.toString());
}else if(BigDecimal.class.isAssignableFrom(clazz)){
cell.setCellValue(val.toString());
}
}
public static Workbook exportExcel(List<List> dataList){
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet();
Map<String, CellStyle> styles = createStyles(workBook);
exportExcel(sheet,dataList,styles);
return workBook;
}
public static Workbook exportBigExcel(List<List> dataList){
SXSSFWorkbook workBook = new SXSSFWorkbook(1000);
SXSSFSheet sheet = workBook.createSheet();
int columnLen = 0;
Map<String, CellStyle> styles = createStyles(workBook);
Map<Integer, Integer> titleColWidth = new HashMap<>();
for(int i=0;i<=dataList.size()-1;i++){
SXSSFRow curRow = sheet.createRow(i);
List innerDataList= dataList.get(i);
columnLen = innerDataList.size();
for(int j=0;j<=innerDataList.size()-1;j++){
SXSSFCell curCell= curRow.createCell(j);
inputValue(curCell, innerDataList.get(j));
if(i == 0) {
curCell.setCellStyle(styles.get("header"));
titleColWidth.put(j, val.toString().length() * 3 * 17 / 10 * 256);
} else {
curCell.setCellStyle(styles.get("data"));
}
}
}
sheet.trackAllColumnsForAutoSizing();
for (int i = 0; i < columnLen; i++) {
sheet.autoSizeColumn(i);
int width = titleColWidth.get(i);
int max = Math.max(sheet.getColumnWidth(i) * 17 / 10, width);
sheet.setColumnWidth(i, Math.min(max, 255 * 256));
}
return workBook;
}
public static Map<String, CellStyle> createStyles(Workbook wb)
{
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
Font dataFont = wb.createFont();
dataFont.setFontName("Arial");
dataFont.setFontHeightInPoints((short) 10);
style.setFont(dataFont);
styles.put("data", style);
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"));
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = wb.createFont();
headerFont.setFontName("Arial");
headerFont.setFontHeightInPoints((short) 10);
headerFont.setBold(true);
headerFont.setColor(IndexedColors.WHITE.getIndex());
style.setFont(headerFont);
styles.put("header", style);
return styles;
}
public static void exportExcel(XSSFSheet sheet,List<List> dataList,Map<String,CellStyle> cellStyle){
int columnLen = 0;
for(int i=0;i<=dataList.size()-1;i++){
XSSFRow curRow = sheet.createRow(i);
List innerDataList= dataList.get(i);
columnLen = innerDataList.size();
for(int j=0;j<=innerDataList.size()-1;j++){
Object o = innerDataList.get(j);
XSSFCell curCell= curRow.createCell(j);
if (cellStyle != null) {
if(i == 0) {
curCell.setCellStyle(cellStyle.get("header"));
} else {
curCell.setCellStyle(cellStyle.get("data"));
}
}
inputValue(curCell, o);
}
}
for (int i = 0; i < columnLen; i++) {
sheet.autoSizeColumn(i);
sheet.setColumnWidth(i,sheet.getColumnWidth(i)*17/10);
}
}
}
效果
