导入pom依赖
<!--poi依赖-->
<!--xls(03)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!--xlsx(07)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!--日期格式化工具-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
<!--test-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
写操作
public class ExcelWriteTest {
private final String PATH = "C:\\develop\\dev_code\\code_myStudy\\SpringBoot_study\\excel";
/**
* 文件后缀:xls
* @throws IOException
*/
@Test
public void testWrite03() throws IOException {
//创建excel工作薄
Workbook workbook = new HSSFWorkbook();
//在工作薄中创建工资表sheet
//Sheet sheet = workbook.createSheet();
Sheet sheet = workbook.createSheet("直播观众统计");
//创建行,第一行row1
Row row1 = sheet.createRow(0);
//创建单元格 col1-1
Cell cell11 = row1.createCell(0);
cell11.setCellValue("今日新增关注");
//创建单元格 col1-2
Cell cell12 = row1.createCell(1);
cell12.setCellValue(999);
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
cell21.setCellValue("统计时间");
Cell cell22 = row2.createCell(1);
cell22.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
FileOutputStream out = new FileOutputStream(PATH + "观众统计表03.xls");
workbook.write(out);
out.close();
System.out.println("文件生成成功!");
}
/**
* 文件后缀:xlsx
* @throws IOException
*/
@Test
public void testWrite07() throws IOException {
//创建excel工作薄
Workbook workbook = new XSSFWorkbook();
//在工作薄中创建工资表sheet
//Sheet sheet = workbook.createSheet();
Sheet sheet = workbook.createSheet("直播观众统计");
//创建行,第一行row1
Row row1 = sheet.createRow(0);
//创建单元格 col1-1
Cell cell11 = row1.createCell(0);
cell11.setCellValue("今日新增关注");
//创建单元格 col1-2
Cell cell12 = row1.createCell(1);
cell12.setCellValue(999);
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
cell21.setCellValue("统计时间");
Cell cell22 = row2.createCell(1);
cell22.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
FileOutputStream out = new FileOutputStream(PATH + "观众统计表07.xlsx");
workbook.write(out);
out.close();
System.out.println("文件生成成功!");
}
}
在指定位置生成excel文件
大文件写HSSF03
缺点:最多只能处理65536行,否则抛出异常
优点:过程中写入缓存,不操作磁盘,最后一次性写入磁盘,速度快
/**
* 03大数据测试
* @throws IOException
*/
@Test
public void testWrite03BigData() throws IOException {
long begin = System.currentTimeMillis();
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("大数据测试");
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);
}
}
FileOutputStream out = new FileOutputStream(PATH + "bigdata03.xls");
workbook.write(out);
out.close();
long end = System.currentTimeMillis();
System.out.println((double) (end - begin) / 1000);
}
可以看到excel正常生成了,如果将65536改为比他大的值就会报错。
java.lang.IllegalArgumentException: Invalid row number (65536) outside allowable range (0..65535)
大文件写XSSF07
缺点:写数据时速度非常慢,非常耗内存,也会发生内存溢出,如100万条
优点:可以写较大的数据量,如20万条
@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("done");
FileOutputStream out = new FileOutputStream(PATH + "bigdata07.xlsx");
workbook.write(out);
out.close();
long end = System.currentTimeMillis();
System.out.println((double) (end - begin) / 1000);
}
所用时间:20.253s
大文件写SXSSF
优点:可以写非常大的数据量,如100万条甚至更多条,写数据速度快,占用更少的内存
@Test
public void testWrite07BigDataFast() throws IOException {
long begin = System.currentTimeMillis();
//Sxssf写
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("done");
FileOutputStream out = new FileOutputStream(PATH+"bigdata07-fast.xlsx");
workbook.write(out);
out.close();
((SXSSFWorkbook)workbook).dispose();
long end = System.currentTimeMillis();
System.out.println((double)(end - begin)/1000);
}
所用时间:3.492s
读操作
HSSF读文件
private final String PATH = "C:\\develop\\dev_code\\code_myStudy\\SpringBoot_study\\excel";
@Test
public void testRead03() throws Exception {
FileInputStream is = new FileInputStream(PATH+"观众统计表03.xls");
Workbook workbook = new HSSFWorkbook(is);
Sheet sheet = workbook.getSheetAt(0);
//读取第一行第一列
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
System.out.println(cell.getStringCellValue());
is.close();
}
XSSF读文件
@Test
public void testRead07() throws Exception {
FileInputStream is = new FileInputStream(PATH+"观众统计表07.xlsx");
Workbook workbook = new XSSFWorkbook(is);
Sheet sheet = workbook.getSheetAt(0);
//读取第一行第一列
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
System.out.println(cell.getStringCellValue());
is.close();
}
读取不同的数据类型
/**
* 读取不同的数据类型处理
* @throws Exception
*/
@Test
public void testCellType() throws Exception {
InputStream is = new FileInputStream(PATH+"会员消费商品明细表.xls");
Workbook workbook = new HSSFWorkbook(is);
Sheet sheet = workbook.getSheetAt(0);
// 读取标题所有内容
Row rowTitle = sheet.getRow(0);
// 行不为空
if (rowTitle != null) {
// 读取cell
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) {// 行不为空
// 读取cell
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】");
//cellValue = String.valueOf(cell.getNumericCellValue());
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 Cell.CELL_TYPE_ERROR:
System.out.print("【数据类型错误】");
break;
}
System.out.println(cellValue);
}
}
}
}
is.close();
}
计算公式
/**
* 计算公式
* @throws Exception
*/
@Test
public void testFormula() throws Exception{
InputStream is = new FileInputStream(PATH + "计算公式.xls");
Workbook workbook = new HSSFWorkbook(is);
Sheet sheet = workbook.getSheetAt(0);
// 读取第五行第一列
Row row = sheet.getRow(4);
Cell cell = row.getCell(0);
//公式计算器
FormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);
// 输出单元内容
int cellType = cell.getCellType();
switch (cellType) {
case Cell.CELL_TYPE_FORMULA://2
//得到公式
String formula = cell.getCellFormula();
System.out.println(formula);
CellValue evaluate = formulaEvaluator.evaluate(cell);
//String cellValue = String.valueOf(evaluate.getNumberValue());
String cellValue = evaluate.formatAsString();
System.out.println(cellValue);
break;
}
}