Excel 导出 哪位大神有更好的可以指点一下
/**
* expressions封装的查询条件使用时可以自行修改
*
*/
@PostMapping("/reward/exportContract")
public void exportContract(HttpServletResponse response, @RequestParam(name = "order") String orderBy,
@JsonRequestParam(value = "exp", required = false) Expression[] expressions) throws Exception {
int startPosition = 0;
int maxResults = -1;
//查询导出数据
ListRange result = this.queryList(orderBy, startPosition, maxResults, expressions);
//result转实体类数组
DsznJlsqInfo[] data = (DsznJlsqInfo[]) result.getData();
try {
HSSFWorkbook wb = new HSSFWorkbook();
//标题字体
HSSFFont title_font = wb.createFont();
title_font.setFontName("黑体");
title_font.setFontHeightInPoints((short) 24);
title_font.setBold(true);
//标题样式
HSSFCellStyle title_style = wb.createCellStyle();
title_style.setAlignment(HorizontalAlignment.CENTER);
title_style.setVerticalAlignment(VerticalAlignment.CENTER);
title_style.setFont(title_font);
//首行字体
HSSFFont first_font = wb.createFont();
first_font.setFontHeightInPoints((short) 12);
first_font.setBold(true);
//首行样式
HSSFCellStyle first_style = wb.createCellStyle();
first_style.setAlignment(HorizontalAlignment.CENTER);
first_style.setVerticalAlignment(VerticalAlignment.CENTER);
first_style.setFont(first_font);
//数据行样式
HSSFCellStyle data_style = wb.createCellStyle();
data_style.setAlignment(HorizontalAlignment.CENTER);
data_style.setVerticalAlignment(VerticalAlignment.CENTER);
HSSFSheet sheet = wb.createSheet("表名");
HSSFRow row1 = sheet.createRow(0);
HSSFCell cell0 = row1.createCell(0);
HSSFCell cell1 = row1.createCell(1);
HSSFCell cell2 = row1.createCell(2);
HSSFCell cell3 = row1.createCell(3);
HSSFCell cell4 = row1.createCell(4);
HSSFCell cell5 = row1.createCell(5);
HSSFCell cell6 = row1.createCell(6);
HSSFCell cell7 = row1.createCell(7);
HSSFCell cell8 = row1.createCell(8);
HSSFCell cell9 = row1.createCell(9);
//列头
cell0.setCellValue("序号");
cell1.setCellValue("标题");
cell2.setCellValue("申请人");
cell3.setCellValue("身份证号");
cell4.setCellValue("性别");
cell5.setCellValue("联系电话");
cell6.setCellValue("退休时间");
cell7.setCellValue("员工类型");
cell8.setCellValue("申报状态");
cell9.setCellValue("申报时间");
cell0.setCellStyle(first_style);
cell1.setCellStyle(first_style);
cell2.setCellStyle(first_style);
cell3.setCellStyle(first_style);
cell4.setCellStyle(first_style);
cell5.setCellStyle(first_style);
cell6.setCellStyle(first_style);
cell7.setCellStyle(first_style);
cell8.setCellStyle(first_style);
cell9.setCellStyle(first_style);
HSSFRow row2 = null;
if (data != null) {
for (int i = 0; i < data.length; i++) {
DsznJlsqInfo userItem = data[i];
row2 = sheet.createRow(i + 1);
HSSFCell cell20 = row2.createCell(0);
HSSFCell cell21 = row2.createCell(1);
HSSFCell cell22 = row2.createCell(2);
HSSFCell cell23 = row2.createCell(3);
HSSFCell cell24 = row2.createCell(4);
HSSFCell cell25 = row2.createCell(5);
HSSFCell cell26 = row2.createCell(6);
HSSFCell cell27 = row2.createCell(7);
HSSFCell cell28 = row2.createCell(8);
HSSFCell cell29 = row2.createCell(9);
cell20.setCellValue((i + 1) + "");
cell21.setCellValue(StringUtils.trim(userItem.getBt()));
cell22.setCellValue(StringUtils.trim(userItem.getSqr()));
cell23.setCellValue(StringUtils.trim(userItem.getSfzh()));
//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
cell24.setCellValue(StringUtils.trim(userItem.getGender()));
cell25.setCellValue(StringUtils.trim(userItem.getPhone()));
cell26.setCellValue(StringUtils.trim(userItem.getTxsj()));
cell27.setCellValue(StringUtils.trim(userItem.getYglx()));
cell28.setCellValue(StringUtils.trim(userItem.getStatus()));
cell29.setCellValue(StringUtils.trim(userItem.getSqsj()));
cell20.setCellStyle(data_style);
cell21.setCellStyle(data_style);
cell22.setCellStyle(data_style);
cell23.setCellStyle(data_style);
cell24.setCellStyle(data_style);
cell25.setCellStyle(data_style);
cell26.setCellStyle(data_style);
cell27.setCellStyle(data_style);
cell28.setCellStyle(data_style);
cell29.setCellStyle(data_style);
}
}
for (int i = 0; i <= 6; i++) {
sheet.autoSizeColumn(i);
sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 17 / 6);
}
//时间我用了包装类,使用时自行设置
String fileName = new String(("下载的表名-" + DF_TO_STRING.format((new Date()).getTime()) + ".xls").getBytes("gb2312"), "ISO-8859-1");
//准备将Excel的输出流通过response输出到页面下载
//八进制输出流
response.setContentType("application/octet-stream");
// 告诉浏览器用什么软件可以打开此文件
response.setHeader("content-Type", "application/vnd.ms-excel");
//设置导出Excel的名称
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
//刷新缓冲
response.flushBuffer();
wb.write(response.getOutputStream());
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
}