Apache POI如何读写Excel文件呢?

乔欣 Java经验 发布时间:2023-02-07 21:30:38 阅读数:15760 1
下文笔者讲述Apache POI读写Excel文件的方法分享,如下所示

Apache POI读写Excel文件的实现思路

实现思路:
    步骤1:引入 Apache POI依赖
   <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
   </dependency>
   
   步骤2:使用相关类及方法操作excel
    HSSF在类名之前添加前缀,以指示与Microsoft Excel 2003文件相关的操作。
    XSSF在类名之前添加前缀,以指示与Microsoft Excel 2007文件或更高版本相关的操作。
    XSSFWorkbook和HSSFWorkbook是充当Excel工作簿的类
    HSSFSheet和XSSFSheet是充当Excel工作表的类
     Row定义一个Excel行
     Cell定义参照行寻址的Excel单元格

ApachePOI写入excel示例

 
package com.java265;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class ApachePOIExcelWrite {
 
    private static final String FILE_NAME = "/tmp/testExcel.xlsx";
 
    public static void main(String[] args) {
 
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Datatypes in Java");
        Object[][] datatypes = {
                {"Datatype", "Type", "Size(in bytes)"},
                {"int", "Primitive", 2},
                {"float", "Primitive", 4},
                {"double", "Primitive", 8},
                {"char", "Primitive", 1},
                {"String", "Non-Primitive", "No fixed size"}
        };
 
        int rowNum = 0;
        System.out.println("Creating excel");
 
        for (Object[] datatype : datatypes) {
            Row row = sheet.createRow(rowNum++);
            int colNum = 0;
            for (Object field : datatype) {
                Cell cell = row.createCell(colNum++);
                if (field instanceof String) {
                    cell.setCellValue((String) field);
                } else if (field instanceof Integer) {
                    cell.setCellValue((Integer) field);
                }
            }
        }
 
        try {
            FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
            workbook.write(outputStream);
            workbook.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        System.out.println("Done");
    }
}

Apache POI库读取Excel

package com.java265;
 
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
public class ApachePOIExcelRead {
    private static final String FILE_NAME = "/tmp/testExcel.xlsx";
 
    public static void main(String[] args) {
        try {
            FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
            Workbook workbook = new XSSFWorkbook(excelFile);
            Sheet datatypeSheet = workbook.getSheetAt(0);
            Iterator<Row> iterator = datatypeSheet.iterator();
 
            while (iterator.hasNext()) {
 
                Row currentRow = iterator.next();
                Iterator<Cell> cellIterator = currentRow.iterator();
 
                while (cellIterator.hasNext()) {
 
                    Cell currentCell = cellIterator.next();
                    //getCellTypeEnum shown as deprecated for version 3.15
                    //getCellTypeEnum ill be renamed to getCellType starting from version 4.0
                    if (currentCell.getCellTypeEnum() == CellType.STRING) {
                        System.out.print(currentCell.getStringCellValue() + "--");
                    } else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
                        System.out.print(currentCell.getNumericCellValue() + "--");
                    }
 
                }
                System.out.println();
 
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202302/16757770385688.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者