导出报表也是比较常见的功能,今天就聊聊导出Excel报表功能的实现吧。
$("#exportExcel").click("click", function() {//绑定导出按扭
var f = $('#fenye');
f.attr('target','_blank');
f.attr('action','${pageContext.request.contextPath}/background/account/export.html');
f.submit();
});
说明:
1、页面导出按钮绑定事件。
2、事件内容是提交表单,表单action请求的是后端导出报表接口。
@RequestMapping("export")
public void exportExcel(HttpServletResponse response,Account account) {
List<Account> acs = accountService.queryAll(account);
POIUtils.exportToExcel(response, "账号报表", acs, Account.class, "账号", acs.size());
}
导出报表后端接口主要调用poi工具类,同时将response传入过去,直接返回输出文件到浏览器。
/**
* 采用注解形式导出Excel
*/
public static void exportToExcel(HttpServletResponse response, String fileName, List objs, Class clazz,
String sheetName, int pageSize) {
OutputStream out = null;
try {
String tempName = new String(fileName.getBytes(), "ISO8859-1");
response.setHeader("content-disposition", "attachment;filename=" + tempName + ".xls");
response.setContentType("application/vnd.ms-excel");
HSSFWorkbook workbook = handleDataToExcel(objs, clazz, sheetName, pageSize);
out = response.getOutputStream();
workbook.write(out);
} catch (Exception e) {
logger.error("导出报表Excel异常", e);
} finally {
try {
if (out != null) {
out.flush();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
logger.error("关闭流异常", e);
}
}
}
说明:
1、设置返回浏览器格式为文件流格式。
2、将生成的HSSFWorkbook文件格式转换成文件流输出到页面。
3、handleDataToExcel 通过注解方式获得数据,输出excel对象文件。
/**
*组装数据,生成excel文件 *
*/
public static HSSFWorkbook handleDataToExcel(List list, Class clazz, String sheetName, int pageSize) throws Exception {
HSSFWorkbook workbook = null;
workbook = new HSSFWorkbook();
// 获取Excel标题
List<ExcelHeader> headers = getHeaderList(clazz);
Collections.sort(headers);
//
if (null != list && list.size() > 0) {
int sheetCount = list.size() % pageSize == 0 ? list.size() / pageSize : list.size() / pageSize + 1;
for (int i = 1; i <= sheetCount; i++) {
HSSFSheet sheet = null;
if (!StringUtils.isEmpty(sheetName)) {
sheet = workbook.createSheet(sheetName + i);
} else {
sheet = workbook.createSheet();
}
HSSFRow row = sheet.createRow(0);
// 写标题
CellStyle titleStyle = setCellStyle(workbook, position_title);
for (int j = 0; j < headers.size(); j++) {
ExcelHeader excelHeader = headers.get(j);
HSSFCell cell = row.createCell(j);
cell.setCellStyle(titleStyle);
cell.setCellValue(excelHeader.getTitle());
sheet.setColumnWidth(j, excelHeader.getWidth() * 256);
}
// 写内容
Object obj = null;
CellStyle bodyStyle = setCellStyle(workbook, position_body);
int begin = (i - 1) * pageSize;
int end = (begin + pageSize) > list.size() ? list.size() : (begin + pageSize);
int rowCount = 1;
for (int n = begin; n < end; n++) {
row = sheet.createRow(rowCount);
rowCount++;
obj = list.get(n);
for (int x = 0; x < headers.size(); x++) {
Cell cell = row.createCell(x);
cell.setCellStyle(bodyStyle);
Method method = clazz.getDeclaredMethod(headers.get(x).getMethodName());
Object value = method.invoke(obj);
if (value instanceof Date) {
JsonDateSerializer jsonDateSerializer = new JsonDateSerializer();
String formattedDate = jsonDateSerializer.dateFormat.format(new Date());
cell.setCellValue(formattedDate);
} else if (value instanceof Double) {
cell.setCellValue((Double) value);
} else if (value instanceof String) {
cell.setCellValue((String) value);
} else if (value instanceof Integer) {
cell.setCellValue((Integer) value);
}
}
}
}
}
return workbook;
}
说明:
1、首先创建HSSFWorkbook对象,并通过类名获取该类的注解信息,拼接标题的内容集合。
2、根据标题内容集合,写入HSSFRow标题行。
3、遍历传入的数据集合,并通过反射调用类函数,获取内容写入每行。
4、将生成的excel对象返回输出到页面。
软件定制及其他业务
请加微信号:13128600812