目錄
1、介紹
2、代碼示例
2.1、將數(shù)據(jù)寫入Excel文件
2.2、讀取Excel文件中的數(shù)據(jù)
??作者介紹:雙非本科大三網(wǎng)絡(luò)工程專業(yè)在讀,阿里云專家博主,專注于Java領(lǐng)域?qū)W習(xí),擅長web應(yīng)用開發(fā)、數(shù)據(jù)結(jié)構(gòu)和算法,初步涉獵Python人工智能開發(fā)和前端開發(fā)。
??主頁:@逐夢蒼穹??您的一鍵三連,是我創(chuàng)作的最大動力??
1、介紹
官網(wǎng):Apache POI - the Java API for Microsoft Documents
Apache POI 是一個(gè)處理Miscrosoft Office各種文件格式的開源項(xiàng)目。簡單來說就是,我們可以使用 POI 在 Java 程序中對Miscrosoft Office各種文件進(jìn)行讀寫操作。
一般情況下,POI 都是用于操作 Excel 文件。
Apache POI 的應(yīng)用場景:
- 銀行網(wǎng)銀系統(tǒng)導(dǎo)出交易明細(xì)
- 各種業(yè)務(wù)系統(tǒng)導(dǎo)出Excel報(bào)表
- 批量導(dǎo)入業(yè)務(wù)數(shù)據(jù)
2、代碼示例
Apache POI既可以將數(shù)據(jù)寫入Excel文件,也可以讀取Excel文件中的數(shù)據(jù),接下來分別進(jìn)行實(shí)現(xiàn)。
Apache POI的maven坐標(biāo):
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
2.1、將數(shù)據(jù)寫入Excel文件
1). 代碼
package com.sky.test;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class POITest {
/**
* 基于POI向Excel文件寫入數(shù)據(jù)
* @throws Exception
*/
public static void write() throws Exception{
//在內(nèi)存中創(chuàng)建一個(gè)Excel文件對象
XSSFWorkbook excel = new XSSFWorkbook();
//創(chuàng)建Sheet頁
XSSFSheet sheet = excel.createSheet("xzl");
//在Sheet頁中創(chuàng)建行,0表示第1行
XSSFRow row1 = sheet.createRow(0);
//創(chuàng)建單元格并在單元格中設(shè)置值,單元格編號也是從0開始,1表示第2個(gè)單元格
row1.createCell(1).setCellValue("姓名");
row1.createCell(2).setCellValue("城市");
XSSFRow row2 = sheet.createRow(1);
row2.createCell(1).setCellValue("張三");
row2.createCell(2).setCellValue("北京");
XSSFRow row3 = sheet.createRow(2);
row3.createCell(1).setCellValue("李四");
row3.createCell(2).setCellValue("上海");
FileOutputStream out = new FileOutputStream(new File("E:\\xzl.xlsx"));
//通過輸出流將內(nèi)存中的Excel文件寫入到磁盤上
excel.write(out);
//關(guān)閉資源
out.flush();
out.close();
excel.close();
}
}
2). 實(shí)現(xiàn)效果
在E盤中生成xzl.xlsx文件,創(chuàng)建名稱為xzl的Sheet頁,同時(shí)將內(nèi)容成功寫入。
2.2、讀取Excel文件中的數(shù)據(jù)
1). 代碼
package com.sky.test;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class POITest {
/**
* 基于POI讀取Excel文件
* @throws Exception
*/
public static void read() throws Exception{
FileInputStream in = new FileInputStream(new File("E:\\xzl.xlsx"));
//通過輸入流讀取指定的Excel文件
XSSFWorkbook excel = new XSSFWorkbook(in);
//獲取Excel文件的第1個(gè)Sheet頁
XSSFSheet sheet = excel.getSheetAt(0);
//獲取Sheet頁中的最后一行的行號
int lastRowNum = sheet.getLastRowNum();
for (int i = 0; i <= lastRowNum; i++) {
//獲取Sheet頁中的行
XSSFRow titleRow = sheet.getRow(i);
//獲取行的第2個(gè)單元格
XSSFCell cell1 = titleRow.getCell(1);
//獲取單元格中的文本內(nèi)容
String cellValue1 = cell1.getStringCellValue();
//獲取行的第3個(gè)單元格
XSSFCell cell2 = titleRow.getCell(2);
//獲取單元格中的文本內(nèi)容
String cellValue2 = cell2.getStringCellValue();
System.out.println(cellValue1 + " " +cellValue2);
}
//關(guān)閉資源
in.close();
excel.close();
}
public static void main(String[] args) throws Exception {
read();
}
}
2). 實(shí)現(xiàn)效果
將xzl.xlsx文件中的數(shù)據(jù)進(jìn)行讀取文章來源:http://www.zghlxwxcb.cn/news/detail-830479.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-830479.html
到了這里,關(guān)于Apache POI | Java操作Excel文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!