导出Excel工具类,添加了反射和泛型的使用。
package cn.ibaner.corp.constants;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.List;
public class ExportExcel {
public static <T> String exportExcel(String sheetName,String titleName,String fileName,int columnNumber,int[] columnWidth
,String[] columnName,List<T> appFlowList){
if (columnNumber == columnWidth.length && columnWidth.length == columnName.length) {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// sheet.setDefaultColumnWidth(15); //统一设置列宽
for (int i = 0; i < columnNumber; i++) {
for (int j = 0; j <= i; j++) {
if (i == j) {
sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽
}
}
}
// 创建第0行 也就是标题
HSSFRow titleRow = sheet.createRow((int) 0);
titleRow.setHeightInPoints(30);// 设备标题的高度
// 第三步创建标题的单元格样式titleStyle以及字体样式headerFont1
HSSFCellStyle titleStyle = wb.createCellStyle();
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//titleStyle.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
//titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont titleFont = (HSSFFont) wb.createFont(); // 创建字体样式
titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
//titleFont.setFontName("黑体"); // 设置字体类型
titleFont.setFontHeightInPoints((short) 15); // 设置字体大小
titleStyle.setFont(titleFont); // 为标题样式设置字体样式
HSSFCell cell1 = titleRow.createCell(0);// 创建标题第一列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,
columnNumber - 1)); // 合并第0到第17列
cell1.setCellValue(titleName); // 设置值标题
cell1.setCellStyle(titleStyle); // 设置标题样式
// 创建第1行 也就是表头
HSSFRow row = sheet.createRow((int) 1);
row.setHeightInPoints(25);// 设置表头高度
// 第四步,创建表头单元格样式 以及表头的字体样式
HSSFCellStyle headerStyle = wb.createCellStyle();
headerStyle.setWrapText(true);// 设置自动换行
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式
headerStyle.setBottomBorderColor(HSSFColor.BLACK.index);
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
//headerFont.setFontName("黑体"); // 设置字体类型
headerFont.setFontHeightInPoints((short) 11); // 设置字体大小
headerStyle.setFont(headerFont); // 为标题样式设置字体样式
// 第四.一步,创建表头的列
for (int i = 0; i < columnNumber; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(columnName[i]);
cell.setCellStyle(headerStyle);
}
// 第五步,创建单元格,并设置值
if (appFlowList != null) {
for (int i = 0; i < appFlowList.size(); i++) {
//for (int i = 0; i < dataList.length; i++) {
row = sheet.createRow((int) i + 2);
// 为数据内容设置特点新单元格样式1 自动换行 上下居中
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setWrapText(true);// 设置自动换行
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式
// 设置边框
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
HSSFCell datacell = null;
Object obj = appFlowList.get(i);
if (obj == null) return "列表数据为空。";
Field[] fields = obj.getClass().getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
datacell = row.createCell(j);
try {
datacell.setCellValue((String) fields[j].get(obj));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
datacell.setCellStyle(cellStyle);
}
}
// 第六步,将文件存到指定位置
try {
String path = "D:\\" + fileName + ".xls";
FileOutputStream fout = new FileOutputStream(path);
wb.write(fout);
String str = "导出Excel“" + fileName + "”,成功。路径:" + path;
fout.close();
return str;
} catch (Exception e) {
e.printStackTrace();
String str = "导出Excel“" + fileName + "”,失败。";
return str;
}
}
}
return "导出表格Excel的列数据,要与显示的列表数据匹配。";
}
}
出处:https://blog.youkuaiyun.com/j_love93/article/details/77152293