Apache POI的基本使用詳解
基本介紹
POI
- pache POI是用Java編寫的免費開源的跨平臺的Java API,Apache POI提供API給Java程序?qū)icrosoft Office格式檔案讀和寫的功能,
- 使用最多的就是使用POI操作Excel文件。
- 它還能操作word等這些其他形式的文檔
jxl:專門操作Excel,專門用來操作Excel的
使用POI,需要導入maven坐標
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency>
POI結(jié)構(gòu):針對不同的文檔形式來操作的時候會提供相應的一些類
HSSF - 提供讀寫Microsoft Excel XLS格式檔案的功能
XSSF - 提供讀寫Microsoft Excel OOXML XLSX格式檔案的功能
HWPF - 提供讀寫Microsoft Word DOC格式檔案的功能
HSLF - 提供讀寫Microsoft PowerPoint格式檔案的功能
HDGF - 提供讀Microsoft Visio格式檔案的功能
HPBF - 提供讀Microsoft Publisher格式檔案的功能
HSMF - 提供讀Microsoft Outlook格式檔案的功能
入門測試(從Excel文件讀取數(shù)據(jù))
使用POI可以從一個已經(jīng)存在的Excel文件中讀取數(shù)據(jù)
第一步:導入maven坐標
下面是第二步
第二步:創(chuàng)建Excel文件

第三步:寫測試代碼
package com.yy.test;
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 org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
/**
* @author Marston
* @date 2021/10/29
*/
public class POITest {
@Test
public void test() throws Exception {
//傳入一個輸入流,加載指定文件,創(chuàng)建一個Excel對象(工作簿)
XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("E:\\testNomal\\poi.xlsx")));
//讀取Excel文件中的第一個Sheet標簽頁
XSSFSheet sheet = excel.getSheetAt(0);
//一個sheet頁里面有很多行,遍歷這個sheet標簽頁,獲取每一行數(shù)據(jù)
for (Row row : sheet) {
//遍歷行,獲得每個單元格對象
for (Cell cell : row) {
//cell代表單元格對象
System.out.println(cell.getStringCellValue());//getStringCellValue第二列因為是數(shù)值,不能轉(zhuǎn)為String類型的所以報錯
//只要將Excel表格里面的第二列的內(nèi)容改為string類型的就可以了
}
}
//關(guān)閉Excel文件
excel.close();
}
}
運行結(jié)果:

因為是入門案例,我這里就要類型改變?yōu)橄旅娴?,將Excel文件里面的內(nèi)容修改后:


代碼說明及擴展
通過上面的入門案例可以看到,POI操作Excel表格封裝了幾個核心對象:
XSSFWorkbook:工作簿
XSSFSheet:工作表
Row:行
Cell:單元格
上面案例是通過遍歷工作表獲得行,遍歷行獲得單元格,最終獲取單元格中的值。
還有一種方式就是獲取工作表最后一個行號,從而根據(jù)行號獲得行對象,通過行獲取最后一個單元格索引,從而根據(jù)單元格索引獲取每行的一個單元格對象,代碼如下:
package com.yy.test;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
/**
* @author Marston
* @date 2021/10/29
*/
public class POITest {
@Test
public void test2() throws Exception {
//傳入一個輸入流,加載指定文件,創(chuàng)建一個Excel對象(工作簿)
XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("E:\\testNomal\\poi.xlsx")));
//讀取Excel文件中的第一個Sheet標簽頁
XSSFSheet sheet = excel.getSheetAt(0);
//獲取當前工作表最后一行的行號,行號從0開始
int lastRowNum = sheet.getLastRowNum();
System.out.println("lastRowNum:"+lastRowNum);
for(int i=0;i<=lastRowNum;i++){
//根據(jù)行號獲取每一行
XSSFRow row = sheet.getRow(i);
//獲取當前行最后一個單元格索引
short lastCellNum = row.getLastCellNum();
System.out.println("lastCellNum:"+lastCellNum);
for(short j=0;j<lastCellNum;j++){
//根據(jù)單元格索引獲取單元格內(nèi)容
String value = row.getCell(j).getStringCellValue();
System.out.println(value);
}
}
//關(guān)閉Excel文件
excel.close();
}
}

入門測試(向Excel文件寫入數(shù)據(jù))
測試代碼:
//使用POI向Excel文件寫入數(shù)據(jù),并且通過輸出流將創(chuàng)建的Excel文件保存到本地磁盤
//@Test
public void test3() throws Exception{
//在內(nèi)存中創(chuàng)建一個Excel文件(工作簿)
XSSFWorkbook excel = new XSSFWorkbook();
//創(chuàng)建一個工作表對象,名字叫做:POI寫入測試
XSSFSheet sheet = excel.createSheet("POI寫入測試");
//在工作表中創(chuàng)建行對象,在第一行創(chuàng)建
XSSFRow title = sheet.createRow(0);
//在行中創(chuàng)建單元格對象
title.createCell(0).setCellValue("姓名");//第一列內(nèi)容
title.createCell(1).setCellValue("地址");
title.createCell(2).setCellValue("年齡");
//在第二行創(chuàng)建
XSSFRow dataRow = sheet.createRow(1);
dataRow.createCell(0).setCellValue("小明");
dataRow.createCell(1).setCellValue("北京");
dataRow.createCell(2).setCellValue("20");
//創(chuàng)建一個輸出流,通過輸出流將內(nèi)存中的Excel文件寫到磁盤
FileOutputStream out = new FileOutputStream(new File("e:\\hello.xlsx"));
excel.write(out);//寫入
out.flush();//刷新
excel.close();
}

到此這篇關(guān)于Apache POI的基本使用的文章就介紹到這了,更多相關(guān)Apache POI使用內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信