得到一个HTTP请求,直接请求到controller的方法
window.location.href=AppConfig.ctx+”/tmCosConsignment/PrintQueryUnconsignedPieces?”+piecesJsonParam;html
@SuppressWarnings("unchecked")
@RequestMapping("/tmCosConsignment/PrintQueryUnconsignedPieces")
public void printQueryUnconsignedPieces(HttpServletRequest request,HttpServletResponse response){
//********这一段是为了制造查询数据 使用时候可以忽略
Integer pageNum=Integer.valueOf(request.getParameter("pageNum"));
Integer pageSize=Integer.valueOf(request.getParameter("pageSize"));
Map queryParam=new LinkedHashMap();
queryParam.put("searchType","NOT_CLAIM");
Map map=new LinkedHashMap<>();
map.put("pageNum", pageNum);
map.put("pageSize", pageSize);
queryParam.put("pageInfo", map);
PageInfo<?> selectPageInfo = baseDao.selectPageInfo(SqlMapperConsignment.QUERY_UNCONSIGNED_CONSIGNMENT, queryParam);
List<TmCosPiecesModel> tmCosPiecesModels=(List<TmCosPiecesModel>) selectPageInfo.getData();
//查询到此为止
StringtemplatePath = request.getServletContext().getRealPath("")+CosExcelConstants.NO_FORC_STOCK_IN_LABEL_TEMPLATE_PATH;
XSSFWorkbook workbook=tmCosConsignmentService.createNoForecastListWorkBook(templatePath, tmCosPiecesModels);
response.reset();
//设置消息头 文件名
response.addHeader("Content-Disposition", "attachment;filename=no.xlsx");
response.setContentType("application/msexcel-print-Consignment;charset=UTF-8");
try{
//推送信息
OutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
}catch(Exception e) {
e.printStackTrace();
}
}
@Override
public XSSFWorkbook createNoForecastListWorkBook(String templatePath,List<TmCosPiecesModel> tmCosPiecesModels) {
XSSFWorkbook workbook=new XSSFWorkbook();
try {
//导入模板,如果是直接创建一个新的 就不需要这一步
workbook=new XSSFWorkbook(new FileInputStream(templatePath));
//得到表里的sheet 左下角那个表名 如果没有 可以NEW一个
XSSFSheet sheet = workbook.getSheet("Sheet1");
CellStyle borderStyle = createBlackAllBorderCellStyle(workbook);
for (int i = 0; i < tmCosPiecesModels.size(); i++) {
TmCosPiecesModel tmCosPiecesModel=tmCosPiecesModels.get(i);
sheet.createRow(i + 1);
XSSFCell cell = sheet.getRow(i + 1).createCell(0);
cell.setCellStyle(borderStyle);
cell.setCellValue(i+1);
cell = sheet.getRow(i + 1).createCell(1);
cell.setCellStyle(borderStyle);
cell.setCellValue(tmCosPiecesModel.getWarehouseCode());
cell = sheet.getRow(i + 1).createCell(2);
cell.setCellStyle(borderStyle);
cell.setCellValue(tmCosPiecesModel.getLogisticsNo());
cell = sheet.getRow(i + 1).createCell(3);
cell.setCellStyle(borderStyle);
cell.setCellValue(tmCosPiecesModel.getLocCode());
cell = sheet.getRow(i + 1).createCell(4);
cell.setCellStyle(borderStyle);
cell.setCellValue(tmCosPiecesModel.getMemberCode());
cell = sheet.getRow(i + 1).createCell(5);
cell.setCellStyle(borderStyle);
cell.setCellValue(tmCosPiecesModel.getMobileNo());
cell = sheet.getRow(i + 1).createCell(6);
cell.setCellStyle(borderStyle);
cell.setCellValue(tmCosPiecesModel.getEmail());
cell = sheet.getRow(i + 1).createCell(7);
cell.setCellStyle(borderStyle);
cell.setCellValue(tmCosPiecesModel.getSellerName());
cell = sheet.getRow(i + 1).createCell(8);
cell.setCellStyle(borderStyle);
cell.setCellValue(tmCosPiecesModel.getGoodsDescription());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return workbook;
}
//该方法用来设置边框样式
private CellStyle createBlackAllBorderCellStyle(Workbook wb) {
CellStyle s = wb.createCellStyle();
s.setBorderBottom(CellStyle.BORDER_THIN);
s.setBottomBorderColor(IndexedColors.BLACK.getIndex());
s.setBorderTop(CellStyle.BORDER_THIN);
s.setTopBorderColor(IndexedColors.BLACK.getIndex());
s.setBorderLeft(CellStyle.BORDER_THIN);
s.setLeftBorderColor(IndexedColors.BLACK.getIndex());
s.setBorderRight(CellStyle.BORDER_THIN);
s.setRightBorderColor(IndexedColors.BLACK.getIndex());
return s;
}