java excel 操作 poi_Java使用apache poi进行excel相关操作
一.基本介紹
1.1、Apache POI介紹
Apache POI是一個可以進行微軟的文檔進行開源庫,可以操作的文檔類型包括word、ppt、excel、visio、outlook....
本文主要針對Apache POI對excel的操作進行介紹,主要包括如何創(chuàng)建一個excel、錄入數(shù)據(jù)、讀取excel數(shù)據(jù)的方式。
參考官方文檔:
1.2、HSSF和XSSF
在POI中,如果要操作Excel,可以有兩種操作“模式”,分別是HSSF和XSSF,其中HSSF可以處理win97以及低于2007的excel,而XSSF可以用于處理2007以及更高的版本excel,所以我們一般都會使用XSSF。
有一個很重要的術語:工作薄,其實可以理解為“文檔”,excel、word,都屬于工作簿,英文workbook。
1.3、引入依賴
只需要導入poi-ooxml這個庫即可。
org.apache.poi
poi-ooxml
4.1.2
junit
junit
4.13
二.利用poi庫創(chuàng)建excel
2.1、創(chuàng)建一個空excel
下面的代碼運行后
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)建一個空的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);
// 關閉
workbook.close();
outputStream.close();
}
}
2.2、簡單演示寫入excel內(nèi)容
下面演示創(chuàng)建一個data.xlsx,并在“my-sheet-1”sheet內(nèi)的第5行第3列寫入一個“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 {
/**
* 簡單演示如何寫入數(shù)據(jù)內(nèi)容
*/
@Test
public void testCreateSimpleWorkbook() throws IOException {
// 指定創(chuàng)建的excel文件名稱
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("data.xlsx"));
// 定義一個工作薄(所有要寫入excel的數(shù)據(jù),都將保存在workbook中)
XSSFWorkbook workbook = new XSSFWorkbook();
// 創(chuàng)建一個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基礎上,也就是第6行,第3列
XSSFCell cell = row.createCell(2);
// step3:設置單元格的數(shù)據(jù)(寫入數(shù)據(jù))
cell.setCellValue("hello world");
// 執(zhí)行寫入操作
workbook.write(outputStream);
workbook.close();
outputStream.flush();
outputStream.close();
}
}
運行后,下面是生成的excel:
2.3、通常的寫入數(shù)據(jù)流程
一般寫入excel的數(shù)據(jù)都是結(jié)構(gòu)化的(類似于數(shù)據(jù)庫表的結(jié)構(gòu)),下面是一個示例,創(chuàng)建一個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 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"));
// 定義一個工作薄(所有要寫入excel的數(shù)據(jù),都將保存在workbook中)
XSSFWorkbook workbook = new XSSFWorkbook();
// 創(chuàng)建一個sheet
XSSFSheet sheet = workbook.createSheet("my-sheet");
// 第一行,為表頭,需要單獨寫入,下面是錯誤方式,因為在反復的創(chuàng)建第1行(rowNumber為0)
/*
sheet.createRow(0).createCell(0).setCellValue("編號");
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("編號");
head.createCell(1).setCellValue("姓名");
head.createCell(2).setCellValue("年齡");
head.createCell(3).setCellValue("城市");
// 接下來遍歷要錄入的數(shù)據(jù)(建議使用for,并且從第2行開始,也就是rowNumber為1,因為表頭占了一行)
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示例
下面針對上面創(chuàng)建的users.xlsx進行讀取并打印
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"));
// 直接傳入輸入流即可,此時excel就已經(jīng)解析了
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
// 選擇要處理的sheet名稱
XSSFSheet sheet = workbook.getSheet("my-sheet");
// 第一行表頭,單獨處理
// 迭代遍歷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 { // 非表頭(注意讀取的時候要注意單元格內(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();
}
}
運行輸出如下:
總結(jié)
以上是生活随笔為你收集整理的java excel 操作 poi_Java使用apache poi进行excel相关操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 万顺叫车必上市的原因 万顺叫车是否会上市
- 下一篇: 邮政储蓄银行最新存款利率表2022,有以