• Apache POI 读写 Excel 文件


    写入 Excel 文件

    // 写入 Excel 文件
    // ============================================================================== //
    Workbook wb = new HSSFWorkbook(); // or Workbook wb = new XSSFWorkbook();
    
    // Create a sheet.
    Sheet sheet = wb.createSheet("The Name of the Sheet");
    
    // Create a row and put some cells in it. Rows are 0 based.
    Row row = sheet.createRow(0);
    
    // Create a cell and put a value in it.
    Cell cell = row.createCell(0);
    cell.setCellValue("Let me have a try!");
    
    // Or do it on one line.
    row.createCell(1).setCellValue("Let me have a try!");
    
    /*
    备注:
    try (...) {
    	...
    }
    上述的表达能够在所有语句执行完之后自动释放括号中的资源。
    */
    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) { // or workbook.xlsx
        wb.write(fileOut);
    } catch (FileNotFoundException e) {
        System.out.println("The path is not correct!!!");
        System.exit(-1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    读取 Excel 文件

    // 读取 Excel 文件到 Workbook 对象中
    // ============================================================================== //
    
    /*
    使用 File 对象比使用 InputStream 对象所需的内存资源更少,因为使用 InputStream 对象
    需要将整个文件缓存到内存中。
     */
    
    // Use a file
    try {
        Workbook wb = WorkbookFactory.create(new File("MyExcel.xls")); // or MyExcel.xlsx
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    }
    
    // Use an InputStream, needs more memory
    try (InputStream in = new FileInputStream("MyExcel.xls")) { // or MyExcel.xlsx
        Workbook wb = WorkbookFactory.create(in);
    } catch (FileNotFoundException e) {
        System.out.println("The file does not exist or the path is not correct!!!");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    }
    

    遍历 Excel 文件

    // 遍历 Excel
    // ============================================================================== //
    try {
        Workbook wb = WorkbookFactory.create(new File("MyExcel.xls")); // or MyExcel.xlsx
    
        for (Sheet sheet : wb) {
            for (Row row : sheet) {
                for (Cell cell : row) {
                    // pass
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    }
    

    需要的 maven 依赖

    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.17</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.17</version>
    </dependency>
    

    完整代码

    package ai.labrador.poi.excel;
    
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.ss.usermodel.*;
    
    import java.io.*;
    
    /**
     * 学习使用 HSSF 或者 XSSF 读写 Excel 文件
     * Reference:
     * [Busy Developers' Guide to HSSF and XSSF Features](http://poi.apache.org/spreadsheet/quick-guide.html#NewWorkbook)
     *
     * @author  Genpeng Xu
     * @date    2018/06/05
     */
    public class LearnHSSFAndXSSF {
        public static void main(String[] args) {
    
            // 写入 Excel 文件
            // ============================================================================== //
            Workbook wb = new HSSFWorkbook(); // or Workbook wb = new XSSFWorkbook();
    
            // Create a sheet.
            Sheet sheet = wb.createSheet("The Name of the Sheet");
    
            // Create a row and put some cells in it. Rows are 0 based.
            Row row = sheet.createRow(0);
    
            // Create a cell and put a value in it.
            Cell cell = row.createCell(0);
            cell.setCellValue("Let me have a try!");
    
            // Or do it on one line.
            row.createCell(1).setCellValue("Let me have a try!");
    
            /*
            备注:
            try (...) {
                ...
            }
            上述的表达能够在所有语句执行完之后自动释放括号中的资源。
             */
            try (OutputStream fileOut = new FileOutputStream("workbook.xls")) { // or workbook.xlsx
                wb.write(fileOut);
            } catch (FileNotFoundException e) {
                System.out.println("The path is not correct!!!");
                System.exit(-1);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            // 读取 Excel 文件到 Workbook 对象中
            // ============================================================================== //
    
            /*
            使用 File 对象比使用 InputStream 对象所需的内存资源更少,因为使用 InputStream 对象
            需要将整个文件缓存到内存中。
             */
    
            // Use a file
            try {
                Workbook wb = WorkbookFactory.create(new File("MyExcel.xls")); // or MyExcel.xlsx
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InvalidFormatException e) {
                e.printStackTrace();
            }
    
            // Use an InputStream, needs more memory
            try (InputStream in = new FileInputStream("MyExcel.xls")) { // or MyExcel.xlsx
                Workbook wb = WorkbookFactory.create(in);
            } catch (FileNotFoundException e) {
                System.out.println("The file does not exist or the path is not correct!!!");
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InvalidFormatException e) {
                e.printStackTrace();
            }
    
            // 遍历 Excel
            // ============================================================================== //
            try {
                Workbook wb = WorkbookFactory.create(new File("MyExcel.xls")); // or MyExcel.xlsx
    
                for (Sheet sheet : wb) {
                    for (Row row : sheet) {
                        for (Cell cell : row) {
                            // pass
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InvalidFormatException e) {
                e.printStackTrace();
            }
        }
    }
    
  • 相关阅读:
    Log4J日志整合及配置详解
    SmartGit/Hg 4.0.1 发布
    Test Kitchen 0.7.0支持在OpenStack上对Opscode Chef进行集成测试
    RubyGNOME2 1.2.0 发布,支持 GTK+ 3
    PowerDNS Recursor 3.5 RC1 发布
    用于展现图表的50种JavaScript库
    Lambda表达式现状分析
    Node.js 0.8.18 / 0.9.7 发布
    CRUX 3.0 发布,轻量级 Linux 发行版
    Google 更新浏览器内的手写识别技术
  • 原文地址:https://www.cnblogs.com/xugenpeng/p/9143780.html
Copyright © 2020-2023  润新知