zoukankan      html  css  js  c++  java
  • (三)如何使用Apache POI导入导出Excel报表

    1 导入maven坐标

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

    针对不同的类型格式,比如word,excel,ppt,pdf,poi都提供了对象,如下图:
    image.png

    2 从一个已经存在的Excel文件中读取文件

    代码如下:

    /**
         * 读取Excel表中的数据,并将每一单元格打印出来
         */
        public static void testRead(){
            try{
                //创建工作簿,文件已经存在,参数是文件地址
                XSSFWorkbook workbook = new XSSFWorkbook("/Users/mima000000/Desktop/1.xlsx");
                //一个工作簿有多个工作表
                // 获取工作表,既可以根据工作表的顺序获取,也可以根据工作表的名称获取
                //这里根据工作表的顺序获取
                XSSFSheet sheet = workbook.getSheetAt(0);
                //遍历工作表获得行对象
                for (Row row : sheet) {
                    //遍历行对象获取单元格对象
                    System.out.println("第一行值为:");
                    for (Cell cell : row) {
                        //获得单元格中的值
                        String value = cell.getStringCellValue();
                        System.out.print(value);
                    }
                }
                workbook.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    

    上面这段代码的作用是:通过创建一个XSSFWorkbook对象,拿到第一个工作表,遍历表每一行,同时遍历每一列,并打印。
    其中,涉及到这几个核心的对象

    XSSFWorkbook:工作簿
    XSSFSheet:工作簿中的一张工作表
    Row:每一行
    Cell:每一列
    

    你看,这么简单,不就完成了简单的通过Excel读取的任务了。

    3 通过poi把集合中数据导出到Excel文件中

    public static void testWrite(){
            try{
                //在内存中创建一个Excel文件
                XSSFWorkbook workbook = new XSSFWorkbook();
                //创建工作表,指定工作表名称
                XSSFSheet sheet = workbook.createSheet("传智播客");
                //创建行,0表示第一行
                XSSFRow row = sheet.createRow(0);
                //创建单元格,0表示第一个单元格
                row.createCell(0).setCellValue("编号");
                row.createCell(1).setCellValue("名称");
                row.createCell(2).setCellValue("年龄");
                XSSFRow row1 = sheet.createRow(1);
                row1.createCell(0).setCellValue("1");
                row1.createCell(1).setCellValue("小明");
                row1.createCell(2).setCellValue("10");
                XSSFRow row2 = sheet.createRow(2);
                row2.createCell(0).setCellValue("2");
                row2.createCell(1).setCellValue("小王");
                row2.createCell(2).setCellValue("20");
                //通过输出流将workbook对象下载到磁盘
                FileOutputStream out = new FileOutputStream("/Users/mima000000/Desktop/1.xlsx");
                workbook.write(out);
                out.flush();
                out.close();
                workbook.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    

    ok,这就搞定了文件的写操作

  • 相关阅读:
    Windows下对函数打桩,及Linux类似技术
    WIN10重启后,在任务栏下添加快捷工具栏消失问题修复
    VS2012下std::function的BUG解决办法
    【转载】inno setup 水波纹效果,检测安装vcredist_x86.exe等
    ArchLinux下XFCE的一个问题修复:thunar加载的环境变量不正确
    Daliy Algorithm (GPLT)-- day 94
    Daliy Algorithm (greedy , hash )-- day 93
    Daliy Algorithm (tarjan, greedy, bfs )-- day 92
    Daliy Algorithm (heap,greedy , IQ )-- day 91
    Daliy Algorithm (cf , GPLT )-- day 90
  • 原文地址:https://www.cnblogs.com/hellosiyu/p/13725322.html
Copyright © 2011-2022 走看看