Apache POI簡(jiǎn)介及應(yīng)用場(chǎng)景
三十二、Apache POI
32.1 介紹
Apache POI 是一個(gè)處理Miscrosoft Office各種文件格式的開(kāi)源項(xiàng)目。簡(jiǎn)單來(lái)說(shuō)就是,我們可以使用POI在Java程序中對(duì)Miscrosoft Office各種文件進(jìn)行讀寫(xiě)操作。
一般情況下,POI都是用于操作Excel文件。
Apache POI 的應(yīng)用場(chǎng)景:
銀行網(wǎng)銀系統(tǒng)導(dǎo)出交易明細(xì)
各種業(yè)務(wù)系統(tǒng)導(dǎo)出Excel報(bào)表
批量導(dǎo)入業(yè)務(wù)數(shù)據(jù)
32.2 入門(mén)案例
Apache POI 既可以將數(shù)據(jù)寫(xiě)入Excel文件,也可以讀取Excel文件中的數(shù)據(jù),接下來(lái)分別進(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>
32.2.1 將數(shù)據(jù)寫(xiě)入Excel文件
1). 代碼開(kāi)發(fā)
package com.sky.test; 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.FileNotFoundException; import java.io.FileOutputStream; public class POITest { /** * 通過(guò)POI創(chuàng)建Excel 文件并且寫(xiě)入文件內(nèi)容 */ public static void write() throws Exception { //在內(nèi)存中創(chuàng)建一個(gè)Excel文件 XSSFWorkbook excel = new XSSFWorkbook(); //在Excel文件中創(chuàng)建一個(gè)Sheet頁(yè) XSSFSheet sheet = excel.createSheet("info"); //在Sheet中創(chuàng)建一個(gè)行對(duì)象,rownum編號(hào)從0開(kāi)始 XSSFRow row = sheet.createRow(0); //創(chuàng)建單元格并且寫(xiě)入文件內(nèi)容 row.createCell(0).setCellValue("姓名"); row.createCell(1).setCellValue("城市"); //創(chuàng)建一個(gè)新行 row = sheet.createRow(1); row.createCell(0).setCellValue("張三"); row.createCell(1).setCellValue("北京"); row = sheet.createRow(2); row.createCell(0).setCellValue("李四"); row.createCell(1).setCellValue("南京"); //通過(guò)輸出流將內(nèi)存中的Excel文件寫(xiě)入到磁盤(pán) FileOutputStream out = new FileOutputStream(new File("D:\\info.xlsx")); excel.write(out); //關(guān)閉資源 out.close(); excel.close(); } public static void main(String[] args) throws Exception { write(); } }
2). 實(shí)現(xiàn)效果
在D盤(pán)中生成info.xlsx文件,創(chuàng)建名稱為info的Sheet頁(yè),同時(shí)將內(nèi)容成功寫(xiě)入。
32.2.2 讀取Excel文件中的數(shù)據(jù)
1). 代碼開(kāi)發(fā)
/** * 通過(guò)POI讀取Excel文件中的內(nèi)容 */ public static void read() throws Exception { InputStream inputStream = new FileInputStream(new File("D:\\info.xlsx")); //讀取磁盤(pán)上已經(jīng)存在的Excel文件 XSSFWorkbook excel = new XSSFWorkbook(inputStream); //讀取Excel文件中的第一個(gè)Sheet頁(yè) XSSFSheet sheet = excel.getSheetAt(0); //獲取Sheet中最后一行的行號(hào) int lastRowNum = sheet.getLastRowNum(); for (int i=0;i<=lastRowNum;i++){ //獲得某一行 XSSFRow row = sheet.getRow(i); //獲得單元格對(duì)象 String cellValue1 = row.getCell(0).getStringCellValue(); String cellValue2 = row.getCell(1).getStringCellValue(); System.out.println(cellValue1+" "+cellValue2); } //關(guān)閉資源 inputStream.close(); excel.close(); } public static void main(String[] args) throws Exception { //write(); read(); }
2). 實(shí)現(xiàn)效果
到此這篇關(guān)于Apache POI簡(jiǎn)介的文章就介紹到這了,更多相關(guān)Apache POI簡(jiǎn)介內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Apache POI的基本使用詳解
- Java使用Apache.POI中HSSFWorkbook導(dǎo)出到Excel的實(shí)現(xiàn)方法
- Apache POI將PPT轉(zhuǎn)換成圖片實(shí)例代碼
- 基于apache poi根據(jù)模板導(dǎo)出excel的實(shí)現(xiàn)方法
- 利用Java Apache POI 生成Word文檔示例代碼
- java后臺(tái)利用Apache poi 生成excel文檔提供前臺(tái)下載示例
- java Apache poi 對(duì)word doc文件進(jìn)行讀寫(xiě)操作
- Java使用Apache POI庫(kù)讀取Excel表格文檔的示例
相關(guān)文章
ubuntu開(kāi)機(jī)后ROS程序自啟動(dòng)問(wèn)題
這篇文章主要介紹了ubuntu開(kāi)機(jī)后ROS程序自啟動(dòng)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12APACHE支持.htaccess偽靜重寫(xiě)出錯(cuò) No input file specified的解決方案
這篇文章主要介紹了APACHE支持.htaccess偽靜重寫(xiě)出錯(cuò) No input file specified的解決方案,需要的朋友可以參考下2016-09-09Linux 判斷文件修改時(shí)間和系統(tǒng)時(shí)間差
這篇文章主要介紹了 Linux 判斷文件修改時(shí)間和系統(tǒng)時(shí)間差的相關(guān)資料,需要的朋友可以參考下2017-05-05CentOS 7 安裝 MySQL 5.6遇到的各種問(wèn)題小結(jié)
在一測(cè)試服務(wù)器(CentOS Linux release 7.2.1511)上安裝MySQL 5.6(5.6.19 MySQL Community Server)時(shí)遇到了很多奇葩問(wèn)題,今天小編給大家總結(jié)了關(guān)于entOS 7 安裝 MySQL 5.6遇到的各種問(wèn)題,需要的朋友一起看看吧2016-11-11Centos修改DNS重啟或重啟network服務(wù)后丟失問(wèn)題解決方法
這篇文章主要介紹了Centos修改DNS重啟或重啟network服務(wù)后丟失問(wèn)題解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12centos 7 修改sshd | 禁止 root登錄及sshd端口腳本定義
這篇文章主要介紹了centos 7 修改sshd | 禁止 root登錄及sshd端口腳本定義,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09