java操作Excel。opi操作

1.导入依赖

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

倒完依赖就可以直接使用了

//1.首先创建一个excel 对象
XSSFWorkbook wb=new XSSFWorkbook();
//2.根据创建的操作对象创建操作页面  并给文件赋名字
XSSFSheet sheet = wb.createSheet( order.getCustomerName()+"表.xlsl");
//3.创建一行
XSSFRow row = sheet.createRow(0);
//4.根据创建的行创建列
//下标是几代表是第几个单元格内容
XSSFCell cell = row.createCell(0);
//给当前列赋值
cell.setCellValue("订单编号");

导出Excel文件
1.导出指定目录下(本地磁盘中)

//创建字符输出流
FileOutputStream outputStream=new FileOutputStream("F:\\tp\\test.xlsx");
//将内容写入进去
wb.write(outputStream);
//关闭流
wb.close;
outputStream.close;

2.前端点击下载按钮直接下载

//需要设置响应  防止中文乱码
response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(filename,"utf-8"));
//读取字符响应流
//os为字符响应流  response.getOutputStream()从controller传到service为outputStream
wb.write(os);
//关闭流
wb.close;
outputStream.close;

由于自己也不是很明白所以推荐看大佬分享的内容

https://blog.csdn.net/qq_42651904/article/details/88221392

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值