使用POI根据传入的标题、数据、数据格式等生产简单的excel文件,有例子和注解,有兴趣的朋友可以看下:
package com.storemanage.util;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 简单excel生成工具(使用POI)
* Created by HanQiankun([email protected]) on 2018/03/22.
*/
public class SimpleExcelUtil {
/** 默认sheet名称 */
private static final String DEFAULT_SHEET_NAME = "sheet";
/** 单个Sheet页最大行数 */
private static final int SINGLE_SHEET_MAX_ROWS = 65535;
/** 默认单元格宽度 */
private static final int DEFAULT_CELL_WIDTH = 5000;
/** 默认日期格式 */
private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
/** 默认日期格式 */
private static final ThreadLocal DEFAULT_DATE_FORMAT = new ThreadLocal(){
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(DEFAULT_DATE_PATTERN);
}
};
/**
* 根据列标题和列数据生成excel表格文件
* excel默认一个sheet且sheet名称默认为sheet1
* 日期格式:yyyy-MM-dd HH:mm:ss
* list格式:XXX,XXXX,XXXX
* map格式:a:1,b:2
*
* @param columnTitles
* 列标题
* @param datas
* 列数据,只支持对象的属性使用string、date、Number数字类型和list、map,有需要请扩展
* @param columnFields
* 列标题对应对象的属性名(依次与列标题对应)
* @return
* @throws Exception
*/
public static byte[] createExcel(List columnTitles, List extends Object> datas, List columnFields) throws Exception {
return createExcel(null, columnTitles, datas, columnFields,null);
}
/**
* 根据列标题和列数据生成excel表格文件,生成excel默认一个sheet且sheet名称默认为sheet1
* 日期格式:yyyy-MM-dd HH:mm:ss
* list格式:XXX,XXXX,XXXX
* map格式:a:1,b:2
*
* @param columnTitles
* 列标题
* @param datas
* 列数据,只支持对象的属性使用string、date、Number数字类型和list、map,有需要请扩展
* @param columnFields
* 列标题对应对象的属性名(依次与列标题对应)
* @param customFieldFormats
* 每列数据自定义format,没有为空,list类型属性:格式化其子元素,map类型属性: 格式化其子元素的value
* @return
* @throws Exception
*/
public static byte[] createExcel( List columnTitles, List extends Object> datas, List columnFields,
Map customFieldFormats) throws Exception {
return createExcel(null, columnTitles, datas, columnFields, customFieldFormats);
}
/**
* 根据列标题和列数据生成excel表格文件
* excel默认一个sheet且sheet名称默认为sheet1
* 日期格式:yyyy-MM-dd HH:mm:ss
* list格式:XXX,XXXX,XXXX
* map格式:a:1,b:2
*
* @param fileName
* 文件名称
* @param columnTitles
* 列标题
* @param datas
* 列数据,只支持对象的属性使用string、date、Number数字类型和list、map,有需要请扩展
* @param columnFields
* 列标题对应对象的属性名(依次与列标题对应)
* @return
* @throws Exception
*/
public static File createExcel(String fileName, List columnTitles, List extends Object> datas, List columnFields) throws Exception {
byte[] fileBytes = createExcel(null, columnTitles, datas, columnFields,null);
File file = createTmpFile(fileName);
FileUtils.writeByteArrayToFile(file, fileBytes);
return file;
}
/**
* 根据列标题和列数据生成excel表格文件,excel文件默认一个sheet
* 日期格式:yyyy-MM-dd HH:mm:ss
* list格式:XXX,XXXX,XXXX
* map格式:a:1,b:2
*
* @param sheetName
* 生成的excel文件的sheet名
* @param columnTitles
* 列标题
* @param datas
* 列数据,只支持对象的属性使用string、date、Number数字类型和list、map,有需要请扩展
* @param columnFields
* 列标题对应对象的属性名(依次与列标题对应)
* @param customFieldFormats
* 对象属性自定义format,没有为空,list类型属性:格式化其子元素,map类型属性: 格式化其子元素的value
* @return 文件byte
* @throws Exception
*/
public static byte[] createExcel(String sheetName, List columnTitles, List extends Object> datas,
List columnFields, Map customFieldFormats) throws Exception {
if (datas == null || columnTitles == null) {
throw new IllegalArgumentException("illegal data: data or columnTitles is null");
}
if (columnTitles.size() != columnFields.size()) {
throw new IllegalArgumentException("every column title should have its mapped column field name");
}
// File file = null;
HSSFWorkbook workbook = new HSSFWorkbook();
int dataSize = datas.size();
int part = dataSize / SINGLE_SHEET_MAX_ROWS;
if(dataSize > SINGLE_SHEET_MAX_ROWS) {
sheetName = StringUtils.isNotBlank(sheetName) ? sheetName : DEFAULT_SHEET_NAME;
for(int i = 0; i < part; i++) {
String tmpSheetName = String.format("%s%d", sheetName, i + 1);
createSheetAndWriteData(tmpSheetName, columnTitles, datas.subList(0, SINGLE_SHEET_MAX_ROWS), columnFields,
customFieldFormats, workbook);
datas.subList(0, SINGLE_SHEET_MAX_ROWS).clear();
}
if(CollectionUtils.isNotEmpty(datas)) {
sheetName = String.format("%s%d", sheetName, part + 1);
createSheetAndWriteData(sheetName, columnTitles, datas, columnFields, customFieldFormats, workbook);
}
} else {
sheetName = StringUtils.isNotBlank(sheetName) ? sheetName : DEFAULT_SHEET_NAME + "1";
createSheetAndWriteData(sheetName, columnTitles, datas, columnFields,customFieldFormats, workbook);
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.write(os);
return os.toByteArray();
}
private static void createSheetAndWriteData(String sheetName, List columnTitles, List extends Object> datas,
List columnFields, Map customFieldFormats,
HSSFWorkbook workbook) throws Exception {
HSSFSheet sheet = workbook.createSheet(sheetName);
initSheetHeaders(workbook, sheet, columnTitles);
writeData(sheet, datas, columnFields,customFieldFormats);
}
private static void writeData(HSSFSheet sheet, List extends Object> datas, List columnFields,
Map customFieldFormats) throws Exception {
int startX = 1;
int startY = 0;
for (Object item : datas) {
HSSFRow row = sheet.createRow(startX);
for (int i = 0; i < columnFields.size(); i++) {
String fieldName = columnFields.get(i);
Field field = item.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
Format fmt = null;
if(customFieldFormats != null && customFieldFormats.containsKey(fieldName)) {
fmt = customFieldFormats.get(fieldName);
}
HSSFCell cell = row.createCell(startY++);
String columnData = resolveFieldData(field.get(item),fmt);
cell.setCellValue(columnData);
}
startX++;
startY = 0;
}
}
@SuppressWarnings("unchecked")
private static String resolveFieldData(Object orgnColumnData,Format fmt) {
String columnData = "";
if (orgnColumnData == null) {
} else if (orgnColumnData instanceof String) {
columnData = fmt == null ? (String) orgnColumnData : fmt.format((String) orgnColumnData);
} else if (orgnColumnData instanceof Date) {
columnData = fmt ==null ? formatDate((Date) orgnColumnData) : fmt.format((Date) orgnColumnData);
} else if (orgnColumnData instanceof Number) {
columnData = fmt == null ? String.valueOf(orgnColumnData) : fmt.format((Number)orgnColumnData);
} else if (orgnColumnData.getClass().isArray()) {
List
效果如图:
详细源码见:
https://github.com/hzhqk/java/tree/master/poi/simple%20excel