POI介紹
ApachePOI是用Java編寫(xiě)的免費(fèi)開(kāi)源的跨平臺(tái)的JavaAPI,ApachePOI提供API給Java程序?qū)icrosoftOffice格式檔案讀和寫(xiě)的功能鹦肿,其中使用最多的就是使用POI操作Excel文件禁添。
maven坐標(biāo):
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
POI結(jié)構(gòu):
HSSF-提供讀寫(xiě)MicrosoftExcelXLS格式檔案的功能
XSSF-提供讀寫(xiě)MicrosoftExcelOOXMLXLSX格式檔案的功能
HWPF-提供讀寫(xiě)MicrosoftWordDOC格式檔案的功能
HSLF-提供讀寫(xiě)MicrosoftPowerPoint格式檔案的功能
HDGF-提供讀MicrosoftVisio格式檔案的功能
HPBF-提供讀MicrosoftPublisher格式檔案的功能
HSMF-提供讀MicrosoftOutlook格式檔案的功能
入門(mén)案例
從Excel文件讀取數(shù)據(jù)
使用POI可以從一個(gè)已經(jīng)存在的Excel文件中讀取數(shù)據(jù)
在e盤(pán)下創(chuàng)建一個(gè)xlsx文件
public class POITest {
//使用POI讀取Excel文件中的數(shù)據(jù)
@Test
public void test1() throws Exception{
//加載指定文件纠炮,創(chuàng)建一個(gè)Excel對(duì)象(工作簿)
XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("e:\\poi.xlsx")));
//讀取Excel文件中第一個(gè)Sheet標(biāo)簽頁(yè)
XSSFSheet sheet = excel.getSheetAt(0);
//遍歷Sheet標(biāo)簽頁(yè)嘉裤,獲得每一行數(shù)據(jù)
for (Row row : sheet) {
//遍歷行槐瑞,獲得每個(gè)單元格對(duì)象
for (Cell cell : row) {
System.out.println(cell.getStringCellValue());
}
}
//關(guān)閉資源
excel.close();
}
}
通過(guò)上面的入門(mén)案例可以看到犹芹,POI操作Excel表格封裝了幾個(gè)核心對(duì)象:
XSSFWorkbook:工作簿
XSSFSheet:工作表
Row:行
Cell:?jiǎn)卧?/p>
上面案例是通過(guò)遍歷工作表獲得行崎页,遍歷行獲得單元格,最終獲取單元格中的值腰埂。還有一種方式就是獲取工作表最后一個(gè)行號(hào)飒焦,從而根據(jù)行號(hào)獲得行對(duì)象,通過(guò)行獲取最后一個(gè)單元格索引屿笼,從而根據(jù)單元格索引獲取每行的一個(gè)單元格對(duì)象牺荠,代碼如下:
//使用POI讀取Excel文件中的數(shù)據(jù)
@Test
public void test2() throws Exception{
//加載指定文件,創(chuàng)建一個(gè)Excel對(duì)象(工作簿)
XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("e:\\poi.xlsx")));
//讀取Excel文件中第一個(gè)Sheet標(biāo)簽頁(yè)
XSSFSheet sheet = excel.getSheetAt(0);
//獲得當(dāng)前工作表中最后一個(gè)行號(hào)驴一,需要注意:行號(hào)從0開(kāi)始
int lastRowNum = sheet.getLastRowNum();
System.out.println("lastRowNum = " + lastRowNum);
for(int i=0;i<=lastRowNum;i++){
XSSFRow row = sheet.getRow(i);//根據(jù)行號(hào)獲取每一行
//獲得當(dāng)前行最后一個(gè)單元格索引
short lastCellNum = row.getLastCellNum();
System.out.println("lastCellNum = " + lastCellNum);
for(int j=0;j<lastCellNum;j++){
XSSFCell cell = row.getCell(j);//根據(jù)單元格索引獲得單元格對(duì)象
System.out.println(cell.getStringCellValue());
}
}
//關(guān)閉資源
excel.close();
}
向Excel文件寫(xiě)入數(shù)據(jù)
使用POI可以在內(nèi)存中創(chuàng)建一個(gè)Excel文件并將數(shù)據(jù)寫(xiě)入到這個(gè)文件休雌,最后通過(guò)輸出流將內(nèi)存中的Excel文件下載到磁盤(pán)
//使用POI向Excel文件寫(xiě)入數(shù)據(jù),并且通過(guò)輸出流將創(chuàng)建的Excel文件保存到本地磁盤(pán)
@Test
public void test3() throws Exception{
//在內(nèi)存中創(chuàng)建一個(gè)Excel文件(工作簿)
XSSFWorkbook excel = new XSSFWorkbook();
//創(chuàng)建一個(gè)工作表對(duì)象肝断,名稱(chēng)叫 嚶嚶嚶
XSSFSheet sheet = excel.createSheet("嚶嚶嚶");
//在工作表中創(chuàng)建行對(duì)象
XSSFRow title = sheet.createRow(0);
//在行中創(chuàng)建單元格對(duì)象
title.createCell(0).setCellValue("姓名");
title.createCell(1).setCellValue("地址");
title.createCell(2).setCellValue("年齡");
XSSFRow dataRow = sheet.createRow(1);
dataRow.createCell(0).setCellValue("小明");
dataRow.createCell(1).setCellValue("北京");
dataRow.createCell(2).setCellValue("20");
//創(chuàng)建一個(gè)輸出流杈曲,通過(guò)輸出流將內(nèi)存中的Excel文件寫(xiě)到磁盤(pán)
FileOutputStream out = new FileOutputStream(new File("e:\\hello.xlsx"));
excel.write(out);
out.flush();
excel.close();
}
測(cè)試結(jié)果: