JAVA工程转化为MAVEN ,Apache POI读写EXCEL

本文介绍了如何将JAVA工程转化为MAVEN工程,并利用Apache POI库进行EXCEL文件的读写操作。通过添加POM.XML配置文件,引入必要的依赖,如poi-ooxml,实现了对HSSF(Excel 2003)和XSSF(Excel 2007及以后版本)的支持。核心类包括Workbook(工作簿)、Sheet(工作表)、Row(行)和Cell(单元格)。同时提供了JAR包下载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.新建一个JAVA 工程。
2.转化为MAVEN 工程:
在这里插入图片描述
在这里插入图片描述
3.然后会看到多了一个POM.XML :
在这里插入图片描述
然后右键选择:
在这里插入图片描述
填入一下:
在这里插入图片描述
发现自动多了一个:
在这里插入图片描述

再添加:
在这里插入图片描述
发现poi-ooxml也加进来了:
在这里插入图片描述
文字版:

<!-- Used to work with the older excel file format - `.xls` -->
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>

<!-- Used to work with the newer excel file format - `.xlsx` -->
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
  1. 增加如下代码:

在这里插入图片描述

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/MyFirstExcel.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");
    }
}
  1. 说明:Basic definitions for Apache POI library
    This section briefly describe about basic classes used during Excel Read and Write.

HSSF is prefixed before the class name to indicate operations related to a Microsoft Excel 2003 file.
XSSF is prefixed before the class name to indicate operations related to a Microsoft Excel 2007 file or later.
XSSFWorkbook and HSSFWorkbook are classes which act as an Excel Workbook
HSSFSheet and XSSFSheet are classes which act as an Excel Worksheet
Row defines an Excel row
Cell defines an Excel cell addressed in reference to a row.

当然啦,JAR包也可以下载:
https://jar-download.com/artifacts/org.apache.poi/poi-ooxml/3.17/source-code

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值