在博客园上前前后后一共写了两三版的关于Excel导出的通用方法,这也算是我对于Excel相关操作的一步一步脚印吧。现搬迁到这里来,做统一整理。
第一版
import com.google.common.collect.Maps;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* 将读取到的数据库内容写到Excel模板表中,供下载需要
*/
public class WriterExcelFile {
private static final Logger LOGGER = Logger.getLogger(WriterExcelFile.class.getName());
/**
* 将数据写入指定path下的Excel文件中
* 这里会有一个限制条件:列名的顺序必须和数据的存储顺序一致,否则会造成混乱;这是第一版,以后再改进这个
* @param path 文件存储路径
* @param name sheet名
* @param style Excel类型
* @param titles 标题串
* @param values 内容集
* @return T\F
*/
public static boolean generateWorkbook(String path, String name, String style, List titles, List
据说这一版里,有逻辑错误,我也没怎么看,有道友MrSteven
的解决方案如下:
第一版的向每列写入数据的时候稍微有点问题
改成如下代码:
int i=0;
for (Map.Entry map : value.entrySet()) {
Object object = map.getValue();
content = object.toString();
Cell cell = row.createCell(i);
cell.setCellStyle(styles.get("cell"));
cell.setCellValue(content);
i++;
}
// for (int i = 0; i < value.size(); i++) {
// Cell cell = row.createCell(i);
// cell.setCellStyle(styles.get("cell"));
// cell.setCellValue(content);
// }
如果用的话,请自行验证。
第二版
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;
public class WriterExcelUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(WriterExcelUtil.class.getName());
public static void main(String[] args) {
String path = "E://demo.xlsx";
String name = "test";
List titles =Lists.newArrayList();
titles.add("id");
titles.add("name");
titles.add("age");
titles.add("birthday");
titles.add("gender");
titles.add("date");
List> values = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
Map map = Maps.newHashMap();
map.put("id", i + 1D);
map.put("name", "test_" + i);
map.put("age", i * 1.5);
map.put("gender", "man");
map.put("birthday", new Date());
map.put("date", Calendar.getInstance());
values.add(map);
}
System.out.println(writerExcel(path, name, titles, values));
}
/**
* 数据写入Excel文件
*
* @param path 文件路径,包含文件全名,例如:D://file//demo.xls
* @param name sheet名称
* @param titles 行标题列
* @param values 数据集合,key为标题,value为数据
* @return True\False
*/
public static boolean writerExcel(String path, String name, List titles, List> values) {
LOGGER.info("path : {}", path);
String style = path.substring(path.lastIndexOf("."), path.length()).toUpperCase(); // 从文件路径中获取文件的类型
return generateWorkbook(path, name, style, titles, values);
}
/**
* 将数据写入指定path下的Excel文件中
*
* @param path 文件存储路径
* @param name sheet名
* @param style Excel类型
* @param titles 标题串
* @param values 内容集
* @return True\False
*/
private static boolean generateWorkbook(String path, String name, String style, List titles, List> values) {
LOGGER.info("file style : {}", style);
Workbook workbook;
if ("XLS".equals(style.toUpperCase())) {
workbook = new HSSFWorkbook();
} else {
workbook = new XSSFWorkbook();
}
// 生成一个表格
Sheet sheet;
if (null == name || "".equals(name)) {
sheet = workbook.createSheet(); // name 为空则使用默认值
} else {
sheet = workbook.createSheet(name);
}
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);
// 生成样式
Map styles = createStyles(workbook);
/*
* 创建标题行
*/
Row row = sheet.createRow(0);
// 存储标题在Excel文件中的序号
Map titleOrder = Maps.newHashMap();
for (int i = 0; i < titles.size(); i++) {
Cell cell = row.createCell(i);
cell.setCellStyle(styles.get("header"));
String title = titles.get(i);
cell.setCellValue(title);
titleOrder.put(title, i);
}
/*
* 写入正文
*/
Iterator> iterator = values.iterator();
int index = 0; // 行号
while (iterator.hasNext()) {
index++; // 出去标题行,从第一行开始写
row = sheet.createRow(index);
Map value = iterator.next();
for (Map.Entry map : value.entrySet()) {
// 获取列名
String title = map.getKey();
// 根据列名获取序号
int i = titleOrder.get(title);
// 在指定序号处创建cell
Cell cell = row.createCell(i);
// 设置cell的样式
if (index % 2 == 1) {
cell.setCellStyle(styles.get("cellA"));
} else {
cell.setCellStyle(styles.get("cellB"));
}
// 获取列的值
Object object = map.getValue();
// 判断object的类型
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (object instanceof Double) {
cell.setCellValue((Double) object);
} else if (object instanceof Date) {
String time = simpleDateFormat.format((Date) object);
cell.setCellValue(time);
} else if (object instanceof Calendar) {
Calendar calendar = (Calendar) object;
String time = simpleDateFormat.format(calendar.getTime());
cell.setCellValue(time);
} else if (object instanceof Boolean) {
cell.setCellValue((Boolean) object);
} else {
cell.setCellValue(object.toString());
}
}
}
/*
* 写入到文件中
*/
boolean isCorrect = false;
try {
File file = new File(path);
OutputStream outputStream = new FileOutputStream(file);
workbook.write(outputStream);
outputStream.close();
isCorrect = true;
} catch (IOException e) {
isCorrect = false;
LOGGER.error("write Excel file error : {}", e.getMessage());
}
try {
workbook.close();
} catch (IOException e) {
isCorrect = false;
LOGGER.error("workbook closed error : {}", e.getMessage());
}
return isCorrect;
}
/**
* Create a library of cell styles
*/
/**
* @param wb
* @return
*/
private static Map createStyles(Workbook wb) {
Map styles = Maps.newHashMap();
// 标题样式
CellStyle titleStyle = wb.createCellStyle();
titleStyle.setAlignment(HorizontalAlignment.CENTER); // 水平对齐
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直对齐
titleStyle.setLocked(true); // 样式锁定
titleStyle.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
Font titleFont = wb.createFont();
titleFont.setFontHeightInPoints((short) 16);
titleFont.setBold(true);
titleFont.setFontName("微软雅黑");
titleStyle.setFont(titleFont);
styles.put("title", titleStyle);
// 文件头样式
CellStyle headerStyle = wb.createCellStyle();
headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); // 前景色
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 颜色填充方式
headerStyle.setWrapText(true);
headerStyle.setBorderRight(BorderStyle.THIN); // 设置边界
headerStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
headerStyle.setBorderLeft(BorderStyle.THIN);
headerStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
headerStyle.setBorderTop(BorderStyle.THIN);
headerStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
headerStyle.setBorderBottom(BorderStyle.THIN);
headerStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
Font headerFont = wb.createFont();
headerFont.setFontHeightInPoints((short) 12);
headerFont.setColor(IndexedColors.WHITE.getIndex());
titleFont.setFontName("微软雅黑");
headerStyle.setFont(headerFont);
styles.put("header", headerStyle);
Font cellStyleFont = wb.createFont();
cellStyleFont.setFontHeightInPoints((short) 12);
cellStyleFont.setColor(IndexedColors.BLUE_GREY.getIndex());
cellStyleFont.setFontName("微软雅黑");
// 正文样式A
CellStyle cellStyleA = wb.createCellStyle();
cellStyleA.setAlignment(HorizontalAlignment.CENTER); // 居中设置
cellStyleA.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyleA.setWrapText(true);
cellStyleA.setBorderRight(BorderStyle.THIN);
cellStyleA.setRightBorderColor(IndexedColors.BLACK.getIndex());
cellStyleA.setBorderLeft(BorderStyle.THIN);
cellStyleA.setLeftBorderColor(IndexedColors.BLACK.getIndex());
cellStyleA.setBorderTop(BorderStyle.THIN);
cellStyleA.setTopBorderColor(IndexedColors.BLACK.getIndex());
cellStyleA.setBorderBottom(BorderStyle.THIN);
cellStyleA.setBottomBorderColor(IndexedColors.BLACK.getIndex());
cellStyleA.setFont(cellStyleFont);
styles.put("cellA", cellStyleA);
// 正文样式B:添加前景色为浅黄色
CellStyle cellStyleB = wb.createCellStyle();
cellStyleB.setAlignment(HorizontalAlignment.CENTER);
cellStyleB.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyleB.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
cellStyleB.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyleB.setWrapText(true);
cellStyleB.setBorderRight(BorderStyle.THIN);
cellStyleB.setRightBorderColor(IndexedColors.BLACK.getIndex());
cellStyleB.setBorderLeft(BorderStyle.THIN);
cellStyleB.setLeftBorderColor(IndexedColors.BLACK.getIndex());
cellStyleB.setBorderTop(BorderStyle.THIN);
cellStyleB.setTopBorderColor(IndexedColors.BLACK.getIndex());
cellStyleB.setBorderBottom(BorderStyle.THIN);
cellStyleB.setBottomBorderColor(IndexedColors.BLACK.getIndex());
cellStyleB.setFont(cellStyleFont);
styles.put("cellB", cellStyleB);
return styles;
}
}
这个是根据上一版做的xue微改进,应该是没BUG了。
第三版
public class ExcelExport implements Closeable {
private static final Logger LOGGER = LoggerFactory.getLogger(ExcelExport.class);
public static final String EXCEL_SUFFIX = ".xlsx"; // 目前只支持xlsx格式
private static final String SHEET_FONT_TYPE = "Arial";
private Workbook workbook;
private Sheet sheet;
private int rowNum;
private Map styles;
private List columns;
public ExcelExport createSheet(String sheetName, String title, Class> clazz) throws Exception {
this.workbook = createWorkbook();
this.columns = createColumns();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
ExcelField excelField = field.getAnnotation(ExcelField.class);
if (excelField != null) {
this.columns.add(new ColumnField(excelField.title(), field.getName(), field.getType(), excelField.width()));
}
}
if (CollectionUtils.isEmpty(this.columns)) throw new Exception("Excel's headerList are undefined");
this.sheet = workbook.createSheet(StringUtils.defaultString(sheetName, StringUtils.defaultString(title, "Sheet1")));
this.styles = createStyles(workbook);
this.rowNum = 0;
if (StringUtils.isNotBlank(title)) {
Row titleRow = sheet.createRow(rowNum++);
titleRow.setHeightInPoints(30);
Cell titleCell = titleRow.createCell(0);
titleCell.setCellStyle(styles.get("title"));
titleCell.setCellValue(title);
sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(),
this.columns.size() - 1));
}
Row headerRow = sheet.createRow(rowNum++);
headerRow.setHeightInPoints(16);
for (int i = 0; i < this.columns.size(); i++) {
int width = this.columns.get(i).width;
this.sheet.setColumnWidth(i, 256 * width + 184);
Cell cell = headerRow.createCell(i);
cell.setCellStyle(styles.get("header"));
cell.setCellValue(this.columns.get(i).title);
}
return this;
}
public ExcelExport setDataList(List dataList) throws IllegalAccessException {
for (E data : dataList) {
int column = 0;
Row row = this.addRow();
Map map = toMap(data);
for (ColumnField field : this.columns) {
Class> paramType = field.getParamType();
if (map.containsKey(field.getParam())) {
Object value = map.get(field.getParam());
this.addCell(row, column++, value, paramType);
}
}
}
LOGGER.debug("add data into {} success", this.sheet.getSheetName());
return this;
}
private Cell addCell(Row row, int column, Object value, Class> type) {
Cell cell = row.createCell(column);
if (value == null) {
cell.setCellValue("");
} else if (type.isAssignableFrom(String.class)) {
cell.setCellValue((String) value);
} else if (type.isAssignableFrom(Integer.class)) {
cell.setCellValue((Integer) value);
} else if (type.isAssignableFrom(Double.class)) {
cell.setCellValue((Double) value);
} else if (type.isAssignableFrom(Long.class)) {
cell.setCellValue((Long) value);
} else if (type.isAssignableFrom(Float.class)) {
cell.setCellValue((Float) value);
} else if (type.isAssignableFrom(Date.class)) {
Date time = (Date) value;
String timer = DateUtils.formatDate(time, "yyyy-MM-dd HH:mm:ss");
cell.setCellValue(timer);
} else {
cell.setCellValue(Objects.toString(value));
}
cell.setCellStyle(styles.get("data"));
return cell;
}
private Map toMap(Object entity) throws IllegalAccessException {
Map row = Maps.newHashMap();
if (null == entity) return row;
Class clazz = entity.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
row.put(field.getName(), field.get(entity));
}
return row;
}
private Row addRow() {
return sheet.createRow(rowNum++);
}
public ExcelExport write(OutputStream os) {
try {
workbook.write(os);
} catch (IOException ex) {
LOGGER.error(ex.getMessage(), ex);
} finally {
if (null != os) {
try {
os.close();
} catch (IOException e) {
LOGGER.error("close Output Stream failed: {}", e.getMessage());
}
}
}
return this;
}
public ExcelExport write(HttpServletResponse response, String fileName) {
response.reset();
try {
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, UTF8));
write(response.getOutputStream());
} catch (IOException ex) {
LOGGER.error(ex.getMessage(), ex);
}
return this;
}
public ExcelExport writeFile(String name) throws IOException {
FileOutputStream os = new FileOutputStream(name);
this.write(os);
return this;
}
private Workbook createWorkbook() {
return new SXSSFWorkbook();
}
private List createColumns() {
return Lists.newLinkedList();
}
private Map createStyles(Workbook workbook) {
Map styleMap = Maps.newHashMap();
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
Font titleFont = workbook.createFont();
titleFont.setFontName(SHEET_FONT_TYPE);
titleFont.setFontHeightInPoints((short) 16);
titleFont.setBold(true);
style.setFont(titleFont);
styleMap.put("title", style);
style = workbook.createCellStyle();
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 = workbook.createFont();
dataFont.setFontName(SHEET_FONT_TYPE);
dataFont.setFontHeightInPoints((short) 10);
style.setFont(dataFont);
styleMap.put("data", style);
style = workbook.createCellStyle();
style.cloneStyleFrom(styleMap.get("data"));
style.setWrapText(true);
style.setAlignment(HorizontalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = workbook.createFont();
headerFont.setFontName(SHEET_FONT_TYPE);
headerFont.setFontHeightInPoints((short) 10);
headerFont.setBold(true);
headerFont.setColor(IndexedColors.WHITE.getIndex());
style.setFont(headerFont);
style.setBorderRight(BorderStyle.THIN);
styleMap.put("header", style);
return styleMap;
}
public Workbook getWorkbook() {
return workbook;
}
public void setWorkbook(Workbook workbook) {
this.workbook = workbook;
}
public Sheet getSheet() {
return sheet;
}
public void setSheet(Sheet sheet) {
this.sheet = sheet;
}
public int getRowNum() {
return rowNum;
}
public void setRowNum(int rowNum) {
this.rowNum = rowNum;
}
public Map getStyles() {
return styles;
}
public void setStyles(Map styles) {
this.styles = styles;
}
public List getColumns() {
return columns;
}
public void setColumns(List columns) {
this.columns = columns;
}
@Override
public void close() throws IOException {
if (workbook instanceof SXSSFWorkbook && ((SXSSFWorkbook) workbook).dispose())
workbook.close();
}
class ColumnField {
private String title;
private String param;
private Class> paramType;
private int width;
ColumnField(String title, String param, Class> paramType, int width) {
this.title = title;
this.param = param;
this.paramType = paramType;
this.width = width;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
public Class> getParamType() {
return paramType;
}
public void setParamType(Class> paramType) {
this.paramType = paramType;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
}
}
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelField {
/**
* 导出字段标题
*/
String title();
/**
* 列宽
*/
int width() default 10; // 后面还可以添加其他的属性,添加后再修改上面那个代码就行了
}
使用方式:
import com.xxx.utils.ExcelField;
public class ExcelDataModel {
@ExcelField(title = "ID", width = 4)
private String id;
@ExcelField(title = "序号", width = 4)
private Integer serial;
@ExcelField(title = "名字", width = 8)
private String name;
... (getter\setter)
@GetMapping(value = "export/post")
public void exportPost(@ModelAttribute RequestModel model, HttpServletResponse response) {
try (
ExcelExport excelExport = new ExcelExport();
OutputStream out = response.getOutputStream()
) {
List data = xxxService.selectExportData(model);
response.setContentType("octets/stream");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("xx列表", "UTF-8")
+ DateUtils.formatDate(new Date(), "yyyyMMddHHmmss") + ExcelExport.EXCEL_SUFFIX);
String title = "xx列表";
excelExport.createSheet("xx列表", title, ExcelDataModel.class);
excelExport.setDataList(data);
excelExport.write(out);
} catch (Exception e) {
e.printStackTrace();
}
}
这一版可能复杂一点,但是扩展性更好了,还能自定义样式,虽然客户没要求样式,但审美多点也是好的。