当前位置: 首页 > article >正文

Apache Pol (excel)

在这里插入图片描述
是一个处理微软各种文件格式的开源项目,一般情况下,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.kler.cn/a/589035.html

相关文章:

  • 算法——图论——关键活动
  • Python的那些事第四十五篇:继承自Nose的测试框架Nose2
  • 区间预测 | Matlab实现QRBiTCN分位数回归双向时间卷积神经网络注意力机制时序区间预测
  • Ubuntu 一站式初始化笔记
  • 【sql靶场】第13、14、17关-post提交报错注入保姆级教程
  • JVM常用概念之超态虚拟调用
  • 解析GNGGA数据,C语言单片机
  • AI是如何实现屏幕触控防水? 实测华为畅享70X
  • Redis监控:从睁眼瞎到千里眼的进化史
  • 【go语言圣经1.6】
  • 19.如何使用 pandas 处理大型 Excel 文件:并行读取工作表
  • pytorch小记(八):pytorch中有关于.detach()的浅显见解
  • PostgreSQL技术内幕26:PG聚合算子实现分析
  • 【VBA】excel获取股票实时行情(历史数据,基金数据下载)
  • 量子计算与医疗诊断的未来:超越传统的无限可能
  • 深度学习篇---Opencv中Haar级联分类器的自定义
  • Java 大视界 -- 基于 Java 的大数据机器学习模型的迁移学习应用与实践(129)
  • 五大基础算法——贪心算法
  • 各省水资源平台 水资源遥测终端机都用什么协议
  • CSS中绝对定位