Apache POI
POI提供API給JAVA程序?qū)icrosoft Office格式檔案讀和寫的功能
基本功能如下:
HSSF – 提供讀寫Excel格式(03)xls文件
XSSF – 提供讀寫Excel OOXML格式(07)xlsx文件
HWPF – 提供讀寫Word格式
HSLF – 提供讀寫PowerPoint格式
HDGF – 提供讀寫Visio格式
【注】03版本最多65535行,07版本的沒有限制
POI - Excel寫
步驟
1、導(dǎo)入依賴
<!-- excel工具 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<!-- 日期格式化工具 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.1</version>
</dependency>
2、編寫代碼
4個主要步驟:創(chuàng)建工作簿 – 創(chuàng)建工作表 – 創(chuàng)建行 – 創(chuàng)建列
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
/**
* @author xxms
*/
public class ExcelWriteTest
{
public static void main(String[] args) throws Exception {
POIExcel03();
POIExcel07();
}
public static void POIExcel03() throws Exception {
String PATH = "D:\\Study\\Back-end\\";
// 1、創(chuàng)建一個工作簿 03
Workbook workbook = new HSSFWorkbook();
// 2、創(chuàng)建一個工作表
Sheet sheet = workbook.createSheet("xxms觀眾統(tǒng)計表");
// 3、創(chuàng)建一個行
Row row1 = sheet.createRow(0);
// 4、創(chuàng)建一個單元格 (1,1)
Cell cell11 = row1.createCell(0);
cell11.setCellValue("今日新增觀眾");
// (1,2)
Cell cell12 = row1.createCell(1);
cell12.setCellValue(666);
// 第二行
Row row2 = sheet.createRow(1);
//(2,1)
Cell cell21 = row2.createCell(0);
cell21.setCellValue("統(tǒng)計時間");
// (2,2)
Cell cell22 = row2.createCell(1);
String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
cell22.setCellValue(time);
// 生成一張表(IO流) 03版本就是使用xls結(jié)尾
FileOutputStream fileOutputStream = new FileOutputStream(PATH+"xxms觀眾統(tǒng)計表03.xls");
workbook.write(fileOutputStream);
// 關(guān)閉流
fileOutputStream.close();
System.out.println("xxms觀眾統(tǒng)計表03 生成完畢");
}
public static void POIExcel07() throws Exception {
String PATH = "D:\\Study\\Back-end\\";
// 1、創(chuàng)建一個工作簿 07
Workbook workbook = new XSSFWorkbook();
// 2、創(chuàng)建一個工作表
Sheet sheet = workbook.createSheet("xxms觀眾統(tǒng)計表");
// 3、創(chuàng)建一個行
Row row1 = sheet.createRow(0);
// 4、創(chuàng)建一個單元格 (1,1)
Cell cell11 = row1.createCell(0);
cell11.setCellValue("今日新增觀眾");
// (1,2)
Cell cell12 = row1.createCell(1);
cell12.setCellValue(666);
// 第二行
Row row2 = sheet.createRow(1);
//(2,1)
Cell cell21 = row2.createCell(0);
cell21.setCellValue("統(tǒng)計時間");
// (2,2)
Cell cell22 = row2.createCell(1);
String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
cell22.setCellValue(time);
// 生成一張表(IO流) 07版本就是使用xlsx結(jié)尾
FileOutputStream fileOutputStream = new FileOutputStream(PATH+"xxms觀眾統(tǒng)計表07.xlsx");
workbook.write(fileOutputStream);
// 關(guān)閉流
fileOutputStream.close();
System.out.println("xxms觀眾統(tǒng)計表07 生成完畢");
}
}
大數(shù)據(jù)量導(dǎo)入
package com.ruoyi.common.core.controller;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
/**
*
*
* @author xxms
*/
public class ExcelWriteBigDataTest
{
public static void main(String[] args) throws Exception {
POIExcel03BigData();
POIExcel07BigData();
}
public static void POIExcel03BigData() throws Exception {
//時間
long begin = System.currentTimeMillis();
String PATH = "D:\\Study\\Back-end\\";
// 1、創(chuàng)建一個工作簿 03
Workbook workbook = new HSSFWorkbook();
// 2、創(chuàng)建一個工作表
Sheet sheet = workbook.createSheet("xxms觀眾統(tǒng)計表");
// 3、寫入數(shù)據(jù)
for(int rowNum=0;rowNum< 65536;rowNum++) {
Row row = sheet.createRow(rowNum);
for(int cellNum =0;cellNum<10;cellNum++) {
Cell cell = row.createCell(cellNum);
cell.setCellValue(cellNum);
}
}
// 生成一張表(IO流) 03版本就是使用xls結(jié)尾
FileOutputStream fileOutputStream = new FileOutputStream(PATH+"xxms觀眾統(tǒng)計表03BigData.xls");
workbook.write(fileOutputStream);
// 關(guān)閉流
fileOutputStream.close();
long end = System.currentTimeMillis();
System.out.println("xxms觀眾統(tǒng)計表03BigData 生成完畢,時間消耗:"+(double)(end-begin));
}
public static void POIExcel07BigData() throws Exception {
long begin = System.currentTimeMillis();
String PATH = "D:\\Study\\Back-end\\";
// 1、創(chuàng)建一個工作簿 07
Workbook workbook = new XSSFWorkbook();
// 2、創(chuàng)建一個工作表
Sheet sheet = workbook.createSheet("xxms觀眾統(tǒng)計表");
// 3、寫入數(shù)據(jù)
for(int rowNum=0;rowNum< 65536;rowNum++) {
Row row = sheet.createRow(rowNum);
for(int cellNum =0;cellNum<10;cellNum++) {
Cell cell = row.createCell(cellNum);
cell.setCellValue(cellNum);
}
}
// 生成一張表(IO流) 07版本就是使用xlsx結(jié)尾
FileOutputStream fileOutputStream = new FileOutputStream(PATH+"xxms觀眾統(tǒng)計表07BigData.xlsx");
workbook.write(fileOutputStream);
// 關(guān)閉流
fileOutputStream.close();
long end = System.currentTimeMillis();
System.out.println("xxms觀眾統(tǒng)計表07BigData 生成完畢,時間消耗:"+(double)(end-begin));
}
}
時間消耗如下:
xxms觀眾統(tǒng)計表03BigData 生成完畢,時間消耗:847.0
xxms觀眾統(tǒng)計表07BigData 生成完畢,時間消耗:3048.0
HSSF僅能保存65535行數(shù)據(jù),XSSF無限制但速度比較慢,因此可以使用優(yōu)化后的XSSF,即SXSSF(可以寫非常大的數(shù)據(jù)量,如100萬條甚至更多條,寫數(shù)據(jù)速度快,占用更少的內(nèi)存)
【注】會產(chǎn)生臨時文件,需要清理臨時文件(fileOutputStream.close();)(默認(rèn)由100條記錄被保存在內(nèi)存中,如果超過這數(shù)量,則最前面的數(shù)據(jù)被寫入臨時文件,如果向自定義內(nèi)存中數(shù)據(jù)的數(shù)量,可以使用new SXSSFWorkbook(數(shù)量)
public static void POIExcel07BigDataS() throws Exception {
long begin = System.currentTimeMillis();
String PATH = "D:\\Study\\Back-end\\";
// 1、創(chuàng)建一個工作簿 07
Workbook workbook = new SXSSFWorkbook();
// 2、創(chuàng)建一個工作表
Sheet sheet = workbook.createSheet("xxms觀眾統(tǒng)計表");
// 3、寫入數(shù)據(jù)
for (int rowNum = 0; rowNum < 65536; rowNum++) {
Row row = sheet.createRow(rowNum);
for (int cellNum = 0; cellNum < 10; cellNum++) {
Cell cell = row.createCell(cellNum);
cell.setCellValue(cellNum);
}
}
// 生成一張表(IO流) 07版本就是使用xlsx結(jié)尾
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "xxms觀眾統(tǒng)計表07BigDataS.xlsx");
workbook.write(fileOutputStream);
// 關(guān)閉流
fileOutputStream.close();
// 清除臨時文件
((SXSSFWorkbook) workbook).dispose();
long end = System.currentTimeMillis();
System.out.println("xxms觀眾統(tǒng)計表07BigDataS 生成完畢,時間消耗:" + (double) (end - begin));
}
時間對比如下:
xxms觀眾統(tǒng)計表07BigData 生成完畢,時間消耗:2697.0
xxms觀眾統(tǒng)計表07BigDataS 生成完畢,時間消耗:806.0
POI - Excel讀
編寫代碼
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.XSSFWorkbook;
public class ExcelReadTest {
public static void main(String[] args) throws Exception {
POIExcel03Read();
POIExcel07Read();
}
public static void POIExcel03Read() throws Exception {
String PATH = "D:\\Study\\Back-end\\";
// 獲取文件流
FileInputStream inputStream = new FileInputStream(PATH + "xxms觀眾統(tǒng)計表03.xls");
// 1、創(chuàng)建一個工作簿。使用excel能操作的這邊他都可以操作
Workbook workbook = new HSSFWorkbook(inputStream);
// 2、得到表
Sheet sheet = workbook.getSheetAt(0);
// 3、得到行
Row row = sheet.getRow(0);
// 4、得到列
Cell cell = row.getCell(0);
// 獲取字符串類型
String value = cell.getStringCellValue();
System.out.println("03版本的值:" + value);
inputStream.close();
}
public static void POIExcel07Read() throws Exception {
String PATH = "D:\\Study\\Back-end\\";
// 獲取文件流
FileInputStream inputStream = new FileInputStream(PATH + "xxms觀眾統(tǒng)計表07.xlsx");
// 1、創(chuàng)建一個工作簿。使用excel能操作的這邊他都可以操作
Workbook workbook = new XSSFWorkbook(inputStream);
// 2、得到表
Sheet sheet = workbook.getSheetAt(0);
// 3、得到行
Row row = sheet.getRow(0);
// 4、得到列
Cell cell = row.getCell(0);
// 獲取字符串類型
String value = cell.getStringCellValue();
System.out.println("07版本的值:" + value);
inputStream.close();
}
}
讀取Excel中不同類型的數(shù)據(jù)
public static void POIExcelReadTestCellType() throws Exception {
String PATH = "D:\\Study\\Back-end\\";
// 獲取文件流
FileInputStream inputStream = new FileInputStream(PATH + "明細(xì)表.xlsx");
// 1、創(chuàng)建一個工作簿。使用excel能操作的這邊他都可以操作
Workbook workbook = new XSSFWorkbook(inputStream);
// 2、得到表
Sheet sheet = workbook.getSheetAt(0);
// 獲取標(biāo)題內(nèi)容
Row rowTitle = sheet.getRow(0);
if(rowTitle!=null) {
// 得到該行有多少列(一定要掌握)
int cellCount = rowTitle.getPhysicalNumberOfCells();
for(int cellNum = 0;cellNum<cellCount;cellNum++) {
Cell cell = rowTitle.getCell(cellNum);
if(cell!=null) {
// 獲取該單元格存儲的數(shù)據(jù)是什么類型的
CellType cellType = cell.getCellType();
String cellValue = cell.getStringCellValue();
System.out.print(cellValue +"|");
}
}
System.out.println();
}
// 獲取表中的內(nèi)容
// int rowCount = sheet.getLastRowNum() + 1; (范圍更大)
int rowCount = sheet.getPhysicalNumberOfRows();
for(int rowNum = 1;rowNum<rowCount;rowNum++) {
Row rowData = sheet.getRow(rowNum);
if(rowData!=null) {
// 得到該行有多少列(一定要掌握)
int cellCount = rowTitle.getPhysicalNumberOfCells();
for(int cellNum = 0;cellNum<cellCount;cellNum++) {
System.out.print("["+(rowNum+1)+"-"+(cellNum+1)+"]");
Cell cell = rowData.getCell(cellNum);
if(cell!=null) {
// 獲取該單元格存儲的數(shù)據(jù)是什么類型的
CellType cellType = cell.getCellType();
String cellValue= "";
switch(cellType) {
case STRING: //字符串
System.out.print("【STRING】");
cellValue = cell.getStringCellValue();
break;
case BOOLEAN: //布爾
System.out.print("【BOOLEAN】");
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case BLANK: //空
System.out.print("【BLANK】");
cellValue = cell.getStringCellValue();
break;
case NUMERIC: //數(shù)字(日期、普通數(shù)字)
System.out.print("【NUMERIC】");
if(DateUtil.isCellDateFormatted(cell)) { // 日期
System.out.print("【日期】");
Date date = cell.getDateCellValue();
cellValue = new DateTime(date).toString("yyyy-MM-dd HH:mm:ss");
}else {
//不是日期格式,防止數(shù)字過長!
System.out.print("【轉(zhuǎn)換為字符串輸出】");
//這種用BigDecimal包裝再獲取plainString,可以防止獲取到科學(xué)計數(shù)值
BigDecimal bd = new BigDecimal(cell.getNumericCellValue());
cellValue = bd.toPlainString();
// 方法過期了
// cell.setCellType(CellType.STRING);
// cellValue = cell.toString();
}
break;
case FORMULA: // 公式
cellValue = cell.getCellFormula();
case ERROR: // 故障
cellValue = "ERROR VALUE";
break;
default:
cellValue = "UNKNOW VALUE";
System.out.println("【數(shù)據(jù)類型錯誤】");
break;
}
System.out.print(cellValue);
}
System.out.println();
}
}
}
inputStream.close();
}
輸出如下:文章來源:http://www.zghlxwxcb.cn/news/detail-459994.html
序號|卡號|持卡人|手機(jī)號|消費(fèi)日期|小票號|商品編號|商品條碼|商品名稱|商品單位|原價|銷售價|銷售數(shù)量|銷售金額|優(yōu)惠金額|是否上架|
[2-1]【NUMERIC】【轉(zhuǎn)換為字符串輸出】1
[2-2]【NUMERIC】【轉(zhuǎn)換為字符串輸出】100088
[2-3]【STRING】張三
[2-4]【NUMERIC】【轉(zhuǎn)換為字符串輸出】1233333333333
[2-5]【NUMERIC】【日期】2020-04-21 00:00:00
[2-6]【NUMERIC】【轉(zhuǎn)換為字符串輸出】2392392093
[2-7]【STRING】PV92038038
[2-8]【STRING】PV98298392
[2-9]【STRING】蒙牛
[2-10]【STRING】瓶
[2-11]【NUMERIC】【轉(zhuǎn)換為字符串輸出】200.5
[2-12]【NUMERIC】【轉(zhuǎn)換為字符串輸出】1000
[2-13]【NUMERIC】【轉(zhuǎn)換為字符串輸出】1
[2-14]【NUMERIC】【轉(zhuǎn)換為字符串輸出】900
[2-15]【NUMERIC】【轉(zhuǎn)換為字符串輸出】100
[2-16]【BOOLEAN】true
。。。。。
計算公式
表格內(nèi)容如下:
代碼如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-459994.html
public static void POIExcelTestFormula() throws Exception{
String PATH = "D:\\Study\\Back-end\\";
// 獲取文件流
FileInputStream inputStream = new FileInputStream(PATH + "公式.xlsx");
// 1、創(chuàng)建一個工作簿。使用excel能操作的這邊他都可以操作
Workbook workbook = new XSSFWorkbook(inputStream);
// 2、得到表
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(4);
Cell cell = row.getCell(0);
//拿到計算公式 eval
XSSFFormulaEvaluator xssfFormulaEvaluator = new XSSFFormulaEvaluator((XSSFWorkbook)workbook);
//輸出單元格的內(nèi)容
String cellFormula = cell.getCellFormula();
System.out.println(cellFormula); //SUM(A2,A3,A4)
//計算
CellValue evaluate = xssfFormulaEvaluator.evaluate(cell);
String cellValue = evaluate.formatAsString();
System.out.println(cellValue);//600.0
}
到了這里,關(guān)于JAVA操作Excel之POI的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!