poi excel文档生成与读取

1. introduce

  1. poi是什么
    答:用于excel的操作的,可以对集合,map进行操作生成对应的excel文档。做报表。

  2. 对应的iText是pdf操作的

  3. excel的两种版本

    1. 1997~2003版本的
    2. 2003版本的
      区别:后缀名不同。某w*s就是03的。。

      xls用老版本的excel和新版本的excel软件都能打开。
      而新版本的excel文件不会向下兼容。

2. 轮子

	<dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.9</version>
    </dependency>

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

3. demo 以九九乘法表为例

3.1 xls的生成

   /**
     * 给定excel文件名 和 excel文档 生成excel文件
     * @param excelFileName
     * @param wb
     */
    void createExcelFile(String excelFileName, Workbook wb) {
    
    
        // 生成文件,字节流输出
        File file = new File(excelFileName);

        OutputStream out = null;

        try {
    
    
            out = new FileOutputStream(file);
            try {
    
    
                wb.write(out);
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if (out != null) {
    
    
                try {
    
    
                    out.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    /**
     * 生成xls的excel文档 97~03 年
     */
    public void test2() {
    
    
        // 生成空白文档
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();

        // 生成sheet页
        HSSFSheet sheet1 = hssfWorkbook.createSheet("sheet page1");

        // 行
        for (int i = 0; i < 9; i++) {
    
    
            // 创建行
            HSSFRow row = sheet1.createRow(i);

            // 列
            for (int j = 0; j <= i; j++) {
    
    
                // 创建单元格
                HSSFCell cell = row.createCell(j);
                String res = (i + 1) + " * " + (j + 1) + " = " + (i + 1) * (j + 1);
                // 设置单元格内容
                cell.setCellValue(res);

            }
        }

        createExcelFile("excel1.xls", hssfWorkbook);
    }

3.2 xlsx的生成

换了个api

    /**
     * 生成xlsx 的文档
     */
    @Test
    public void test3() {
    
    
        // 空白文档
        XSSFWorkbook xssfSheets = new XSSFWorkbook();

        // sheet页1
        XSSFSheet sheet1 = xssfSheets.createSheet("sheet1");

        // 行
        for (int i = 0; i < 9; i++) {
    
    
            // 创建行
            XSSFRow row = sheet1.createRow(i);

            // 列
            for (int j = 0; j <= i; j++) {
    
    
                // 创建单元格
                XSSFCell cell = row.createCell(j);
                String res = (i + 1) + " * " + (j + 1) + " = " + (i + 1) * (j + 1);
                // 设置单元格内容
                cell.setCellValue(res);

            }
        }

        createExcelFile("excel2.xlsx", xssfSheets);

    }

3.3 读取xlsx

    /**
     * 利用poi读取xlsx文件
     */
    @Test
    public void test4() {
    
    

        try {
    
    
            InputStream in = new FileInputStream(new File("excel2.xlsx"));
            // 获得excel文档
            try {
    
    
                XSSFWorkbook xfb = new XSSFWorkbook(in);

                // sheet 页
                XSSFSheet sheet = xfb.getSheetAt(0);

                // 最大行的下标
                int lastRowNum = sheet.getLastRowNum();

                for (int i = 0; i < lastRowNum; i++) {
    
    
                    // 拿到每行
                    XSSFRow row = sheet.getRow(i);

                    // 最大单元的行下标
                    short cellNum = row.getLastCellNum();

                    // 遍历单元格
                    for (int j = 0; j < cellNum; j++) {
    
    
                        // 获得单元格
                        XSSFCell cell = row.getCell(j);
                        if (cell != null)
                            System.out.print(cell + " ");
                    }
                    System.out.println();
                }

            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        }


    }

最后,轮子的都得是,会用改,然后会用就行。

猜你喜欢

转载自blog.csdn.net/qq_44783283/article/details/109121080