Apache POI案例代碼詳解
簡介:Apache POI 是一個(gè)處理Miscrosoft Office各種文件格式的開源項(xiàng)目。簡單來說就是,我們可以使用POI在Java程序中對Miscrosoft Office各種文件進(jìn)行讀寫操作。
1、應(yīng)用場景
圖 1-1 Apache POI應(yīng)用場景
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、案例代碼
2.1 創(chuàng)建 Excel 文件
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;
import java.io.IOException;
public class POITest {
public static void main(String[] args) throws IOException {
write();
}
/**
* 通過POI創(chuàng)建Excel文件并且寫入文件內(nèi)容
*/
public static void write() throws IOException {
//創(chuàng)建一個(gè)Excel文件
XSSFWorkbook excel = new XSSFWorkbook();
//創(chuàng)建Excel中的單元測
XSSFSheet sheet = excel.createSheet("info");
//在sheet中創(chuàng)建行對象,rownum編號從零開始
XSSFRow row = sheet.createRow(1);
//創(chuàng)建單元格并且寫入文件內(nèi)容
row.createCell(1).setCellValue("姓名");
row.createCell(2).setCellValue("城市");
//創(chuàng)建一個(gè)新行
XSSFRow row1 = sheet.createRow(2);
row1.createCell(1).setCellValue("王寧");
row1.createCell(2).setCellValue("亳州");
//創(chuàng)建一個(gè)新行
XSSFRow row2 = sheet.createRow(2);
row2.createCell(1).setCellValue("王寧");
row2.createCell(2).setCellValue("亳州");
//將內(nèi)存中的Excel表格存儲到指定的盤符下面
FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\info.xlsx"));
excel.write(fileOutputStream);
//關(guān)閉資源
excel.close();
fileOutputStream.close();
}
}2.2 讀取 Excel 文件
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.*;
public class POITest {
public static void main(String[] args) throws IOException {
read();
}
/**
* 通過POI讀取Excel文件
*/
public static void read() throws IOException{
FileInputStream fileInputStream = new FileInputStream(new File("D:\\info.xlsx"));
//創(chuàng)建一個(gè)Excel對象,用于讀取Excle數(shù)據(jù)
XSSFWorkbook excel = new XSSFWorkbook(fileInputStream);
//讀取Excel文件中的第一個(gè)Sheet頁
XSSFSheet sheet = excel.getSheetAt(0);
//獲取Sheet頁中有數(shù)據(jù)的最后一行
int lastRowNum = sheet.getLastRowNum();
for (int i = 1; i <= lastRowNum; i++) {
//獲取某一行
XSSFRow row = sheet.getRow(i);
//獲取當(dāng)前行的單元格對象
String cellValue1 = row.getCell(1).getStringCellValue();
String cellValue2 = row.getCell(2).getStringCellValue();
System.out.println(cellValue1 + " " + cellValue2);
}
//關(guān)閉資源
excel.close();
fileInputStream.close();
}
}到此這篇關(guān)于Apache POI案例詳解的文章就介紹到這了,更多相關(guān)Apache POI內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
linux中g(shù)rep命令使用實(shí)戰(zhàn)詳解
這篇文章主要介紹了linux中g(shù)rep命令使用實(shí)戰(zhàn)詳解的相關(guān)資料,需要的朋友可以參考下2023-02-02
centos配置mutt和msmtp實(shí)現(xiàn)郵件發(fā)送
這篇文章主要為大家詳細(xì)介紹了centos配置mutt和msmtp實(shí)現(xiàn)郵件發(fā)送,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
CentOS6 配置Nginx,MySql,php-fpm開機(jī)啟動的方法
這篇文章主要介紹了CentOS6 配置Nginx,MySql,php-fpm開機(jī)啟動的方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
Vim如何使用相對行號實(shí)現(xiàn)一切操作詳解
在使用用Vim時(shí)時(shí)常會遇到像復(fù)制多行或者想快速向下/向上移動多行時(shí)而不知道行數(shù)的情況, 今天發(fā)現(xiàn)一個(gè)Vim對此有幫助的特性: 相對行號,下面這篇文章主要給大家介紹了關(guān)于Vim如何使用相對行號實(shí)現(xiàn)一切操作的相關(guān)資料,需要的朋友可以參考下。2017-11-11
Centos7.4 zabbix3.4.7源碼安裝的方法步驟
這篇文章主要介紹了Centos7.4 zabbix3.4.7源碼安裝的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
在Bash腳本中創(chuàng)建和使用數(shù)組方法總結(jié)
在本篇文章里小編給大家整理了關(guān)于在Bash腳本中創(chuàng)建和使用數(shù)組方法和相關(guān)知識點(diǎn),需要的朋友們在學(xué)習(xí)下。2019-03-03

