一、POI简介
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Micrsoft Office格式档案读和写的功能。
HSSF
: 提供读写Microsoft Excel
格式档案的功能。(.xls)
XSSF
: 提供读写Microsoft Excel OOXML
格式档案的功能。(.xlsx)
HWPF
: 提供读写Microsoft Word
格式档案的功能。
HSLF
:提供读写Microsoft PowerPoint
格式档案的功能。
HDGF
: 提供读写Microsoft Visio
格式档案的功能。
二、excel版本
(1)03excel
版本:xls
,最大储存行数是65536;
(2)07excel
版本:xlsx
。
三、excel操作术语
(1)整个excel
文档称为workbook
;
(2)每个workbook
包括多个sheet
(工作单元);
(3)每个sheet
包括多个row
(行);
(4)每个row
包括多个cell
(单元格/列);
(5)每个cell
包括多个内容。
四、POI大数据写操作
(1)03版本excel
中存储行数限制是65536,使用对象HSSFWorkbook
;
(2)07版本excel中能存储,使用对象XSSFWorkbook
,但是效率很低;
(3)写入大数据量的execl文件内容,在poi
提供了一个专门针对大数据写操作的对象SXSSFWorkbook
,推荐使用;
(4)批处理操作。
03&07版本写测试类
/**
* 功能描述
* @MethodName: textWriterExcel03
* @MethodParam: []
* @Return: void
* @Description: TODO--03Excel版本测试写操作(.xls)
* @Author: LinHong
* @CreateDate: 2019/11/18 15:43
*/
@Test
public void textWriterExcel03() throws IOException {
//1、创建workbook( 工作簿 )
Workbook workbook = new HSSFWorkbook();
//2、使用workbook( 工作表 名为"xx管理")
Sheet sheet = workbook.createSheet("xx管理");
//3、使用sheet创建row(行)
Row row = sheet.createRow(0);
//4、使用row创建cell(单元格)
Cell cell = row.createCell(0);
//5、向cell设置值
cell.setCellValue("Hello World!");
//6、使用流的方式写入到文件中
FileOutputStream out = new FileOutputStream("E:/(000)DevelopTools/poi-test-space/test-write03.xls");
workbook.write(out);
//关闭文件
out.close();
}
/**
* 功能描述
* @MethodName: textWriterExcel07
* @MethodParam: []
* @Return: void
* @Description: TODO--07Excel版本测试写操作(.xlsx)
* @Author: LinHong
* @CreateDate: 2019/11/18 15:44
*/
@Test
public void textWriterExcel07() throws IOException {
//1、创建workbook(工作簿)
Workbook workbook = new XSSFWorkbook();
//2、使用workbook(工作表 名为"xx管理")
Sheet sheet = workbook.createSheet("xx管理");
//3、使用sheet创建row(行)
Row row = sheet.createRow(0);
//4、使用row创建cell(单元格)
Cell cell = row.createCell(0);
//5、向cell设置值
cell.setCellValue("Hello World!");
//6、使用流的方式写入到文件中
FileOutputStream out = new FileOutputStream("E:/(000)DevelopTools/poi-test-space/test-write03.xlsx");
workbook.write(out);
//关闭文件
out.close();
}
03版本大数据写测试类
/**
* 功能描述
* @MethodName: textWriterExcel031
* @MethodParam: []
* @Return: void
* @Description: TODO--03Excel版本大数据测试写操作(.xls)65536
* @Author: LinHong
* @CreateDate: 2019/11/18 15:43
*/
@Test
public void textWriterExcel031() throws IOException {
//1、创建workbook( 工作簿 )
Workbook workbook = new HSSFWorkbook();
//2、使用workbook( 工作表 名为"xx管理")
Sheet sheet = workbook.createSheet("xx管理");
try {
for (int i = 0; i <=66666; i++) { //会报错
//3、使用sheet创建row(行)
Row row = sheet.createRow(i);
//4、使用row创建cell(单元格)
Cell cell = row.createCell(0);
//5、向cell设置值
cell.setCellValue("Hello World!"+i);
}
//6、使用流的方式写入到文件中
FileOutputStream out = new FileOutputStream("E:/(000)DevelopTools/poi-test-space/test-write03.xls");
workbook.write(out);
//关闭文件
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
报错
五、POI读操作
(1)03版本
/**
* 功能描述
* @MethodName: textReadExcel03
* @MethodParam: []
* @Return: void
* @Description: TODO 03Excel版本测试读操作
* @Author: LinHong
* @CreateDate: 2019/11/18 15:42
*/
@Test
public void textReadExcel03() throws Exception{
//1、获取文件
FileInputStream inputStream = new FileInputStream("E:/(000)DevelopTools/poi-test-space/test-read03.xls");
//2、创建workbook
Workbook workbook = new HSSFWorkbook(inputStream);
//3、获取sheet
Sheet sheet = workbook.getSheetAt(0);
//4、获取row
Row row = sheet.getRow(0);
//5、获取cell
Cell cell1 = row.getCell(0);
Cell cell2 = row.getCell(1);
Cell cell3 = row.getCell(2);
//6、获取cell值
String stringCellValue1 = cell1.getStringCellValue();
//注意类型 String stringCellValue2 = cell2.getStringCellValue();
double numericCellValue2 = cell2.getNumericCellValue();
String stringCellValue3 = cell3.getStringCellValue();
System.out.println(stringCellValue1);
System.out.println(numericCellValue2);
System.out.println(stringCellValue3);
}
(2)07版本同上
小结:excel读操作时,excel的行数可以不确定,但是多少列和每列的内容必须是规定好的!
♚学习、实战、总结、分享,让生活变得更美好!
☞林大侠博客:https://coding0110lin.blog.csdn.net/ 欢迎转载,一起技术交流探讨!