Java使用apache?poi操作excel的方式
一.基本介紹
1.1、Apache POI介紹
Apache POI是一個(gè)可以進(jìn)行微軟的文檔進(jìn)行開源庫,可以操作的文檔類型包括word、ppt、excel、visio、outlook....
本文主要針對(duì)Apache POI對(duì)excel的操作進(jìn)行介紹,主要包括如何創(chuàng)建一個(gè)excel、錄入數(shù)據(jù)、讀取excel數(shù)據(jù)的方式。
參考官方文檔:
1.2、HSSF和XSSF
在POI中,如果要操作Excel,可以有兩種操作“模式”,分別是HSSF和XSSF,其中HSSF可以處理win97以及低于2007的excel,而XSSF可以用于處理2007以及更高的版本excel,所以我們一般都會(huì)使用XSSF。
有一個(gè)很重要的術(shù)語:工作薄,其實(shí)可以理解為“文檔”,excel、word,都屬于工作簿,英文workbook。
1.3、引入依賴
只需要導(dǎo)入poi-ooxml這個(gè)庫即可。
<dependencies> <!-- poi庫 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <!-- 示例以單元測(cè)試的形式,所以需要導(dǎo)入junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> </dependency> </dependencies>
二.利用poi庫創(chuàng)建excel
2.1、創(chuàng)建一個(gè)空excel
下面的代碼運(yùn)行后
package cn.ganlixin.poi; import org.apache.poi.xssf.usermodel.XSSFCell; 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.*; public class CreateExcelTest { /** * 創(chuàng)建一個(gè)空的Excel文件 */ @Test public void testCreateEmptyExcel() throws IOException { // 創(chuàng)建文件 BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("data.xlsx")); // 創(chuàng)建工作簿類(所有要寫入excel的數(shù)據(jù),都將保存在workbook中) XSSFWorkbook workbook = new XSSFWorkbook(); // 將workbook中的數(shù)據(jù)寫入到文件中。 workbook.write(outputStream); // 關(guān)閉 workbook.close(); outputStream.close(); } }
2.2、簡(jiǎn)單演示寫入excel內(nèi)容
下面演示創(chuàng)建一個(gè)data.xlsx,并在“my-sheet-1”sheet內(nèi)的第5行第3列寫入一個(gè)“hello world”:
package cn.ganlixin.poi; import org.apache.poi.xssf.usermodel.XSSFCell; 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.*; public class CreateExcelTest { /** * 簡(jiǎn)單演示如何寫入數(shù)據(jù)內(nèi)容 */ @Test public void testCreateSimpleWorkbook() throws IOException { // 指定創(chuàng)建的excel文件名稱 BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("data.xlsx")); // 定義一個(gè)工作?。ㄋ幸獙懭雃xcel的數(shù)據(jù),都將保存在workbook中) XSSFWorkbook workbook = new XSSFWorkbook(); // 創(chuàng)建一個(gè)sheet XSSFSheet sheet = workbook.createSheet("my-sheet-1"); // 開始寫入數(shù)據(jù)流程,2大步:1、定位到單元格,2、寫入數(shù)據(jù);定位單元格,需要通過行、列配合指定。 // step1: 先選擇第幾行(0表示第一行),下面表示在第6行 XSSFRow row = sheet.createRow(5); // step2:選擇第幾列(0表示第一列),注意,這里的第幾列,是在上面指定的row基礎(chǔ)上,也就是第6行,第3列 XSSFCell cell = row.createCell(2); // step3:設(shè)置單元格的數(shù)據(jù)(寫入數(shù)據(jù)) cell.setCellValue("hello world"); // 執(zhí)行寫入操作 workbook.write(outputStream); workbook.close(); outputStream.flush(); outputStream.close(); } }
運(yùn)行后,下面是生成的excel:
2.3、通常的寫入數(shù)據(jù)流程
一般寫入excel的數(shù)據(jù)都是結(jié)構(gòu)化的(類似于數(shù)據(jù)庫表的結(jié)構(gòu)),下面是一個(gè)示例,創(chuàng)建一個(gè)users.xlsx文檔:
package cn.ganlixin.poi; import org.apache.poi.xssf.usermodel.XSSFCell; 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.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class CreateExcelTest { private static class User { private Integer id; private String name; private Integer age; private String addr; // 避免占用篇幅,所以省略了構(gòu)造方法、getter、setter } /** * 寫入結(jié)構(gòu)化的用戶信息數(shù)據(jù) */ @Test public void testCreateUsersExcel() throws IOException { List<User> userList = new ArrayList<>(); userList.add(new User(1, "abc", 99, "北京")); userList.add(new User(2, "lol", 77, "上海")); userList.add(new User(3, "qaq", 88, "深圳")); userList.add(new User(4, "owo", 66, "杭州")); // 指定創(chuàng)建的excel文件名稱 BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("users.xlsx")); // 定義一個(gè)工作薄(所有要寫入excel的數(shù)據(jù),都將保存在workbook中) XSSFWorkbook workbook = new XSSFWorkbook(); // 創(chuàng)建一個(gè)sheet XSSFSheet sheet = workbook.createSheet("my-sheet"); // 第一行,為表頭,需要單獨(dú)寫入,下面是錯(cuò)誤方式,因?yàn)樵诜磸?fù)的創(chuàng)建第1行(rowNumber為0) /* sheet.createRow(0).createCell(0).setCellValue("編號(hào)"); sheet.createRow(0).createCell(1).setCellValue("姓名"); sheet.createRow(0).createCell(2).setCellValue("年齡"); sheet.createRow(0).createCell(3).setCellValue("城市"); */ // 正確方式 XSSFRow head = sheet.createRow(0); head.createCell(0).setCellValue("編號(hào)"); head.createCell(1).setCellValue("姓名"); head.createCell(2).setCellValue("年齡"); head.createCell(3).setCellValue("城市"); // 接下來遍歷要錄入的數(shù)據(jù)(建議使用for,并且從第2行開始,也就是rowNumber為1,因?yàn)楸眍^占了一行) for (int rowNumber = 1, index = 0; index < userList.size(); index++, rowNumber++) { User user = userList.get(index); // 寫入數(shù)據(jù)流程,1、定位到單元格,2、寫入數(shù)據(jù);定位單元格,需要通過行、列配合指定。 XSSFRow row = sheet.createRow(rowNumber); row.createCell(0).setCellValue(user.getId()); row.createCell(1).setCellValue(user.getName()); row.createCell(2).setCellValue(user.getAge()); row.createCell(3).setCellValue(user.getAddr()); } // 執(zhí)行寫入操作 workbook.write(outputStream); workbook.close(); outputStream.flush(); outputStream.close(); } }
生成的excel如下:
三.使用POI讀取Excel內(nèi)容
3.1、讀取excel示例
下面針對(duì)上面創(chuàng)建的users.xlsx進(jìn)行讀取并打印
package cn.ganlixin.poi; 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.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; public class ReadExcelTest { @Test public void testReadUsersExcel() throws IOException { // 指定excel文件,創(chuàng)建緩存輸入流 BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("users.xlsx")); // 直接傳入輸入流即可,此時(shí)excel就已經(jīng)解析了 XSSFWorkbook workbook = new XSSFWorkbook(inputStream); // 選擇要處理的sheet名稱 XSSFSheet sheet = workbook.getSheet("my-sheet"); // 第一行表頭,單獨(dú)處理 // 迭代遍歷sheet剩余的每一行 for (int rowNum = 0; rowNum < sheet.getPhysicalNumberOfRows(); rowNum++) { if (rowNum == 0) { // 讀取第一行(表頭) XSSFRow head = sheet.getRow(rowNum); String headColumn_1 = head.getCell(0).getStringCellValue(); String headColumn_2 = head.getCell(1).getStringCellValue(); String headColumn_3 = head.getCell(2).getStringCellValue(); String headColumn_4 = head.getCell(3).getStringCellValue(); String headStr = String.format("%s\t%s\t%s\t%s", headColumn_1, headColumn_2, headColumn_3, headColumn_4); System.out.println(headStr); } else { // 非表頭(注意讀取的時(shí)候要注意單元格內(nèi)數(shù)據(jù)的格式,要使用正確的讀取方法) XSSFRow row = sheet.getRow(rowNum); int id = (int) row.getCell(0).getNumericCellValue(); String name = row.getCell(1).getStringCellValue(); int age = (int) row.getCell(2).getNumericCellValue(); String addr = row.getCell(3).getStringCellValue(); String rowContent = String.format("%s\t%s\t%s\t%s", id, name, age, addr); System.out.println(rowContent); } } workbook.close(); inputStream.close(); } }
運(yùn)行輸出如下:
到此這篇關(guān)于Java使用apache poi進(jìn)行excel相關(guān)操作的文章就介紹到這了,更多相關(guān)java apache poi操作excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA 統(tǒng)計(jì)字符串中中文,英文,數(shù)字,空格,特殊字符的個(gè)數(shù)
這篇文章主要介紹了JAVA 統(tǒng)計(jì)字符串中中文,英文,數(shù)字,空格,特殊字符的個(gè)數(shù) ,本文通過一段代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06Java Arrays.sort和Collections.sort排序?qū)崿F(xiàn)原理解析
這篇文章主要介紹了Java Arrays.sort和Collections.sort排序?qū)崿F(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Elasticsearch查詢及聚合類DSL語句寶典示例詳解
這篇文章主要為大家介紹了Elasticsearch查詢及聚合類DSL語句寶典示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01JAVA匿名內(nèi)部類(Anonymous Classes)的具體使用
本文主要介紹了JAVA匿名內(nèi)部類,匿名內(nèi)部類在我們JAVA程序員的日常工作中經(jīng)常要用到,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Java實(shí)現(xiàn)較大二進(jìn)制文件的讀、寫方法
本篇文章主要介紹了Java實(shí)現(xiàn)較大二進(jìn)制文件的讀、寫方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(34)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07SpringBoot實(shí)現(xiàn)多端口監(jiān)聽的代碼示例
當(dāng)你需要在同一個(gè)Spring Boot應(yīng)用中,通過不同的端口來提供不同的服務(wù)或功能時(shí),就需要實(shí)現(xiàn)多端口監(jiān)聽,所以本文給大家介紹了SpringBoot實(shí)現(xiàn)多端口監(jiān)聽的方法示例,并有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2024-09-09