/**
* 导出派送失败的报表
*/
public void exportData() {
Workbook hssfWorkbook = null;
InputStream is = null;
try {
String createTime_start = this.getPara("createTime_start");
String createTime_end = this.getPara("createTime_end");
ExpressOrder model = this.getModel(ExpressOrder.class);
String fileName = "jsp/dist/module/TrackTheQuery/派送失败报表模板.xlsx";
// File file = new File(this.getRequest().getServletContext().getRealPath("/") + fileName);
File file = new File(fileName);
is = new FileInputStream(file);
if (fileName.endsWith("xlsx")){
hssfWorkbook = new XSSFWorkbook(is);//Excel 2007
}else if (fileName.endsWith("xls")){
hssfWorkbook = new HSSFWorkbook(is);//Excel 2003
}
Sheet sheet = hssfWorkbook.getSheetAt(0);
//1) 导出第一行
//第二: 获取第一行
Row row = sheet.getRow(0);
//第三:获取第一行第二列
Cell cell = row.getCell(1);
//1.4设置单元格内容
// String bigTitle = inputDate.replace("-0", "-").replace("-", "年") + "月份出货表";
// cell.setCellValue(bigTitle);
//第三: 获取第三行
row = sheet.getRow(2);
// 获取第三行每一个单元格样式
CellStyle cellStyle_1 = row.getCell(1).getCellStyle();
CellStyle cellStyle_2 = row.getCell(2).getCellStyle();
CellStyle cellStyle_3 = row.getCell(3).getCellStyle();
CellStyle cellStyle_4 = row.getCell(4).getCellStyle();
CellStyle cellStyle_5 = row.getCell(5).getCellStyle();
CellStyle cellStyle_6 = row.getCell(6).getCellStyle();
CellStyle cellStyle_7 = row.getCell(7).getCellStyle();
CellStyle cellStyle_8 = row.getCell(8).getCellStyle();
//3) 导出第三行和后面的行(数据行)
List<Record> list = TransportationService.SERVICE.getTransportationList(model, createTime_start, createTime_end);
if (list != null && list.size() > 0) {
int index = 2;
// 遍历货物
for (Record cp : list) {
// 创建第三行
row = sheet.createRow(index++);
// 设置行高
row.setHeightInPoints(24);
// 创建单元格(第二列)
cell = row.createCell(1);
cell.setCellValue(cp.getContract().getCustomName());
cell.setCellStyle(cellStyle_1);
// 创建单元格(第三列)
cell = row.createCell(2);
cell.setCellValue(cp.getContract().getContractNo());
cell.setCellStyle(cellStyle_2);
// 创建其他单元格
cell = row.createCell(3);
cell.setCellValue(cp.getProductNo());
cell.setCellStyle(cellStyle_3);
cell = row.createCell(4);
cell.setCellValue(cp.getCnumber());
cell.setCellStyle(cellStyle_4);
cell = row.createCell(5);
cell.setCellValue(cp.getFactory().getFactoryName());
cell.setCellStyle(cellStyle_5);
cell = row.createCell(6);
cell.setCellValue(cp.getContract().getDeliveryPeriod());
cell.setCellStyle(cellStyle_6);
cell = row.createCell(7);
cell.setCellValue(cp.getContract().getShipTime());
cell.setCellStyle(cellStyle_7);
cell = row.createCell(8);
cell.setCellValue(cp.getContract().getTradeTerms());
cell.setCellStyle(cellStyle_8);
}
}
//预存二进制
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// 将excel写到缓冲流中
hssfWorkbook.write(bos);
download(bos, this.getResponse(), "账单录入"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".xlsx");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
hssfWorkbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName)
throws IOException {
response.setContentType("application/octet-stream;charset=utf-8");
returnName = response.encodeURL(new String(returnName.getBytes(), "iso8859-1")); // 保存的文件名,必须和页面编码一致,否则乱码
response.addHeader("Content-Disposition", "attachment;filename=" + returnName); // .concat(String.valueOf(URLEncoder.encode("xxx.xls", "UTF-8")))
response.setContentLength(byteArrayOutputStream.size());
ServletOutputStream outputstream = response.getOutputStream(); // 取得输出流
byteArrayOutputStream.writeTo(outputstream); // 写到输出流
byteArrayOutputStream.close(); // 关闭
outputstream.flush(); // 刷数据
}