Apache POI簡介及應用場景
三十二、Apache POI
32.1 介紹
Apache POI 是一個處理Miscrosoft Office各種文件格式的開源項目。簡單來說就是,我們可以使用POI在Java程序中對Miscrosoft Office各種文件進行讀寫操作。
一般情況下,POI都是用于操作Excel文件。
Apache POI 的應用場景:
銀行網(wǎng)銀系統(tǒng)導出交易明細
各種業(yè)務系統(tǒng)導出Excel報表
批量導入業(yè)務數(shù)據(jù)
32.2 入門案例
Apache POI 既可以將數(shù)據(jù)寫入Excel文件,也可以讀取Excel文件中的數(shù)據(jù),接下來分別進行實現(xiàn)。
Apache POI的maven坐標:
<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ù)寫入Excel文件
1). 代碼開發(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 { /** * 通過POI創(chuàng)建Excel 文件并且寫入文件內容 */ public static void write() throws Exception { //在內存中創(chuàng)建一個Excel文件 XSSFWorkbook excel = new XSSFWorkbook(); //在Excel文件中創(chuàng)建一個Sheet頁 XSSFSheet sheet = excel.createSheet("info"); //在Sheet中創(chuàng)建一個行對象,rownum編號從0開始 XSSFRow row = sheet.createRow(0); //創(chuàng)建單元格并且寫入文件內容 row.createCell(0).setCellValue("姓名"); row.createCell(1).setCellValue("城市"); //創(chuàng)建一個新行 row = sheet.createRow(1); row.createCell(0).setCellValue("張三"); row.createCell(1).setCellValue("北京"); row = sheet.createRow(2); row.createCell(0).setCellValue("李四"); row.createCell(1).setCellValue("南京"); //通過輸出流將內存中的Excel文件寫入到磁盤 FileOutputStream out = new FileOutputStream(new File("D:\\info.xlsx")); excel.write(out); //關閉資源 out.close(); excel.close(); } public static void main(String[] args) throws Exception { write(); } }
2). 實現(xiàn)效果
在D盤中生成info.xlsx文件,創(chuàng)建名稱為info的Sheet頁,同時將內容成功寫入。
32.2.2 讀取Excel文件中的數(shù)據(jù)
1). 代碼開發(fā)
/** * 通過POI讀取Excel文件中的內容 */ public static void read() throws Exception { InputStream inputStream = new FileInputStream(new File("D:\\info.xlsx")); //讀取磁盤上已經(jīng)存在的Excel文件 XSSFWorkbook excel = new XSSFWorkbook(inputStream); //讀取Excel文件中的第一個Sheet頁 XSSFSheet sheet = excel.getSheetAt(0); //獲取Sheet中最后一行的行號 int lastRowNum = sheet.getLastRowNum(); for (int i=0;i<=lastRowNum;i++){ //獲得某一行 XSSFRow row = sheet.getRow(i); //獲得單元格對象 String cellValue1 = row.getCell(0).getStringCellValue(); String cellValue2 = row.getCell(1).getStringCellValue(); System.out.println(cellValue1+" "+cellValue2); } //關閉資源 inputStream.close(); excel.close(); } public static void main(String[] args) throws Exception { //write(); read(); }
2). 實現(xiàn)效果
到此這篇關于Apache POI簡介的文章就介紹到這了,更多相關Apache POI簡介內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
APACHE支持.htaccess偽靜重寫出錯 No input file specified的解決方案
這篇文章主要介紹了APACHE支持.htaccess偽靜重寫出錯 No input file specified的解決方案,需要的朋友可以參考下2016-09-09CentOS 7 安裝 MySQL 5.6遇到的各種問題小結
在一測試服務器(CentOS Linux release 7.2.1511)上安裝MySQL 5.6(5.6.19 MySQL Community Server)時遇到了很多奇葩問題,今天小編給大家總結了關于entOS 7 安裝 MySQL 5.6遇到的各種問題,需要的朋友一起看看吧2016-11-11Centos修改DNS重啟或重啟network服務后丟失問題解決方法
這篇文章主要介紹了Centos修改DNS重啟或重啟network服務后丟失問題解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12centos 7 修改sshd | 禁止 root登錄及sshd端口腳本定義
這篇文章主要介紹了centos 7 修改sshd | 禁止 root登錄及sshd端口腳本定義,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09