java导出excel

后台代码

 

POI 

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.15</version>
</dependency>

        // 第一步,创建一个webbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("客户信息表");
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow(0);
        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        // 创建一个居中格式
        style.setAlignment(HorizontalAlignment.CENTER);
        //设置标题栏
        String [] cellValue = {"客户姓名","服务方向","性别","联系方式","负责人","导入时间","来源"};
        HSSFCell cell = row.createCell((short) 0);
        for (int i = 0;i<cellValue.length;i++){
            cell.setCellValue(cellValue[i]);
            cell.setCellStyle(style);
            //设置单元格宽度
            sheet.setColumnWidth(i,20*256);
            //设置居中样式
            sheet.setDefaultColumnStyle(i,style);
            cell = row.createCell((short) i+1);
        }
        // 第五步,写入实体数据 数据从数据库得到,
        List<CustomerDO> list = customerService.findAll(customerDO);
        for (int i = 0; i < list.size(); i++)
        {
            row = sheet.createRow( i + 1);
            CustomerDO customerDOExcel =  list.get(i);
            // 第四步,创建单元格,并设置值
            row.createCell((short) 0).setCellValue(customerDOExcel.getName());
            row.createCell((short) 1).setCellValue(customerDOExcel.getService());
            row.createCell((short) 2).setCellValue(customerDOExcel.getSex());
            row.createCell((short) 3).setCellValue(customerDOExcel.getPhone());
            row.createCell((short) 4).setCellValue(customerDOExcel.getResponsible());
            row.createCell((short) 5).setCellValue(new SimpleDateFormat("yyyy-mm-dd HH:MM:ss").format(customerDOExcel.getCreateDate()));
            row.createCell((short) 6).setCellValue(customerDOExcel.getSource());
        }
        // 第六步,将文件存到指定位置
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            // 文件名
            String fileName = "客户信息表.xls";
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
            wb.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

下面是设置列宽参考:来自连接https://blog.csdn.net/u012794845/article/details/81080171 

在使用NPOI技术开发自动操作EXCEL软件时遇到不能精确设置列宽的问题。

如

ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");

sheet1.SetColumnWidth(0,  50 * 256);   // 在EXCEL文档中实际列宽为49.29

sheet1.SetColumnWidth(1, 100 * 256);   // 在EXCEL文档中实际列宽为99.29

sheet1.SetColumnWidth(2, 150 * 256);   // 在EXCEL文档中实际列宽为149.29

到此一般人应该知道问题出在哪了,解决方法如下:

 

ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");

sheet1.SetColumnWidth(0,  (int)((50 + 0.72) * 256));   // 在EXCEL文档中实际列宽为50

sheet1.SetColumnWidth(1,  (int)((100 + 0.72) * 256));   // 在EXCEL文档中实际列宽为100

sheet1.SetColumnWidth(2,  (int)((150 + 0.72) * 256));   // 在EXCEL文档中实际列宽为150

既在要设置的实际列宽中加上列宽基数:0.72

 

 

前台vue页面下载

 var url = config.oaUrl + 'customer/exportCustomer';
          fetch(url,{
            method: 'POST',
            headers: new Headers({
              'token':dataUtils.getData(config.key.tokenKey) // 指定提交方式为表单提交
            }),
          }).then(res => res.blob().then(blob => {
            var a = document.createElement('a');
            var url = window.URL.createObjectURL(blob);
            var filename = '客户信息表.xlsx';
            a.href = url;
            a.download = filename;
            a.click();
            window.URL.revokeObjectURL(url);
          }));

 

 

效果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值