• HSSFWorkBook - 提供读写Microsoft Excel格式档案的功能。(.xls)-03
• XSSFWorkBook - 提供读写Microsoft Excel OOXML格式档案的功能。(.xlsx)-07
pom中引入xml相关依赖
<dependencies>
<!--xls-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!--xlsx-->
<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>
使用HSSF
缺点:最多只能处理65536行,否则会抛出异常 java.lang.IllegalArgumentException: Invalid
row number (65536) outside allowable range (0…65535)
优点:过程中写入缓存,不操作磁盘,最后一次性写入磁盘,速度快
使用XSSF
缺点:写数据时速度非常慢,非常耗内存,也会发生内存溢出,如100万条
优点:可以写较大的数据量,如20万条
使用SXSSF
优点:可以写非常大的数据量,如100万条甚至更多条,写数据速度快,占用更少的内存 注意:
过程中会产生临时文件,需要清理临时文件(C:\Users\helen\AppData\Local\Temp)默认由100条记录被保存在内存中,如果查过这数量,则最前面的数据被写入临时文件 如果想自定义内存中数据的数量,可以使用new
SXSSFWorkbook(数量)
测试excel导出
public class writeTest {
/**
* poi写入excel 03版本
*/
@Test
public void write03() throws IOException {
//1、创建工作表workbook
HSSFWorkbook workbook = new HSSFWorkbook();
//2、在工作表中创建Sheet
HSSFSheet sheet = workbook.createSheet("测试");
//3、在这个Sheet中在创建行数
//创建行和列:他们的索引是从0开始的
HSSFRow row = sheet.createRow(0);
//4、在行数中创建列
HSSFCell cell1 = row.createCell(0);
//5、在响应的列对应的单元格中设置数据
cell1.setCellValue("人数");
HSSFCell cell2 = row.createCell(1);
cell2.setCellValue(123456789);
//6、写入磁盘
FileOutputStream fileOutputStream = new FileOutputStream("f:/0324.xls");
workbook.write(fileOutputStream);
//7、关闭流
fileOutputStream.close();
}
/**
* poi 07版本写入
* @throws IOException
*/
@Test
public void write07() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("测试07");
XSSFRow row = sheet.createRow(0);
XSSFCell cell1 = row.createCell(0);
cell1.setCellValue("ceshi");
XSSFCell cell2 = row.createCell(1);
cell2.setCellValue(123456688);
FileOutputStream fileOutputStream = new FileOutputStream("f:/测试.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
}
/**
* 写入的时候、写的是大数据
* 比如: 几万条数据
* 时间考虑性能
*
* 03版本:
*/
@Test
public void WriterData03() throws Exception{
Long begin = System.currentTimeMillis();
//1、创建工作表workbook
Workbook workbook = new HSSFWorkbook();
//2、在工作表中创建Sheet
Sheet sheet = workbook.createSheet("会员管理");
//3、在这个Sheet中在创建行数
//创建行和列:他们的索引是从0开始的
for(int i = 0; i < 65537; i++){
Row row = sheet.createRow(i);
//4、在行数中创建列
Cell cell1 = row.createCell(0);
//5、在响应的列对应的单元格中设置数据
cell1.setCellValue("人数"+i);
}
//6、写入磁盘
FileOutputStream outputStream = new FileOutputStream("f:/0401-03.xls");
workbook.write(outputStream);
Long end = System.currentTimeMillis();
System.err.println((double) (end-begin)/1000);
//7、关闭流
outputStream.close();
}
@Test
public void WriterData07() throws Exception{
Long begin = System.currentTimeMillis();
//1、创建工作表workbook
Workbook workbook = new XSSFWorkbook();
//2、在工作表中创建Sheet
Sheet sheet = workbook.createSheet("会员管理");
//3、在这个Sheet中在创建行数
//创建行和列:他们的索引是从0开始的
//如果是07 版本的话、能写大于65535数据
for(int i = 0; i < 65537; i++){
Row row = sheet.createRow(i);
//4、在行数中创建列
Cell cell1 = row.createCell(0);
//5、在响应的列对应的单元格中设置数据
cell1.setCellValue("人数"+i);
}
//6、写入磁盘
FileOutputStream outputStream = new FileOutputStream("D:/0401-07.xlsx");
workbook.write(outputStream);
Long end = System.currentTimeMillis();
System.err.println((double) (end-begin)/1000);
//7、关闭流
outputStream.close();
}
/**
*
* 使用SXSSF
**/
@Test
public void SXWriterData07() throws Exception{
Long begin = System.currentTimeMillis();
//1、创建工作表workbook
Workbook workbook = new SXSSFWorkbook();
//2、在工作表中创建Sheet
Sheet sheet = workbook.createSheet("会员管理");
//3、在这个Sheet中在创建行数
//创建行和列:他们的索引是从0开始的
//如果是07 版本的话、能写大于65535数据
for(int i = 0; i < 100000; i++){
Row row = sheet.createRow(i);
//4、在行数中创建列
Cell cell1 = row.createCell(0);
//5、在响应的列对应的单元格中设置数据
cell1.setCellValue("人数"+i);
}
//6、写入磁盘
FileOutputStream outputStream = new FileOutputStream("D:/0401SXSSF-07.xlsx");
workbook.write(outputStream);
Long end = System.currentTimeMillis();
System.err.println((double) (end-begin)/1000);
//7、关闭流
outputStream.close();
((SXSSFWorkbook)workbook).dispose();
}
}
读取excel操作
public class readerTest {
@Test
public void read03() throws Exception {
//1、获取一个文件(EXCL)路径,获取流
FileInputStream fileInputStream = new FileInputStream("f:/0324.xls");
//2、根据这个流创建一个WorkBook
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
//3、获取次工作表中的Sheet
HSSFSheet sheet = workbook.getSheetAt(0);
//4、根据这个Sheet获取一行
Row row = sheet.getRow(0);
//5、获取列
Cell cell1 = row.getCell(0);
Cell cell2 = row.getCell(1);
String stringCellValue = cell1.getStringCellValue();
double numericCellValue = cell2.getNumericCellValue();
//6、根据列获取列中的数据
System.out.println(stringCellValue + "----------"+numericCellValue);
//7、关闭流
fileInputStream.close();
}
@Test
public void read07() throws IOException {
FileInputStream fileInputStream = new FileInputStream("f:/测试.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
XSSFSheet sheetAt = workbook.getSheetAt(0);
Row row = sheetAt.getRow(0);
Cell cell1 = row.getCell(0);
Cell cell2 = row.getCell(1);
String stringCellValue = cell1.getStringCellValue();
double numericCellValue = cell2.getNumericCellValue();
System.out.println(stringCellValue+"_______________"+numericCellValue);
fileInputStream.close();
}
}