Java读写excel文件

最近在学习某马的外卖项目,需要使用Java读写excel文件。

  • 导入依赖
xml 复制代码
<dependency>
 <groupId>org.apache.poi</groupId>
 <artifactId>poi</artifactId>
 <version>3.16</version>
</dependency>
<dependency>
 <groupId>org.apache.poi</groupId>
 <artifactId>poi-ooxml</artifactId>
 <version>3.16</version>
</dependency>
  • 读取excel
java 复制代码
FileInputStream in = new FileInputStream(new File("D:\\itcast.xlsx"));
//通过输入流读取指定的Excel文件XSSFWorkbook excel = new XSSFWorkbook(in);
//获取Excel文件的第1个Sheet页
XSSFSheet sheet = excel.getSheetAt(0);
//获取Sheet页中的最后一行的行号
int lastRowNum = sheet.getLastRowNum();
for (int i = 0; i <= lastRowNum; i++) {
     //获取Sheet页中的行
     XSSFRow titleRow = sheet.getRow(i);
     //获取行的第2个单元格
     XSSFCell cell1 = titleRow.getCell(1);
     //获取单元格中的文本内容
     String cellValue1 = cell1.getStringCellValue();
     //获取行的第3个单元格
     XSSFCell cell2 = titleRow.getCell(2);
     //获取单元格中的文本内容
     String cellValue2 = cell2.getStringCellValue();
     System.out.println(cellValue1 + " " +cellValue2);
     }
     //关闭资源
     in.close();
     excel.close();
  • 写excel
java 复制代码
//在内存中创建一个Excel文件对象
XSSFWorkbook excel = new XSSFWorkbook();
//创建Sheet页
XSSFSheet sheet = excel.createSheet("itcast");
//在Sheet页中创建行,0表示第1行
XSSFRow row1 = sheet.createRow(0);
//创建单元格并在单元格中设置值,单元格编号也是从0开始,1表示第2个单元格
row1.createCell(1).setCellValue("姓名");
row1.createCell(2).setCellValue("城市");
XSSFRow row2 = sheet.createRow(1);
row2.createCell(1).setCellValue("张三");
row2.createCell(2).setCellValue("北京");
XSSFRow row3 = sheet.createRow(2);
row3.createCell(1).setCellValue("李四");
row3.createCell(2).setCellValue("上海");
FileOutputStream out = new FileOutputStream(new File("D:\\itcast.xlsx"));
//通过输出流将内存中的Excel文件写入到磁盘上
excel.write(out);
//关闭资源out.flush();
out.close();
excel.close();
  • 下载excel
java 复制代码
    /**
     * 导出excel表
     * @param httpServletResponse
     */
    @Override
    public void exportData(HttpServletResponse httpServletResponse) throws IOException {
        //打开文件
        InputStream in=  this.getClass().getClassLoader().getResourceAsStream("template/template.xlsx");
        //创键excel对象
        XSSFWorkbook excel=new XSSFWorkbook(in);
        XSSFSheet sheet=excel.getSheetAt(0);
        //创建日期
        LocalDate end=LocalDate.now().plusDays(-1);
        LocalDate begin=LocalDate.now().plusDays(-30);
        sheet.getRow(1).getCell(1).setCellValue(begin+"---"+end);
        //读取概览数据
        BusinessDataVO businessDataVO=this.getBusinessData(LocalDateTime.of(begin,LocalTime.MIN),LocalDateTime.of(end,LocalTime.MAX));
        sheet.getRow(3).getCell(2).setCellValue(businessDataVO.getTurnover());
        sheet.getRow(3).getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());
        sheet.getRow(3).getCell(6).setCellValue(businessDataVO.getNewUsers());
        sheet.getRow(4).getCell(2).setCellValue(businessDataVO.getValidOrderCount());
        sheet.getRow(4).getCell(4).setCellValue(businessDataVO.getUnitPrice());
        //遍历每一天
        int cnt=7;
        while(!begin.equals(end)){
            BusinessDataVO businessData=this.getBusinessData(LocalDateTime.of(begin,LocalTime.MIN),LocalDateTime.of(begin,LocalTime.MAX));
            sheet.getRow(cnt).getCell(1).setCellValue(String.valueOf(begin));
            sheet.getRow(cnt).getCell(2).setCellValue(businessData.getTurnover());
            sheet.getRow(cnt).getCell(3).setCellValue(businessData.getValidOrderCount());
            sheet.getRow(cnt).getCell(4).setCellValue(businessData.getOrderCompletionRate());
            sheet.getRow(cnt).getCell(5).setCellValue(businessData.getUnitPrice());
            sheet.getRow(cnt).getCell(6).setCellValue(businessData.getNewUsers());
            begin=begin.plusDays(1);
            cnt++;
        }
        //最后一天
        BusinessDataVO businessData=this.getBusinessData(LocalDateTime.of(begin,LocalTime.MIN),LocalDateTime.of(begin,LocalTime.MAX));
        sheet.getRow(cnt).getCell(1).setCellValue(String.valueOf(begin));
        sheet.getRow(cnt).getCell(2).setCellValue(businessData.getTurnover());
        sheet.getRow(cnt).getCell(3).setCellValue(businessData.getValidOrderCount());
        sheet.getRow(cnt).getCell(4).setCellValue(businessData.getOrderCompletionRate());
        sheet.getRow(cnt).getCell(5).setCellValue(businessData.getUnitPrice());
        sheet.getRow(cnt).getCell(6).setCellValue(businessData.getNewUsers());
        begin=begin.plusDays(1);
        cnt++;

        //设置content-type
        httpServletResponse.setContentType("application/octet-stream");
        //设置文件头
        httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("example.xlsx", "UTF-8"));
        //获取输出流
        ServletOutputStream out=httpServletResponse.getOutputStream();
        excel.write(out);
        //关闭流
        in.close();
        out.close();
        excel.close();
    }
相关推荐
神仙别闹1 分钟前
基于Python实现LSTM对股票走势的预测
开发语言·python·lstm
保持学习ing3 分钟前
day1--项目搭建and内容管理模块
java·数据库·后端·docker·虚拟机
京东云开发者14 分钟前
Java的SPI机制详解
java
超级小忍36 分钟前
服务端向客户端主动推送数据的几种方法(Spring Boot 环境)
java·spring boot·后端
程序无bug40 分钟前
Spring IoC注解式开发无敌详细(细节丰富)
java·后端
小莫分享43 分钟前
Java Lombok 入门
java
程序无bug43 分钟前
Spring 对于事务上的应用的详细说明
java·后端
食亨技术团队44 分钟前
被忽略的 SAAS 生命线:操作日志有多重要
java·后端
苦学编程的谢1 小时前
Maven
java·maven·intellij-idea
考虑考虑1 小时前
Maven 依赖范围(Scope)
java·后端·maven