Apache Pol (excel)

news/2025/3/17 22:30:35/

在这里插入图片描述
是一个处理微软各种文件格式的开源项目,一般情况下,POI都是用来操作EXCEL文件的 读和写操作
test测试
/*

  • 使用POI操作Excel文件

  • */
    public class POITest {

    /*

    • 通过POI创建Excel文件并写入文件内容

    • */
      public static void write() throws Exception{
      //在内存中创建一个excel文件
      XSSFWorkbook excel=new XSSFWorkbook();
      //在Excel文件中创建一个sheet页
      XSSFSheet sheet = excel.createSheet(“info”);
      //在sheet中创建行对象,rownum编号从0开始
      XSSFRow row = sheet.createRow(1);
      //创建单元格并写入文件内容
      row.createCell(1).setCellValue(“姓名”);
      row.createCell(2).setCellValue(“城市”);

      //创建一个新行
      row = sheet.createRow(2);
      row.createCell(1).setCellValue(“张三”);
      row.createCell(2).setCellValue(“北京”);

      row = sheet.createRow(3);
      row.createCell(1).setCellValue(“李四”);
      row.createCell(2).setCellValue(“南京”);

      //通过输出流将内存中的Excel文件写入磁盘中
      FileOutputStream out=new FileOutputStream(new File(“/Users/renliaoliao/Desktop/info.xlsx”));
      excel.write(out);

      //关闭资源
      out.close();
      excel.close();
      }

    /*

    • 通过POI读取Excel文件中的内容

    • */
      public static void read() throws Exception{
      //读取磁盘上以及存在的excel文件
      FileInputStream in = new FileInputStream(new File(“/Users/renliaoliao/Desktop/info.xlsx”));
      XSSFWorkbook excel = new XSSFWorkbook(in);
      //将文本文件读取出来 读取第一个sheet页
      XSSFSheet sheet = excel.getSheetAt(0);

      //获取sheet页中最后一行的行号
      int lastRowNum = sheet.getLastRowNum();

      for (int i=1;i<=lastRowNum;i++){
      //获取某一行
      XSSFRow row = sheet.getRow(i);
      //获取单元格对象
      String cellValue1 = row.getCell(1).getStringCellValue();
      String cellValue2 = row.getCell(2).getStringCellValue();
      System.out.println(cellValue1+" "+cellValue2);
      }

      //关闭资源
      in.close();
      excel.close();
      }
      public static void main(String[] args) throws Exception {
      //write();
      read();
      }
      }

在这里插入图片描述

导出运营数据表数据
/*
* 导出运营数据报表
* */
@Override
public void expo rtBusinessData(HttpServletResponse response) {
//1.查询数据库 获取运营数据
LocalDate dateBegin = LocalDate.now().minusDays(30);
LocalDate dateEnd = LocalDate.now().minusDays(1);
//查询概览数据
BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(dateBegin, LocalTime.MIN), LocalDateTime.of(dateEnd, LocalTime.MAX));

    //2.通过POI将数据写入到Excel文件中InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");try {//基于模板文件创建一个新的excel文件XSSFWorkbook excel = new XSSFWorkbook(in);//获取表格标签的sheet页XSSFSheet sheet = excel.getSheet("Sheet1");//填充数据--时间sheet.getRow(1).getCell(1).setCellValue("时间:"+dateBegin+"至"+dateEnd);//获取第四行XSSFRow row = sheet.getRow(3);row.getCell(2).setCellValue(businessDataVO.getTurnover());row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());row.getCell(6).setCellValue(businessDataVO.getNewUsers());//获得第五行row = sheet.getRow(4);row.getCell(2).setCellValue(businessDataVO.getValidOrderCount());row.getCell(4).setCellValue(businessDataVO.getUnitPrice());//填充明细数据for (int i = 0; i < 30; i++) {LocalDate date = dateBegin.plusDays(i);//查询某一天的营业数据BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));//获得某一行row=sheet.getRow(7+i);row.getCell(1).setCellValue(date.toString());row.getCell(2).setCellValue(businessData.getTurnover());row.getCell(3).setCellValue(businessData.getValidOrderCount());row.getCell(4).setCellValue(businessData.getOrderCompletionRate());row.getCell(5).setCellValue(businessData.getUnitPrice());row.getCell(6).setCellValue(businessData.getNewUsers());}//3.通过输出流将excel文件下载到客户端浏览器ServletOutputStream out = response.getOutputStream();excel.write(out);//关闭资源out.close();excel.close();} catch (IOException e) {throw new RuntimeException(e);}}

}


http://www.ppmy.cn/news/1579930.html

相关文章

vue 识别word表格中的图片

但是识别不出 .emf .tif tif是不显示 emf这是不识别 <template><div class"container"><h2>提取 Word 表格中的图片</h2><input type"file" change"handleFileUpload" accept".docx"><div v-if"…

某大厂自动化工程师面试题

一些大厂的自动化工程师面试题汇总: 基础知识类 请解释什么是PLC(可编程逻辑控制器)?什么是PID控制?它在自动化系统中的作用是什么?请描述一下工业4.0的基本概念。编程与控制系统类 你熟悉哪些PLC编程语言?请举例说明。如何在SCADA系统中实现数据采集和监控?请解释一下…

获取pytdx行情服务器ip和port

之前一直用的api.connect(124.71.187.122, 7709)突然不能用了&#xff0c;调查了一下&#xff0c;给出如下解决方案 打开通达信客户端&#xff0c;选项->通讯设置。 勾掉"登录时查找最快的主站"&#xff0c;手动选择"行情主站"&#xff0c;点确定。 点…

【SpringMVC】入门版

1.基本概念 1.1三层架构 三层架构也就是我们常说的b/s架构中的表现层&#xff0c;业务层和持久层,每层都各司其职&#xff0c;下面来分别讲解这三层的作用。 表现层&#xff1a; 也就是我们常说的web层。它负责接收客户端的请求&#xff0c;向客户端响应结果&#xff0c;通…

【前端】Vue3 + AntdVue + Ts + Vite4 + pnpm + Pinia 实战

文章目录 一、项目搭建1.1 生态工具对比包管理 pnpm&#xff08;npm、yarn&#xff09;打包工具 vite &#xff08;webpack&#xff09; 1.2 项目创建1.2.0 项目搭建1.2.1 node 版本1.2.2 corepack1.2.3 npm install -g1.2.4 pnpm1.2.5 pnpm add types/node --save-dev1.2.6 pn…

Android (Kotlin) 高版本 DownloadManager 封装工具类,支持 APK 断点续传与自动安装

以下是一个针对 Android 高版本的 DownloadManager 封装工具类&#xff0c;支持 断点续传 和 自动安装 APK 功能。该工具类兼容 Android 10 及以上版本的文件存储策略&#xff0c;并适配了 FileProvider 和未知来源应用安装权限。 工具类&#xff1a;DownloadUtils import and…

【Go学习】04-4-Gorm框架-增删改查事务钩子

【Go学习】04-4-Gorm框架-增删改查 增删改查插入数据用指定的字段创建忽略字段批量插入map创建sql表达式使用原生sql语句 更新数据保存数据更新单个列更新多列更新选定的字段表达式子查询更新 删除数据查询数据查询函数whereselectorder分页count分组直接执行sql语句 事务和Hoo…

207、【图论】孤岛的总面积

题目 思路 相比于 206、【图论】岛屿数量&#xff0c;就是在这个代码的基础上。先遍历边界&#xff0c;将边界连接的岛屿变为0&#xff0c;然后再计算一遍当前为1的岛屿面积。 代码实现 import collectionsn, m list(map(int, input().split())) graph []for _ in range(n…