apache的POI是常用的Java開發excel應用的第三方工具
POI常用對象分析
- Workbook - 工作簿
- Sheet - 工作表 文件下的分頁
- Row - 行對象
-
Col - 列對象
(Row,Col) 行列索引 鎖定唯一操作單元格
maven依賴
<!--導入依賴-->
<dependencies>
<!--03 版本 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<!--07版本-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<!--日期格式化工具-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
<!--測試-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
</dependencies>
複雜分表寫excel測試
@Test
public void testWrite07() throws Exception {
// 1.創建工作簿
Workbook workbook = new XSSFWorkbook();
// 2.創建sheet
Sheet userSheet = workbook.createSheet("用戶角色表");
Sheet roleSheet = workbook.createSheet("用戶權限表");
// 第一行
Row userRow1 = userSheet.createRow(0);
userRow1.createCell(0).setCellValue("賬號");
userRow1.createCell(1).setCellValue("用戶名");
userRow1.createCell(2).setCellValue("角色類別");
userRow1.createCell(3).setCellValue("操作時間");
// 測試數據 10 行
for (int i = 1; i < 11; i++){
Row row = userSheet.createRow(i);
row.createCell(0).setCellValue("root" + i);
row.createCell(1).setCellValue("測試用戶" + i);
row.createCell(2).setCellValue((int)(Math.random()*5 + 1));
row.createCell(3).setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
}
// 用戶權限
Row roleRow1 = roleSheet.createRow(0);
roleRow1.createCell(0).setCellValue("權限類別");
roleRow1.createCell(1).setCellValue("權限名稱");
for (int i = 1; i < 6 ; i++){
Row row = roleSheet.createRow(i);
row.createCell(0).setCellValue(i);
row.createCell(1).setCellValue("測試權限" + i);
}
FileOutputStream fileOutputStream = new FileOutputStream(path + "//excel//用戶權限表07.xlsx");
// 生成文件
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("文件生成成功");
}
封裝寫工具類
- 封裝三種工作簿用於寫文件不同場景調用
- HSSF 03版本文件寫 (最多 65535行)
- XSSF 07版本文件寫 無上限
- SXSSF 提升大文件excel寫速度
- 提取文件名、表名、行列內容、路徑、起始行爲參數封裝寫工具類
package com.ht.utils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
/**
*
* @author 掌灬紋
* @since 2021-2-25
*/
public class PoiWriteUtils {
/**
*
* 1. 文件名 自動添加後綴 .xls
* 2. sheetName 分頁命名
* 3. rowBeginIndex 起始行索引 哪行開始存儲 0-第一行
* 4. excel 行標題名稱
* 5. excel 列存放內容
* 6. 文件生成路徑
*/
/**
* 03版本 xls 文件寫
* @param fileName 文件名
* @param sheetName 分頁命名
* @param rowBeginIndex 起始行索引 0-第一行
* @param row 行標題內容
* @param col 列存放內容
* @param path 文件生成路徑
* @return true&false
* @throws IOException
*/
public boolean writeExcelByPoiHSSF(
String fileName, String sheetName,
int rowBeginIndex, List<String> row,
List<List<Object>> col,
String path
) throws IOException {
// 處理文件後綴名 即 路徑
fileName += ".xls";
path += fileName;
// 創建表格
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet(sheetName);
// 起始行
Row row1 = sheet.createRow(rowBeginIndex);
int rowLen = row.size();
for (int i = 0; i < rowLen; i++) {
// 第一行 第幾列 初始化
row1.createCell(i).setCellValue(row.get(i));
}
// 文件內容
// 內容記錄 行數
int colLen = col.size();
for (int i = rowBeginIndex + 1; i < colLen + rowBeginIndex + 1; i++) {
// 多少行內容
Row temp = sheet.createRow(i);
for (int j = 0; j < col.get(i - rowBeginIndex - 1).size(); j++) {
// 每行內容寫入文件
temp.createCell(j).setCellValue(col.get(i - rowBeginIndex - 1).get(j).toString());
}
}
// IO操作
FileOutputStream out = null;
try {
out = new FileOutputStream(path);
workbook.write(out);// 寫文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
out.close();
workbook.close();
}
return true;
}
/**
* 07 版本 xlsx 文件寫
* @param fileName
* @param sheetName
* @param rowBeginIndex
* @param row
* @param col
* @param path
* @return
* @throws IOException
*/
public boolean writeExcelByPoiXHSSF(
String fileName, String sheetName,
int rowBeginIndex, List<String> row,
List<List<Object>> col,
String path
) throws IOException{
// 處理文件後綴名 即 路徑
fileName += ".xlsx";
path += fileName;
// 創建表格
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet(sheetName);
// 起始行
Row row1 = sheet.createRow(rowBeginIndex);
int rowLen = row.size();
for (int i = 0; i < rowLen; i++) {
// 第一行 第幾列 初始化
row1.createCell(i).setCellValue(row.get(i));
}
// 文件內容
// 內容記錄 行數
int colLen = col.size();
for (int i = rowBeginIndex + 1; i < colLen + rowBeginIndex + 1; i++) {
// 多少行內容
Row temp = sheet.createRow(i);
for (int j = 0; j < col.get(i - rowBeginIndex - 1).size(); j++) {
// 每行內容寫入文件
temp.createCell(j).setCellValue(col.get(i - rowBeginIndex - 1).get(j).toString());
}
}
// IO操作
FileOutputStream out = null;
try {
out = new FileOutputStream(path);
workbook.write(out);// 寫文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
out.close();
workbook.close();
}
return true;
}
/**
* 大數據寫 快速寫重構
* @param fileName
* @param sheetName
* @param rowBeginIndex
* @param row
* @param col
* @param path
* @return
*/
public boolean writeExcelByPoiSXSSFBigData(
String fileName, String sheetName,
int rowBeginIndex, List<String> row,
List<List<Object>> col,
String path
) throws IOException{
// 處理文件後綴名 即 路徑
fileName += ".xlsx";
path += fileName;
// 創建表格
Workbook workbook = new SXSSFWorkbook();
Sheet sheet = workbook.createSheet(sheetName);
// 起始行
Row row1 = sheet.createRow(rowBeginIndex);
int rowLen = row.size();
for (int i = 0; i < rowLen; i++) {
// 第一行 第幾列 初始化
row1.createCell(i).setCellValue(row.get(i));
}
// 文件內容
// 內容記錄 行數
int colLen = col.size();
for (int i = rowBeginIndex + 1; i < colLen + rowBeginIndex + 1; i++) {
// 多少行內容
Row temp = sheet.createRow(i);
for (int j = 0; j < col.get(i - rowBeginIndex - 1).size(); j++) {
// 每行內容寫入文件
temp.createCell(j).setCellValue(col.get(i - rowBeginIndex - 1).get(j).toString());
}
}
// IO操作
FileOutputStream out = null;
try {
out = new FileOutputStream(path);
workbook.write(out);// 寫文件
//清空臨時文件
((SXSSFWorkbook)workbook).dispose();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
out.close();
workbook.close();
}
return true;
}
}
工具類測試
package com.ht;
import com.ht.utils.PoiWriteUtils;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
// 測試工具
public class TestUtils {
public static void main(String[] args) throws IOException {
PoiWriteUtils utils = new PoiWriteUtils();
// 數據庫字段
List<String> row = new ArrayList<String>();
row.add("姓名");
row.add("年齡");
row.add("家庭住址");
// 數據庫內容
List<List<Object>> col = new ArrayList();
List<Object> col1 = new ArrayList<Object>();
col1.add("Ht");col1.add("20");col1.add("河北省廊坊市");
List<Object> col2 = new ArrayList<Object>();
col2.add("愛神");col2.add("18");col2.add("河北省唐山市");
col.add(col1);col.add(col2);
// 路徑
String path = "E:\\enviroment\\git\\cupid-study\\poi-easyexcel\\poi\\excel\\";
utils.writeExcelByPoiHSSF("測試", "測試表", 0,row,col,path);
}
@Test
public void testBigData() throws IOException {
PoiWriteUtils utils = new PoiWriteUtils();
// 大數據 寫入
List<String> row = new ArrayList<String>();
row.add("姓名");
row.add("年齡");
row.add("家庭住址");
// 文件內容 按集合存記錄
List<List<Object>> col = new ArrayList();
for (int i = 0; i < 100000; i++){
List<Object> temp = new ArrayList<Object>();
temp.add("測試姓名" + i + 1);temp.add(i + 1);temp.add("測試地址" + i + 1);
col.add(temp);
}
String path = "E:\\enviroment\\git\\cupid-study\\poi-easyexcel\\poi\\excel\\";
utils.writeExcelByPoiSXSSFBigData("大數據測試","大數據測試",0,row,col,path);
System.out.println("文件生成成功!");
}
}