|
|
|
@ -24,69 +24,64 @@ import cn.hutool.poi.excel.StyleSet;
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @ClassName: ExportExcelServlet
|
|
|
|
|
* @Description: 管理员 将所有订单信息导出为excel
|
|
|
|
|
* @Description: 管理员将所有订单信息导出为excel
|
|
|
|
|
* @author: ljy
|
|
|
|
|
* @date: 2019年11月24日 上午2:04:49
|
|
|
|
|
*/
|
|
|
|
|
public class ExportExcelServlet extends HttpServlet {
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
// 从数据库中拉取数据
|
|
|
|
|
PrepService prepService = new PrepServiceImpl();
|
|
|
|
|
List<Prep> rows = prepService.getAll();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 通过工具类创建writer
|
|
|
|
|
ExcelWriter writer = ExcelUtil.getWriter();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置单元格样式 (好像没用,待修复)
|
|
|
|
|
StyleSet style = writer.getStyleSet();
|
|
|
|
|
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("pmemberId", "会员资料ID");
|
|
|
|
|
writer.addHeaderAlias("startStation", "起始站");
|
|
|
|
|
writer.addHeaderAlias("endStation", "终点站");
|
|
|
|
|
writer.addHeaderAlias("trainNumber", "火车车次");
|
|
|
|
|
writer.addHeaderAlias("startTime", "开车时间");
|
|
|
|
|
writer.addHeaderAlias("endTime", "到站时间");
|
|
|
|
|
writer.addHeaderAlias("price", "车票价格");
|
|
|
|
|
writer.addHeaderAlias("way", "付款情况");
|
|
|
|
|
writer.addHeaderAlias("booktime", "订票时间");
|
|
|
|
|
|
|
|
|
|
// 合并单元格后的标题行,使用默认标题样式
|
|
|
|
|
writer.merge(10, "订单中心");
|
|
|
|
|
// 一次性写出内容,使用默认样式,强制输出标题
|
|
|
|
|
writer.write(rows, true);
|
|
|
|
|
|
|
|
|
|
//out为OutputStream,需要写出到的目标流
|
|
|
|
|
|
|
|
|
|
//response为HttpServletResponse对象
|
|
|
|
|
response.setContentType("application/vnd.ms-excel;charset=utf-8");
|
|
|
|
|
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
|
|
|
|
|
response.setHeader("Content-Disposition","attachment;filename=preps.xls");
|
|
|
|
|
ServletOutputStream out=response.getOutputStream();
|
|
|
|
|
|
|
|
|
|
writer.flush(out, true);
|
|
|
|
|
// 关闭writer,释放内存
|
|
|
|
|
writer.close();
|
|
|
|
|
//此处记得关闭输出Servlet流
|
|
|
|
|
IoUtil.close(out);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理GET请求,用于导出Excel文件。
|
|
|
|
|
* @param request HttpServletRequest对象,包含客户端请求信息。
|
|
|
|
|
* @param response HttpServletResponse对象,用于发送响应到客户端。
|
|
|
|
|
* @throws ServletException 可能抛出的Servlet异常。
|
|
|
|
|
* @throws IOException 可能抛出的IO异常。
|
|
|
|
|
*/
|
|
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
// 从数据库中拉取数据
|
|
|
|
|
PrepService prepService = new PrepServiceImpl();
|
|
|
|
|
List<Prep> rows = prepService.getAll();
|
|
|
|
|
|
|
|
|
|
// 通过工具类创建writer
|
|
|
|
|
ExcelWriter writer = ExcelUtil.getWriter();
|
|
|
|
|
|
|
|
|
|
// 设置单元格样式
|
|
|
|
|
StyleSet style = writer.getStyleSet();
|
|
|
|
|
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("pmemberId", "会员资料ID");
|
|
|
|
|
writer.addHeaderAlias("startStation", "起始站");
|
|
|
|
|
writer.addHeaderAlias("endStation", "终点站");
|
|
|
|
|
writer.addHeaderAlias("trainNumber", "火车车次");
|
|
|
|
|
writer.addHeaderAlias("startTime", "开车时间");
|
|
|
|
|
writer.addHeaderAlias("endTime", "到站时间");
|
|
|
|
|
writer.addHeaderAlias("price", "车票价格");
|
|
|
|
|
writer.addHeaderAlias("way", "付款情况");
|
|
|
|
|
writer.addHeaderAlias("booktime", "订票时间");
|
|
|
|
|
|
|
|
|
|
// 合并单元格后的标题行,使用默认标题样式
|
|
|
|
|
writer.merge(10, "订单中心");
|
|
|
|
|
// 一次性写出内容,使用默认样式,强制输出标题
|
|
|
|
|
writer.write(rows, true);
|
|
|
|
|
|
|
|
|
|
// 设置响应的内容类型为Excel文件
|
|
|
|
|
response.setContentType("application/vnd.ms-excel;charset=utf-8");
|
|
|
|
|
// 设置响应头,提示浏览器这是一个附件,需要下载
|
|
|
|
|
response.setHeader("Content-Disposition","attachment;filename=preps.xls");
|
|
|
|
|
ServletOutputStream out = response.getOutputStream();
|
|
|
|
|
|
|
|
|
|
// 将Excel写入到输出流
|
|
|
|
|
writer.flush(out, true);
|
|
|
|
|
// 关闭writer,释放内存
|
|
|
|
|
writer.close();
|
|
|
|
|
// 关闭输出流
|
|
|
|
|
IoUtil.close(out);
|
|
|
|
|
}
|
|
|
|
|
}
|