自定义导出excel字段工具类
方法:
public void exportExcel(String title, Collection<T> dataset,
List filetitle, List entityFile, ServletOutputStream out) {
// 声明一个工作薄
try {
Iterator<T> its = null;
// 首先检查数据看是否是正确的
if(null != dataset){
its = dataset.iterator();
}
if (null == its || dataset == null || !its.hasNext() || title == null
|| out == null) {
throw new Exception("传入的数据不对!");
}
// 取得实际泛型类
T ts = (T) its.next();
Class tCls = ts.getClass();
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为20个字节
int[] width = STYLE_WIDTH_ROLE;
sheet.setDefaultColumnWidth(20);
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setBorderBottom(HSSFCellStyle.BORDER_NONE);
style.setBorderLeft(HSSFCellStyle.BORDER_NONE);
style.setBorderRight(HSSFCellStyle.BORDER_NONE);
style.setBorderTop(HSSFCellStyle.BORDER_NONE);
style.setWrapText(true);
style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);// 设置背景色
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFCellStyle style1 = workbook.createCellStyle();// 3,设置表格风格
style1.setAlignment(CellStyle.ALIGN_CENTER);
style1.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style1.setBorderBottom(HSSFCellStyle.BORDER_NONE);
style1.setBorderLeft(HSSFCellStyle.BORDER_NONE);
style1.setBorderRight(HSSFCellStyle.BORDER_NONE);
style1.setBorderTop(HSSFCellStyle.BORDER_NONE);
style1.setWrapText(true);
// 设置标题样式
// style = ExcelStyle.setHeadStyle(workbook, style);
// 得到所有字段
// Field filed[] = ts.getClass().getDeclaredFields();
Object[] filed = entityFile.toArray();
// 标题 (字段标题,直接用数组传入)
List<String> exportfieldtile = filetitle;
// 导出的字段的get方法
List<Method> methodObj = new ArrayList<Method>();
// 遍历整个filed
for (int i = 0; i < entityFile.size(); i++) {
int j = i;
String fieldname = (String) entityFile.get(i);
String getMethodName = "get"
+ fieldname.substring(0, 1).toUpperCase()
+ fieldname.substring(1);
Method getMethod = tCls
.getMethod(getMethodName, new Class[] {});
methodObj.add(getMethod);
}
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
row.setHeightInPoints(18);
for (int i = 0; i < exportfieldtile.size(); i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(
exportfieldtile.get(i));
cell.setCellValue(text);
}
int index = 0;
// 循环整个集合
its = dataset.iterator();
int i = 1;
while (its.hasNext()) {
// 从第二行开始写,第一行是标题
index++;
row = sheet.createRow(index);
row.setHeightInPoints(18);
T t = (T) its.next();
for (int k = 0; k < methodObj.size() ; k++) {
int j = k;
HSSFCell cell = row.createCell(k);
sheet.setColumnWidth(index, width[4]);
cell.setCellStyle(style1);
/*if (k == 0) {// 每行第一个单元格是序号
cell.setCellValue(i++);// i++是先赋值然后执行+1
} else {*/
Method getMethod = methodObj.get(k );
Object value = getMethod.invoke(t, new Object[] {});
String textValue = getValue(value);
cell.setCellValue(textValue);
//}
}
}
workbook.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
调用方法:
String title = "";
List<String> sheetFields = new ArrayList<>();
List<String> beanFieldsStr = new ArrayList<>();
List<ExportExcelVo> excelList = new ArrayList<>();
// 下载参数设置
response.setContentType("octets/stream");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
ServletOutputStream outs = response.getOutputStream();// 输出流
ExportExcel<ExportExcelVo> exportExcel = new ExportExcel<ExportExcelVo>();
exportExcel.exportExcel(title, excelList, sheetFields, beanFieldsStr, outs);// 导出模版格式