1、POI简介与依赖导入
poi官网:poi.apache.org
03版的数据只能有65536条
07版的则无限制
<!--xls(03)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!--xlsx(07)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!--日期格式化工具-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!--单元测试工具-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.10</version>
</dependency>
2、POI创建Excel文件
1、poi创建xlsx03==>HSSFWorkbook
String PATH="D:\\AAjava_project\\parent\\poi";
@Test
public void testCreateExcel03() throws IOException {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet1");
FRow row1 = sheet.createRow(0);//创建第一行
//第一行第一个单元格及其值
FCell cell11 = row1.createCell(0);
cell11.setCellValue("创建Excel表格cell11");
//第二行第二个单元格及其值
FCell cell12 = row1.createCell(1);
cell12.setCellValue("创建Excel表格cell12");
//创建第二行并且设置单元格内的值
Row row2 = sheet.createRow(1);//创建第一行
//第一行第一个单元格及其值
Cell cell21 = row2.createCell(0);
cell21.setCellValue("创建Excel表格cell21");
//第二行第二个单元格及其值
Cell cell22 = row2.createCell(1);
cell22.setCellValue("创建Excel表格cell22");
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "创建Excel表格03.xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("Excel生成完毕");
}
2、poi创建xlsx07==>XSSFWorkbook
String PATH="D:\\AAjava_project\\parent\\poi";
@Test
public void testCreateExcel07() throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet1");
FRow row1 = sheet.createRow(0);//创建第一行
//第一行第一个单元格及其值
FCell cell11 = row1.createCell(0);
cell11.setCellValue("创建Excel表格cell11");
//第二行第二个单元格及其值
FCell cell12 = row1.createCell(1);
cell12.setCellValue("创建Excel表格cell12");
//创建第二行并且设置单元格内的值
Row row2 = sheet.createRow(1);//创建第一行
//第一行第一个单元格及其值
Cell cell21 = row2.createCell(0);
cell21.setCellValue("创建Excel表格cell21");
//第二行第二个单元格及其值
Cell cell22 = row2.createCell(1);
cell22.setCellValue("创建Excel表格cell22");
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "创建Excel表格03.xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("Excel生成完毕");
}
3、HSSFWorkBook导入大量数据
03.xls
当行数超出65536时,程序会出现错误
@Test
public void testCreateExcel03BigData() throws IOException {
long startTime = System.currentTimeMillis();
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet();
Row row;
for(int i=0;i<65536;i++){
row = sheet.createRow(i);
for(int j=0;j<10;j++){
row.createCell(j).setCellValue(j);
}
}
System.out.println("数据导入完毕");
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "xls03导入大量数据.xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("xls03导入大量数据完成");
long endTime = System.currentTimeMillis();
System.out.println((double)(endTime-startTime)/1000);
}
4、XSSFWorkBook导入大量数据
07.xlsx
对数据行数没有限制,但是导入效率会降低
@Test
public void testCreateExcel07BigData() throws IOException {
long startTime = System.currentTimeMillis();
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
Row row;
for(int i=0;i<100000;i++){
row = sheet.createRow(i);
for(int j=0;j<10;j++){
row.createCell(j).setCellValue(j);
}
}
System.out.println("数据导入完毕");
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "xls07导入大量数据.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("xls03导入大量数据完成");
long endTime = System.currentTimeMillis();
System.out.println((double)(endTime-startTime)/1000);
}
5、解决XSSFWorkBook效率问题
解决效率问题:使用SXSSFWorkBook
写数据更快,占用更少的内存
会产生临时文件,需要清理临时文件
@Test
public void testCreateExcel07BigDatas() throws IOException {
long startTime = System.currentTimeMillis();
Workbook workbook = new SXSSFWorkbook();
Sheet sheet = workbook.createSheet();
Row row;
for(int i=0;i<100000;i++){
row = sheet.createRow(i);
for(int j=0;j<10;j++){
row.createCell(j).setCellValue(j);
}
}
System.out.println("数据导入完毕");
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "xls07导入大量数据s.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
//清理临时文件
((SXSSFWorkbook)workbook).dispose();
System.out.println("xls03导入大量数据完成");
long endTime = System.currentTimeMillis();
System.out.println((double)(endTime-startTime)/1000);
}
3、POI读取Excel文件内容
1、HSSFWorkbook读取Excel
03.xls读取xls文件
@Test
public void testgetData03() throws IOException {
FileInputStream fileInputStream = new FileInputStream("D:\\AAjava_project\\parent\\poi创建Excel表格.xls");
Workbook workbook = new HSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
//获取为字符串的值
//获取的类型值与表格中的值的类型不匹配则报错
System.out.println(row.getCell(0).getStringCellValue());
fileInputStream.close();
}
2、HSSFWorkbook读取Excel
07.xlsx读取xlsx文件
@Test
public void testgetData07() throws IOException {
FileInputStream fileInputStream = new FileInputStream("D:\\AAjava_project\\parent\\poi创建Excel表格07.xlsx");
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
//获取为字符串的值
//获取的类型值与表格中的值的类型不匹配则报错
//System.out.println(row.getCell(0).getStringCellValue());
System.out.println(row.getCell(1).getStringCellValue());
fileInputStream.close();
}
3、读取不同类型的数据(重点)
4、了解计算公式
100
200
400
700
@Test
public void testFormula() throws IOException {
FileInputStream fileInputStream = new FileInputStream("D:\\AAjava_project\\parent\\公式.xls");
Workbook workbook = new HSSFWorkbook(fileInputStream);
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:
String cellFormula = cell.getCellFormula();
System.out.println(cellFormula);
//计算
CellValue evaluate = formulaEvaluator.evaluate(cell);
String cellValue = evaluate.formatAsString();
System.out.println(cellValue);
}
}
4、EasyExcel的使用
1、导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.0-beta2</version>
</dependency>
2、创建excel表格并写入数据
1、DemoData
@Data
public class DemoData {
@ExcelProperty("字符串标题")
private String string;
@ExcelProperty("日期标题")
private Date date;
@ExcelProperty("数字标题")
private Double doubleData;
@ExcelIgnore//忽略该字段
private String ignore;
}
2、EasyTest
public class EasyTest {
private List<DemoData> data(){
ArrayList<DemoData> list = new ArrayList<>();
for(int i=0;i<10;i++){
DemoData data = new DemoData();
data.setString("字符串"+i);
data.setDate(new Date());
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
@Test
public void testeasy(){
String fileName="D:\\AAjava_project\\parent\\EasyTest.xlsx";
EasyExcel.write(fileName,DemoData.class).sheet("模板").doWrite(data());
}
}