POI导出EXCEL到网页下载

得到一个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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值