Update ExportExcelServlet.java

pull/1/head
pght2c95q 8 months ago
parent 77c4177bf0
commit 7bfcf69c25

@ -24,69 +24,64 @@ import cn.hutool.poi.excel.StyleSet;
/** /**
* *
* @ClassName: ExportExcelServlet * @ClassName: ExportExcelServlet
* @Description: excel * @Description: excel
* @author: ljy * @author: ljy
* @date: 20191124 2:04:49 * @date: 20191124 2:04:49
*/ */
public class ExportExcelServlet extends HttpServlet { public class ExportExcelServlet extends HttpServlet {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/**
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { * GETExcel
// 从数据库中拉取数据 * @param request HttpServletRequest
PrepService prepService = new PrepServiceImpl(); * @param response HttpServletResponse
List<Prep> rows = prepService.getAll(); * @throws ServletException Servlet
* @throws IOException IO
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 通过工具类创建writer // 从数据库中拉取数据
ExcelWriter writer = ExcelUtil.getWriter(); PrepService prepService = new PrepServiceImpl();
List<Prep> rows = prepService.getAll();
// 设置单元格样式 (好像没用,待修复) // 通过工具类创建writer
StyleSet style = writer.getStyleSet(); ExcelWriter writer = ExcelUtil.getWriter();
CellStyle cellStyle = style.getHeadCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中 // 设置单元格样式
cellStyle.setShrinkToFit(true); //设置单元格列宽自适应 StyleSet style = writer.getStyleSet();
cellStyle.setDataFormat((short) 2); CellStyle cellStyle = style.getHeadCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
cellStyle.setShrinkToFit(true); //设置单元格列宽自适应
cellStyle.setDataFormat((short) 2);
//自定义标题别名
writer.addHeaderAlias("prepId", "订单ID"); //自定义标题别名
writer.addHeaderAlias("trainId", "车次ID"); writer.addHeaderAlias("prepId", "订单ID");
writer.addHeaderAlias("pmemberId", "会员资料ID"); writer.addHeaderAlias("trainId", "车次ID");
writer.addHeaderAlias("startStation", "起始站"); writer.addHeaderAlias("pmemberId", "会员资料ID");
writer.addHeaderAlias("endStation", "终点站"); writer.addHeaderAlias("startStation", "起始站");
writer.addHeaderAlias("trainNumber", "火车车次"); writer.addHeaderAlias("endStation", "终点站");
writer.addHeaderAlias("startTime", "开车时间"); writer.addHeaderAlias("trainNumber", "火车车次");
writer.addHeaderAlias("endTime", "到站时间"); writer.addHeaderAlias("startTime", "开车时间");
writer.addHeaderAlias("price", "车票价格"); writer.addHeaderAlias("endTime", "到站时间");
writer.addHeaderAlias("way", "付款情况"); writer.addHeaderAlias("price", "车票价格");
writer.addHeaderAlias("booktime", "订票时间"); writer.addHeaderAlias("way", "付款情况");
writer.addHeaderAlias("booktime", "订票时间");
// 合并单元格后的标题行,使用默认标题样式
writer.merge(10, "订单中心"); // 合并单元格后的标题行,使用默认标题样式
// 一次性写出内容,使用默认样式,强制输出标题 writer.merge(10, "订单中心");
writer.write(rows, true); // 一次性写出内容,使用默认样式,强制输出标题
writer.write(rows, true);
//out为OutputStream需要写出到的目标流
// 设置响应的内容类型为Excel文件
//response为HttpServletResponse对象 response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setContentType("application/vnd.ms-excel;charset=utf-8"); // 设置响应头,提示浏览器这是一个附件,需要下载
//test.xls是弹出下载对话框的文件名不能为中文中文请自行编码 response.setHeader("Content-Disposition","attachment;filename=preps.xls");
response.setHeader("Content-Disposition","attachment;filename=preps.xls"); ServletOutputStream out = response.getOutputStream();
ServletOutputStream out=response.getOutputStream();
// 将Excel写入到输出流
writer.flush(out, true); writer.flush(out, true);
// 关闭writer释放内存 // 关闭writer释放内存
writer.close(); writer.close();
//此处记得关闭输出Servlet流 // 关闭输出流
IoUtil.close(out); IoUtil.close(out);
}
}
}
}
Loading…
Cancel
Save