Java 實現操作 excel
使用 POI
環境搭建
創建一個 maven 工程,pom.xml
中導入以下依賴:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 處理 03版 .xls -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!-- 處理 07版 .xlsx -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
Excel03 版本的導出
03版本的導出較快,但是最多隻能寫65536行。
public class ExcelWrite {
String PATH = "C:\\Users\\Administrator\\Desktop\\";
@Test
public void write03() throws Exception {
//創建工作簿
Workbook workbook = new HSSFWorkbook();
//創建工作表
Sheet sheet = workbook.createSheet("統計表");
//創建第一行
Row row0 = sheet.createRow(0);
//在第一行創建兩個單元格子
Cell cell00 = row0.createCell(0);
cell00.setCellValue("姓名");
Cell cell01 = row0.createCell(1);
cell01.setCellValue("性別");
//創建第二行
Row row1 = sheet.createRow(1);
//在第二行創建兩個單元格子
Cell cell10 = row1.createCell(0);
cell10.setCellValue("sjh");
Cell cell11 = row1.createCell(1);
cell11.setCellValue("male");
//生成一張表
FileOutputStream fos = new FileOutputStream(PATH + "03.xls");
workbook.write(fos);
//釋放資源
fos.close();
System.out.println("excel 生成完畢");
}
}
執行該測試方法,在桌面生成了一個 03.xls
:
Excel07 版本的導出
只是把 Workbook
接口的實現類由 HSSFWorkbook
換成了 XSSFWorkbook
,以及文件結尾從 .xls
換成了 .xlsx
,其餘不變。
07 版本的導出較慢,但能寫更多的行,如果希望提升速度,可以使用SXSSFWorkbook
加強版實現類,但是在使用完成後要調用 dispose()
方法清除臨時的緩衝文件。
@Test
public void write07() throws Exception {
//創建工作簿
Workbook workbook = new XSSFWorkbook();
//創建工作表
Sheet sheet = workbook.createSheet("統計表");
//創建第一行
Row row0 = sheet.createRow(0);
//在第一行創建兩個單元格子
Cell cell00 = row0.createCell(0);
cell00.setCellValue("姓名");
Cell cell01 = row0.createCell(1);
cell01.setCellValue("性別");
//創建第二行
Row row1 = sheet.createRow(1);
//在第二行創建兩個單元格子
Cell cell10 = row1.createCell(0);
cell10.setCellValue("sjh");
Cell cell11 = row1.createCell(1);
cell11.setCellValue("male");
//生成一張表
FileOutputStream fos = new FileOutputStream(PATH + "07.xlsx");
workbook.write(fos);
//釋放資源
fos.close();
System.out.println("excel 生成完畢");
}
執行該測試方法,在桌面生成了一個 07.xlsx
:
Excel03 版本的導入
07 版本的導入也一樣,更換實現類即可。
創建一個 Controller:
@Controller
@RequestMapping("/excel")
public class ExcelRead {
String PATH = "C:\\Users\\Administrator\\Desktop\\";
@RequestMapping("/read03")
public void read03(HttpServletResponse response) throws Exception {
//設置輸出編碼
response.setContentType("text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.print("開始解析 excel 文件...<br/>");
writer.print("---------------------<br/>");
//獲取輸入流並創建工作簿
FileInputStream fis = new FileInputStream(PATH + "03.xls");
Workbook workbook = new HSSFWorkbook(fis);
//獲取表
Sheet sheet = workbook.getSheetAt(0);
//獲取總共的行數
int rowNum = sheet.getLastRowNum();
for (int r = 0; r <= rowNum; r++) {
Row row = sheet.getRow(r);//獲取行對象
int cellNum = row.getLastCellNum();//獲取總共列數
for (int c = 0; c < cellNum; c++) {
Cell cell = row.getCell(c);//獲取單元格
writer.print(cell.getStringCellValue()+" ");
}
writer.print("<br/>");;//換行
}
//關閉流
fis.close();
writer.println("---------------------<br/>");
writer.println("解析 excel 文件完畢<br/>");
}
}
創建一個啓動類:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}
}
啓動 Spring Boot,訪問對應 url,成功解析桌面的 03.xls
文件 :