一.导入导出的认识
操作办公软件(word,excel,ppt) 03,07
JXL(只支持excel,内存优化) POI(功能强大,支持广泛)
POI的基本使用
1.1 导包
<!-- poi支持的jar包 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
1.2 创建 excel文件
//创建一个新的excel,并且写99乘法表:导出
@Test
public void testCreateExcel() throws Exception{
//1.创建一个工作薄(在内存中)
SXSSFWorkbook wb = new SXSSFWorkbook();
//2.创建一张表格
Sheet sheet = wb.createSheet("99乘法表");
//3.在表格创建行
for(int i=1;i<=9;i++){
Row row = sheet.createRow(i-1);
//4.在表格中创建列(格子)
for(int j=1;j<=i;j++){
Cell cell = row.createCell(j-1);
//5.格子中写东西
cell.setCellValue(i+"*"+j+"="+(i*j));
}
}
FileOutputStream out = new FileOutputStream("99.xlsx");
wb.write(out);
out.close();
}
1.2 读取excel文件
//读取excel:导入
@Test
public void testReadExcel() throws Exception{
//读取了emp-poi.xlsx ,创建了Workbook(内存)
InputStream inp = new FileInputStream("emp-poi.xlsx");
Workbook wb = WorkbookFactory.create(inp);
//拿到对应的表
Sheet sheet = wb.getSheetAt(0);
//拿到这表的总行数
int lastRowNum = sheet.getLastRowNum();
for (int i = 1; i <= lastRowNum; i++) {
//拿到每一行
Row row = sheet.getRow(i);
//拿到这一行的总列数
short lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
//拿到这一个格子与它的数据
Cell cell = row.getCell(j);
System.out.print(cell.getStringCellValue()+" ");
}
System.out.println();
}
}
三.EasyPOI的使用
3.1 导包
注意:今天使用EasyPOI需要把之前的poi导包去掉
<!-- easypoi的支持 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web