POI写数据.
主要靠这三个对象实现
- 导入依赖
<dependencies>
<!-- xls 03 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!-- xls 07 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!-- 日期格式化工具 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>jodi-time</artifactId>
<version>2.10.1</version>
</dependency>
<!-- test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
测试生成excel文件
@Test
public void test03() throws Exception {
// 1. 创建一个工作簿
Workbook workbook = new HSSFWorkbook();
// 2. 创建一个工作表
Sheet sheet = workbook.createSheet("员工计划表");
// 3. 创建一行
Row row1 = sheet.createRow(0);
// 4. 创建一个单元格
Cell cell11 = row1.createCell(0);
// 5. 写入数据
cell11.setCellValue("今日员工数据11");
// 4. 创建一个单元格
Cell cell12 = row1.createCell(1);
// 5. 写入数据
cell12.setCellValue("今日员工数据12");
// 创建第二行
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
cell21.setCellValue("今日员工数据21");
Cell cell22 = row2.createCell(1);
String date = new DateTime().toString("yyy-MM-dd HH:mm:ss");
cell22.setCellValue(date);
// 生成一张表 03 使用xls结尾
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "/员工表03.xls");
workbook.write(fileOutputStream);
// 关闭流
fileOutputStream.close();
// 输出成功
System.out.println("文件生成成功");
}
@Test
public void test07() throws Exception {
// 1. 创建一个工作簿 07
Workbook workbook = new XSSFWorkbook();
// 2. 创建一个工作表
Sheet sheet = workbook.createSheet("员工计划表");
// 3. 创建一行
Row row1 = sheet.createRow(0);
// 4. 创建一个单元格
Cell cell11 = row1.createCell(0);
// 5. 写入数据
cell11.setCellValue("今日员工数据11");
// 4. 创建一个单元格
Cell cell12 = row1.createCell(1);
// 5. 写入数据
cell12.setCellValue("今日员工数据12");
// 创建第二行
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
cell21.setCellValue("今日员工数据21");
Cell cell22 = row2.createCell(1);
String date = new DateTime().toString("yyy-MM-dd HH:mm:ss");
cell22.setCellValue(date);
// 生成一张表 03 使用xlsx结尾
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "/员工表07.xlsx");
workbook.write(fileOutputStream);
// 关闭流
fileOutputStream.close();
// 输出成功
System.out.println("文件生成成功");
}
@Test
// 测试大数据量
public void testBigData03() throws IOException {
// 开始时间
long begin = System.currentTimeMillis();
// 创建一个工作簿
Workbook workbook = new HSSFWorkbook();
// 创建表
Sheet sheet = workbook.createSheet("表");
//写入数据
for (int rowNum = 0; rowNum < 65535; 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 fileOutputStream = new FileOutputStream(PATH + "/testBigData.xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
// 结束时间
long end = System.currentTimeMillis();
System.out.println((double)(end - begin) / 1000);
}
@Test
// 测试大数据量
public void testBigData07() throws IOException {
// 开始时间
long begin = System.currentTimeMillis();
// 创建一个工作簿
Workbook workbook = new XSSFWorkbook();
// 创建表
Sheet sheet = workbook.createSheet("表");
//写入数据
for (int rowNum = 0; rowNum < 65538; 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 fileOutputStream = new FileOutputStream(PATH + "/testBigData07.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
((SXSSFWorkbook) workbook).dispose(); // 关闭临时文件
// 结束时间
long end = System.currentTimeMillis();
System.out.println((double)(end - begin) / 1000);
}
POI读数据
@Test
public void test07() throws Exception {
// 获取文件流
FileInputStream fileInputStream = new FileInputStream(PATH + "/testBigData.xls");
// 1. 创建一个工作簿 07
Workbook workbook = new HSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
// 得到行
Row row = sheet.getRow(0);
// 得到列
Cell cell = row.getCell(0);
double numericCellValue = cell.getNumericCellValue();
System.out.println(numericCellValue);
fileInputStream.close();
}
读取不同类型的值
package com.starcpdk;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
public class EasyExcelReadTest {
private static final String PATH = "I:\\Projects\\JAVA_WEB_Base\\easyexcel";
@Test
public void test07() throws Exception {
// 获取文件流
FileInputStream fileInputStream = new FileInputStream(PATH + "/testBigData.xls");
// 1. 创建一个工作簿 07
Workbook workbook = new HSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
// 得到行
Row row = sheet.getRow(0);
// 得到列
Cell cell = row.getCell(0);
double numericCellValue = cell.getNumericCellValue();
System.out.println(numericCellValue);
fileInputStream.close();
}
@Test
public void testCellType() throws Exception {
// 获取文件流
FileInputStream fileInputStream = new FileInputStream(PATH + "/明细表.xls");
// 1. 创建一个工作簿 07
Workbook workbook = new HSSFWorkbook(fileInputStream);
// 获取标题内容
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){
String cellValue = cell.getStringCellValue();
System.out.println(cellValue);
}
}
}
// 获取表中的内容
// 获取所有行数
int rowCount = sheet.getPhysicalNumberOfRows();
for (int rowNum = 1; rowNum < rowCount; rowNum++) {
Row rowData = sheet.getRow(rowNum);
if (rowData != null){
// 读取列
int cellCount = rowData.getPhysicalNumberOfCells();
for (int cellNum = 0; cellNum < cellCount; cellNum++) {
Cell cell = rowData.getCell(cellNum + 1);
// 匹配列的数据类型
if (cell != null){
int cellType = cell.getCellType();
String cellValue = "";
switch (cellType){
case HSSFCell.CELL_TYPE_STRING: // 字符串
System.out.println("[String]");
cellValue = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // 布尔
System.out.println("[Boolean]");
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK: // 空
System.out.println("[BLANK]");
break;
case HSSFCell.CELL_TYPE_NUMERIC: // 数字(日期和普通数字)
System.out.println("[NUMERIC]");
if (HSSFDateUtil.isCellDateFormatted(cell)){
// 日期
Date date = cell.getDateCellValue();
cellValue = new DateTime(date).toString();
}else {
// 如果不是日期格式 , 防止数字过长
System.out.println("[转换为字符串输出]");
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cellValue = String.valueOf(cell.getBooleanCellValue());
}
break;
case HSSFCell.CELL_TYPE_ERROR: // 空
System.out.println("[数据类型错误]");
break;
}
System.out.println(cellValue);
}
}
}
}
fileInputStream.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;
}
}
EasyExcel
- 导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.7</version>
</dependency>
参考官网和github即可