使用poi在web导出Excel表格这个功能相信在很多后台的管理系统都很常用,在此我在这里记录一下以备后用时方便查找。
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;
/**
* Excel导出
*
* @param request
* @param response
* @throws Exception
*/
@RequestMapping("/exporExcel")
public void exporExcel(HttpServletRequest request,HttpServletResponse response) throws Exception {
SqlCondition cond = new SqlCondition();
List<QryStudentRecordModel> list = studentSV.queryStudentsRecordByCondition(cond);
if (list != null && list.size() > 0) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("学生档案表");
String[] excelHeader =
{"序号", "姓名", "性别", "出生日期"};
// 单元格列宽
int[] excelHeaderWidth = {50, 100, 50, 100};
HSSFRow row = sheet.createRow((int) 0);
HSSFCellStyle style = wb.createCellStyle();
// 设置居中样式
style.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
// 设置合计样式
HSSFCellStyle style1 = wb.createCellStyle();
Font font = wb.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setBoldweight(Font.BOLDWEIGHT_BOLD); // 粗体
font.setFontHeightInPoints((short)12); //设置字体大小
style1.setFont(font);
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
// 合并单元格
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 2));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 3, 3));
// 设置列宽度(像素)
for (int i = 0; i < excelHeaderWidth.length; i++) {
sheet.setColumnWidth(i, 32 * excelHeaderWidth[i]);
}
// 添加表格头
for (int i = 0; i < excelHeader.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(excelHeader[i]);
cell.setCellStyle(style1);
}
row = sheet.createRow((int) 1);
//循环列表数据,逐个添加
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i + 2);
QryStudentRecordModel qryStudentRecordModel = list.get(i);
int cellNum = 0;
HSSFCell cell = row.createCell(cellNum++);
cell.setCellValue(i + 1);
cell.setCellStyle(style);
cell = row.createCell(cellNum++);
cell.setCellValue(qryStudentRecordModel.getStudentName());
cell.setCellStyle(style);
cell = row.createCell(cellNum++);
cell.setCellValue(qryStudentRecordModel.getSex());
cell.setCellStyle(style);
cell = row.createCell(cellNum++);
cell.setCellValue(qryStudentRecordModel.getBirthday());
cell.setCellStyle(style);
}
response.setContentType("application/vnd.ms-excel");
//注意此处文件名称如果想使用中文的话,要转码new String( "中文".getBytes( "gb2312" ), "ISO8859-1" )
response.setHeader("Content-disposition",
"attachment;filename=" + new String( "学生信息".getBytes( "gb2312" ), "ISO8859-1" ) + DateUtils
.formatDate(new Date(), "yyyyMMddHHmmss") + ".xls");
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}
}
前端页面请求:
$('#exportExcelBtn').click(function() {
window.location.href = url;//此处填上你请求Excel对应的url
});
注意,此处前端请求不能使用ajax,小编本来也用ajax,后面发现请求没报错也毫无反应,网上查了下原因,解释如下:
因为导出excel,实际上是文件下载,后台需要往前端(浏览器)写文件流的。
而Ajax请求获取数据都是“字符串”,整个交互传输用的都是字符串数据,它没法解析后台返回的文件流,但浏览器可以。
看来知道这些请求背后的基本原理也是很重要的。最后结果如下:
好了,以上就是最基本也是最实用用法。