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
文件 :