目录
POI
1.倒入依赖
<dependencies>
<!--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>
</dependencies>
2.写入excel
- 03版excel和07版excel不同,具体不同请自行百度,03版的后缀名只能为xls,07则可以为xlsx,03版的excel最多只能存储65536行,超出则会抛出异常;07版的excel则没有数据上限制,在操作大量数据时,要注意。
- Workbook工作簿接口有三个实现类,分别是操作03版excel,加强操作07版excel和操作07版excel实现类,加强版可以增快对07版excel操作的速度
- 写入大量数据时:
- 代码示例:
//03版本写入excel,后缀为xls。03版的excel最多只能存储65536行,超出则会抛出异常
@Test
public void testWrite03() throws Exception {
//生成Excel的目录
String PATH = "/Users/liuxiaoxiao/Desktop/exercise/";
//创建工作簿03版
Workbook workbook = new HSSFWorkbook();
//创建工作表
Sheet sheet = workbook.createSheet("成绩表");
//创建第一行
Row row0 = sheet.createRow(0);
//创建第一行的第一列
Cell cell11 = row0.createCell(0);
//把第一行的第一列单元格赋值
cell11.setCellValue("姓名");
//创建第一行的第二列
Cell cell12 = row0.createCell(1);
//把第一行的第二列单元格赋值
cell12.setCellValue("刘诗诗");
//创建第二行
Row row1 = sheet.createRow(1);
//创建第二行的第一列
Cell cell21 = row1.createCell(0);
//把第2行的第一列单元格赋值
cell21.setCellValue("成绩");
//创建第二行的第二列
Cell cell22 = row1.createCell(1);
//把第2行的第二列单元格赋值
cell22.setCellValue("100");
//创建第3行
Row row2 = sheet.createRow(2);
//创建第3行的第一列
Cell cell31 = row2.createCell(0);
//把第3行的第一列单元格赋值
cell31.setCellValue("时间");
//创建第3行的第二列
Cell cell32 = row2.createCell(1);
//把第3行的第二列单元格赋值
String time = new DateTime().toString("yyyy-MM-dd Hh:mm:ss");
cell32.setCellValue(time);
//创建文件流,生成excel表
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "03版excel成绩表.xls");
//将表内容写到文件流里
workbook.write(fileOutputStream);
//关闭流
fileOutputStream.close();
}
//07版本写入excel,后缀为xlsx
@Test
public void testWrite07() throws Exception {
//生成Excel的目录
String PATH = "/Users/liuxiaoxiao/Desktop/exercise/";
//创建工作簿07版
Workbook workbook = new XSSFWorkbook();
//创建工作表
Sheet sheet = workbook.createSheet("成绩表");
//创建第一行
Row row0 = sheet.createRow(0);
//创建第一行的第一列
Cell cell11 = row0.createCell(0);
//把第一行的第一列单元格赋值
cell11.setCellValue("姓名");
//创建第一行的第二列
Cell cell12 = row0.createCell(1);
//把第一行的第二列单元格赋值
cell12.setCellValue("刘诗诗");
//创建第二行
Row row1 = sheet.createRow(1);
//创建第二行的第一列
Cell cell21 = row1.createCell(0);
//把第2行的第一列单元格赋值
cell21.setCellValue("成绩");
//创建第二行的第二列
Cell cell22 = row1.createCell(1);
//把第2行的第二列单元格赋值
cell22.setCellValue("100");
//创建第3行
Row row2 = sheet.createRow(2);
//创建第3行的第一列
Cell cell31 = row2.createCell(0);
//把第3行的第一列单元格赋值
cell31.setCellValue("时间");
//创建第3行的第二列
Cell cell32 = row2.createCell(1);
//把第3行的第二列单元格赋值
String time = new DateTime().toString("yyyy-MM-dd Hh:mm:ss");
cell32.setCellValue(time);
//创建文件流,生成excel表
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "07版excel成绩表.xlsx");
//将表内容写到文件流里
workbook.write(fileOutputStream);
//关闭流
fileOutputStream.close();
}
//使用加强版07excel工作簿SXSSFWorkbook来操作大量数据
@Test
public void testWriteBigData07() throws Exception {
//生成Excel的目录
String PATH = "/Users/liuxiaoxiao/Desktop/exercise/";
//创建工作簿07加强版
Workbook workbook = new SXSSFWorkbook();
//创建工作表
Sheet sheet = workbook.createSheet("成绩表");
for (int rowNum = 0; rowNum < 100000;rowNum++){//100000行数据
Row row = sheet.createRow(rowNum);//创建行
for (int cellNum = 0; cellNum < 8 ;cellNum++){//每行有8条数据
Cell cell = row.createCell(cellNum);//创建行中的列
cell.setCellValue("1");//每个单元格都写入1
}
}
//创建文件流,生成excel表
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "07加强版excel成绩表.xlsx");
//将表内容写到文件流里
workbook.write(fileOutputStream);
//关闭流
fileOutputStream.close();
//清除临时文件
((SXSSFWorkbook)workbook).dispose();
}
3.读取excel中数据
- 注意:读取excel表格内容时要注意单元格数据类型,不对应会报错
//读取03版excel,和读取07版excel的区别的就是创建工作簿的类不同,一个是HSSFWorkbook,一个是XSSFWorkbook
@Test
public void testRead03Excel() throws IOException {
//读取excel的路径
String PATH = "/Users/liuxiaoxiao/Desktop/exercise/03版excel成绩表.xls";
//创建文件流
FileInputStream inputStream = new FileInputStream(PATH);
//创建工作簿
Workbook workbook = new HSSFWorkbook(inputStream);
//得到工作簿的第一个表
Sheet sheet = workbook.getSheetAt(0);
//得到第一行
Row row = sheet.getRow(0);
//得到第一列
Cell cell = row.getCell(0);
//得到第一行第一列的内容
//注意单元格对应的数据类型,不对应的话会报错
// 例如得到数字类型:cell.getNumericCellValue();
String stringCellValue = cell.getStringCellValue();
System.out.println(stringCellValue);
//关闭流
inputStream.close();
}
//读取07版excel,和读取03版excel的区别的就是创建工作簿的类不同,一个是HSSFWorkbook,一个是XSSFWorkbook
@Test
public void testRead07Excel() throws IOException {
//读取excel的路径
String PATH = "/Users/liuxiaoxiao/Desktop/exercise/07版excel成绩表.xlsx";
//创建文件流
FileInputStream inputStream = new FileInputStream(PATH);
//创建工作簿
Workbook workbook = new XSSFWorkbook(inputStream);
//得到工作簿的第一个表
Sheet sheet = workbook.getSheetAt(0);
//得到第一行
Row row = sheet.getRow(0);
//得到第一列
Cell cell = row.getCell(0);
//得到第一行第一列的内容
//注意但愿个对应的数据类型,不对应的话会报错
// 例如得到数字类型:cell.getNumericCellValue();
String stringCellValue = cell.getStringCellValue();
System.out.println(stringCellValue);
//关闭流
inputStream.close();
}
4.读取不同的数据类型
//获取当前行下有多少列;row是获取的行行
int physicalNumberOfCells = row.getPhysicalNumberOfCells();
//获取总共有多少行
int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
//匹配列的数据类型
if (cell != null){
//获取列对应的类型
int cellType = cell.getCellType();
//单元格的值
String data = "";
switch (cellType){
case HSSFCell.CELL_TYPE_STRING://字符串
data = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN://布尔
data = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK://空
break;
case HSSFCell.CELL_TYPE_NUMERIC://数字
if (HSSFDateUtil.isCellDateFormatted(cell)){//如果是日期格式
Date dateCellValue = cell.getDateCellValue();
data = new DateTime(dateCellValue).toString("yyyy-MM-dd");
}else {
//防止数字过长,先把数字转换为字符串
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
data = cell.toString();
}
break;
case HSSFCell.CELL_TYPE_ERROR://数据类型错误
break;
}
}
5.计算公式
@Test
public void testFormula() throws IOException {
FileInputStream inputStream = new FileInputStream("表格路径");
Workbook workbook = new HSSFWorkbook(inputStream);
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 HSSFCell.CELL_TYPE_FORMULA://公式格式
String cellFormula = cell.getCellFormula();//获取公式
CellValue cellValue = formulaEvaluator.evaluate(cell);//计算
String data = cellValue.formatAsString();
break;
}
}
EasyExcel
官方文档:读Excel · 语雀
官方文档的代码解释的特别详细,根据业务需求去看文档就可以了,写代码时记得倒入相关依赖!
固定套路:
- 写入:固定类格式进行写入
- 读取:根据监听器设置的规则进行读取