zoukankan      html  css  js  c++  java
  • 生成Excel文件

    @Autowired
        private WayBillService wayBillService;
        @Action("report_exportXls")
        public String exportXsl() throws Exception{
            //
            List<WayBill> wayBills = wayBillService.findWayBills(model);
            //生成Excel文件
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
            HSSFSheet sheet = hssfWorkbook.createSheet("运单数据");
            //表头
            HSSFRow headRow = sheet.createRow(0);
            headRow.createCell(0).setCellValue("运单号");
            headRow.createCell(1).setCellValue("寄件人");
            headRow.createCell(2).setCellValue("寄件人电话");
            headRow.createCell(3).setCellValue("寄件人地址");
            headRow.createCell(4).setCellValue("收件人");
            headRow.createCell(5).setCellValue("收件人电话");
            headRow.createCell(6).setCellValue("收件人地址");
            //表格数据
            for (WayBill wayBill : wayBills) {
                HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
                dataRow.createCell(0).setCellValue(wayBill.getWayBillNum());
                dataRow.createCell(1).setCellValue(wayBill.getSendName());
                dataRow.createCell(2).setCellValue(wayBill.getSendMobile());
                dataRow.createCell(3).setCellValue(wayBill.getSendAddress());
                dataRow.createCell(4).setCellValue(wayBill.getRecName());
                dataRow.createCell(5).setCellValue(wayBill.getRecMobile());
                dataRow.createCell(6).setCellValue(wayBill.getRecAddress());
            }
            //下载导出
            //设置头信息
            ServletActionContext.getResponse().setContentType("application/vnd.ms-excel");
            String filename = "运单数据.xls";
            
            String agent = ServletActionContext.getRequest().getHeader("user-agent");
            filename = FileUtils.encodeDownloadFilename(filename, agent);
            ServletActionContext.getResponse().setHeader("Content-Disposition",
                    "attachment;filename="+filename);
            ServletOutputStream outputStream = ServletActionContext.getResponse().getOutputStream();
            hssfWorkbook.write(outputStream);
            //关闭
            hssfWorkbook.close();
            return NONE;
        }

  • 相关阅读:
    python 有关datetime时间日期 以及时间戳转换
    开园杂记
    vue beforeRouteEnter 注意点
    html 头像裁剪框
    window server服务器配置ftp服务
    js 使用 delete 删除对象的属性
    win 自带的截屏工具
    idea 配置新建类自动加注解
    mysql varchar 使用唯一索引时无法区分大小写 可以使用varbinary
    在 laradock 环境中使用 laravel-swoole 加速你的 laravel 应用
  • 原文地址:https://www.cnblogs.com/lijingbo/p/7400095.html
Copyright © 2011-2022 走看看