转自狂神,作为个人笔记使用
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
Apache POI 官网:https://poi.apache.org/
pom依赖
<dependencies>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poiartifactId>
<version>3.9version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxmlartifactId>
<version>3.9version>
dependency>
<dependency>
<groupId>joda-timegroupId>
<artifactId>joda-timeartifactId>
<version>2.10.1version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
03版本Excel:
@Test
public void testWrite03() throws Exception {
// 1、创建一个工作簿
Workbook workbook = new HSSFWorkbook();
// 2、创建一个工作表
Sheet sheet = workbook.createSheet("狂神观众统计表");
// 3、创建一个行 (1,1)
Row row1 = sheet.createRow(0);
// 4、创建一个单元格
Cell cell11 = row1.createCell(0);
cell11.setCellValue("今日新增观众");
// (1,2)
Cell cell12 = row1.createCell(1);
cell12.setCellValue(666);
// 第二行 (2,1)
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
cell21.setCellValue("统计时间");
// (2,2)
Cell cell22 = row2.createCell(1);
String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
cell22.setCellValue(time);
// 生成一张表(IO 流) 03 版本就是使用 xls结尾!
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "狂神观众统计表03.xls");
// 输出
workbook.write(fileOutputStream);
// 关闭流
fileOutputStream.close();
System.out.println("狂神观众统计表03 生成完毕!");
}
07版本Excel:
@Test
public void testWrite07() throws Exception {
// 1、创建一个工作簿 07
Workbook workbook = new XSSFWorkbook();
// 2、创建一个工作表
Sheet sheet = workbook.createSheet("狂神观众统计表");
// 3、创建一个行 (1,1)
Row row1 = sheet.createRow(0);
// 4、创建一个单元格
Cell cell11 = row1.createCell(0);
cell11.setCellValue("今日新增观众");
// (1,2)
Cell cell12 = row1.createCell(1);
cell12.setCellValue(666);
// 第二行 (2,1)
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
cell21.setCellValue("统计时间");
// (2,2)
Cell cell22 = row2.createCell(1);
String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
cell22.setCellValue(time);
// 生成一张表(IO 流) 03 版本就是使用 xlsx结尾!
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "狂神观众统计表07.xlsx");
// 输出
workbook.write(fileOutputStream);
// 关闭流
fileOutputStream.close();
System.out.println("狂神观众统计表03 生成完毕!");
}
03版本写多行数据:大文件写HSSF
@Test
public void testWrite03BigData() throws IOException {
// 时间
long begin = System.currentTimeMillis();
// 创建一个薄
Workbook workbook = new HSSFWorkbook();
// 创建表
Sheet sheet = workbook.createSheet();
// 写入数据
for (int rowNum = 0; rowNum < 65537; rowNum++) {
Row row = sheet.createRow(rowNum);
for (int cellNum = 0; cellNum < 10 ; cellNum++) {
Cell cell = row.createCell(cellNum);
cell.setCellValue(cellNum);
}
}
System.out.println("over");
FileOutputStream outputStream = new FileOutputStream(PATH + "testWrite03BigData.xls");
workbook.write(outputStream);
outputStream.close();
long end = System.currentTimeMillis();
System.out.println((double) (end-begin)/1000);
}
07版本写多行数据:大文件写XSSF
@Test
public void testWrite07BigData() throws IOException {
// 时间
long begin = System.currentTimeMillis();
// 创建一个薄
Workbook workbook = new XSSFWorkbook();
// 创建表
Sheet sheet = workbook.createSheet();
// 写入数据
for (int rowNum = 0; rowNum < 100000; rowNum++) {
Row row = sheet.createRow(rowNum);
for (int cellNum = 0; cellNum < 10 ; cellNum++) {
Cell cell = row.createCell(cellNum);
cell.setCellValue(cellNum);
}
}
System.out.println("over");
FileOutputStream outputStream = new FileOutputStream(PATH + "testWrite07BigData.xlsx");
workbook.write(outputStream);
outputStream.close();
long end = System.currentTimeMillis();
System.out.println((double) (end-begin)/1000);
}
07版升级版写多行数据:大文件写SXSSF
@Test
public void testWrite07BigDataS() throws IOException {
// 时间
long begin = System.currentTimeMillis();
// 创建一个薄
Workbook workbook = new SXSSFWorkbook();
// 创建表
Sheet sheet = workbook.createSheet();
// 写入数据
for (int rowNum = 0; rowNum < 100000; rowNum++) {
Row row = sheet.createRow(rowNum);
for (int cellNum = 0; cellNum < 10 ; cellNum++) {
Cell cell = row.createCell(cellNum);
cell.setCellValue(cellNum);
}
}
System.out.println("over");
FileOutputStream outputStream = new FileOutputStream(PATH + "testWrite07BigDataS.xlsx");
workbook.write(outputStream);
outputStream.close();
// 清除临时文件!
((SXSSFWorkbook) workbook).dispose();
long end = System.currentTimeMillis();
System.out.println((double) (end-begin)/1000);
}
注意获取值的类型
03版本读:
@Test
public void testRead03() throws Exception {
// 获取文件流
FileInputStream inputStream = new FileInputStream(PATH + "kuang-poi狂神观众统计表03.xls");
// 1、创建一个工作簿。 使用excel能操作的这边他都可以操作!
Workbook workbook = new HSSFWorkbook(inputStream);
// 2、得到表
Sheet sheet = workbook.getSheetAt(0);
// 3、得到行
Row row = sheet.getRow(0);
// 4、得到列
Cell cell = row.getCell(1);
// 读取值的时候,一定需要注意类型!
// getStringCellValue 字符串类型
// System.out.println(cell.getStringCellValue());
System.out.println(cell.getNumericCellValue());
inputStream.close();
}
07版本读:
@Test
public void testRead07() throws Exception {
// 获取文件流
FileInputStream inputStream = new FileInputStream(PATH + "kuang-poi狂神观众统计表07.xlsx");
// 1、创建一个工作簿。 使用excel能操作的这边他都可以操作!
Workbook workbook = new XSSFWorkbook(inputStream);
// 2、得到表
Sheet sheet = workbook.getSheetAt(0);
// 3、得到行
Row row = sheet.getRow(0);
// 4、得到列
Cell cell = row.getCell(1);
// 读取值的时候,一定需要注意类型!
// getStringCellValue 字符串类型
// System.out.println(cell.getStringCellValue());
System.out.println(cell.getNumericCellValue());
inputStream.close();
}
读取不同的数据类型:
@Test
public void testCellType() throws Exception {
// 获取文件流
FileInputStream inputStream = new FileInputStream(PATH + "明细表.xls");
// 创建一个工作簿。 使用excel能操作的这边他都可以操作!
Workbook workbook = new HSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0);
// 获取标题内容
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){
int cellType = cell.getCellType();
String cellValue = cell.getStringCellValue();
System.out.print(cellValue + " | ");
}
}
System.out.println();
}
// 获取表中的内容
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) {
int cellType = cell.getCellType();
String cellValue = "";
switch (cellType) {
case HSSFCell.CELL_TYPE_STRING: // 字符串
System.out.print("【String】");
cellValue = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // 布尔
System.out.print("【BOOLEAN】");
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK: // 空
System.out.print("【BLANK】");
break;
case HSSFCell.CELL_TYPE_NUMERIC: // 数字(日期、普通数字)
System.out.print("【NUMERIC】");
if (HSSFDateUtil.isCellDateFormatted(cell)){ // 日期
System.out.print("【日期】");
Date date = cell.getDateCellValue();
cellValue = new DateTime(date).toString("yyyy-MM-dd");
}else {
// 不是日期格式,防止数字过长!
System.out.print("【转换为字符串输出】");
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cellValue = cell.toString();
}
break;
case HSSFCell.CELL_TYPE_ERROR:
System.out.print("【数据类型错误】");
break;
}
System.out.println(cellValue);
}
}
}
}
inputStream.close();
}
@Test
public void testFormula() throws Exception {
FileInputStream inputStream = new FileInputStream(PATH + "公式.xls");
Workbook workbook = new HSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(4);
Cell cell = row.getCell(0);
// 拿到计算公司 eval
FormulaEvaluator FormulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook)workbook);
// 输出单元格的内容
int cellType = cell.getCellType();
switch (cellType){
case Cell.CELL_TYPE_FORMULA: // 公式
String formula = cell.getCellFormula();
System.out.println(formula);
// 计算
CellValue evaluate = FormulaEvaluator.evaluate(cell);
String cellValue = evaluate.formatAsString();
System.out.println(cellValue);
break;
}
}