java后台生成的Excel文件并通过浏览器下载

java后台生成Excel文件需要引入poi,
以下是poi的pom可以引入自己的项目里面去

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10-FINAL</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10-FINAL</version>
        </dependency>

生成Excel的demo

 
    @RequestMapping(value = "/exportExcel/{ids}")
    @ResponseBody
    public void exportExcel( HttpServletResponse response) throws IOException {


        
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("店铺信息");// 创建工作表(Sheet)
        int i = 0;
        //设置头部内容
        HSSFRow row = sheet.createRow(i);// 创建行,从0开始
        HSSFCell cell = row.createCell(0);// 创建行的单元格,也是从0开始
        cell.setCellValue("店铺名称");// 设置单元格内容
        row.createCell(1).setCellValue("店铺联系电话");
        row.createCell(2).setCellValue("店铺营业时间");
        row.createCell(3).setCellValue("店铺所属行业");
        row.createCell(4).setCellValue("店铺详细地址");
        row.createCell(5).setCellValue("店铺logo");// 设置单元格内容,重载

//        HSSFRow row1 = sheet.createRow(1);// 创建行,从0开始
//        HSSFCell cell1 = row1.createCell(0);// 创建行的单元格,也是从0开始
//        cell.setCellValue("店铺名称");// 设置单元格内容
//        row.createCell(1).setCellValue("店铺地址");// 设置单元格内容,重载
//        row.createCell(2).setCellValue("店铺营业时间");// 设置单元格内容,重载


        //==需要填充的数据
            int j = ++i;
            HSSFRow zrow = sheet.createRow(j);// 创建行,从0开始
            HSSFCell zcell = zrow.createCell(0);// 创建行的单元格,也是从0开始
            zcell.setCellValue("xxx国际大酒店");
            zrow.createCell(1).setCellValue("131xxxxxxxxxx");
            zrow.createCell(2).setCellValue("上午11点");
            zrow.createCell(3).setCellValue("软件");
            zrow.createCell(4).setCellValue("北京市xxxxxxxxxxx");
            zrow.createCell(5).setCellValue("http://www.xxxxxxxxxx.jpg");

        

        OutputStream output;
        try {
            output = response.getOutputStream();
            //清空缓存
            response.reset();
            //定义浏览器响应表头,顺带定义下载名,比如students(中文名需要转义)
            String s = "店铺入驻信息"+System.currentTimeMillis();
            response.setHeader("Content-disposition", "attachment;filename=" + new String(s.getBytes(), "iso-8859-1") + ".xls");
            //定义下载的类型,标明是excel文件
            response.setContentType("application/vnd.ms-excel");
            //这时候把创建好的excel写入到输出流
            workbook.write(output);
            //养成好习惯,出门记得随手关门
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

技术交流

在这里插入图片描述

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜空繁星vv

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值