POI
1.基本功能
结构:
HSSF — 提供读写Mircrosft Excel格式档案功能(03版)
XSSF — 提供读写Mircrosft Excel OOXML格式档案的功能(07版)
HWPF — 提供读写Mircrosft World格式档案的功能
HSLF — 提供读写Mircrosft PowerPoint格式档案的功能
HDGF — 提供读写Mircrosft Visio格式档案的功能
2.POI — 导出
导入依赖
<dependencies>
<!--xls(03)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!--xls(07)-->
<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>
基础代码
public class ExcelWriteTest {
String PATH = "E:\\hanfeng\\";
@Test
public void testWrite03() throws IOException {
// 1.创建工作簿
Workbook workbook = new HSSFWorkbook();
// 2.创建一个工作表
Sheet sheet = workbook.createSheet("狂神观众统计表");
// 3.创建一个行
Row row1 = sheet.createRow(0);
// 4.创建一个单元格
Cell cell11 = row1.createCell(0);
cell11.setCellValue("今日新增观众");
Cell cell12 = row1.createCell(1);
cell12.setCellValue(666);
// 第二行
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
cell21.setCellValue("统计时间");
Cell cell22 = row2.createCell(1);
String time = new DateTime().toString("yyyy-MM-ss HH:mm:ss");
cell22.setCellValue(time);
// 生成一张表(IO 流)
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "狂神观众统计表03.xls");
workbook.write(fileOutputStream);
// 关闭流
fileOutputStream.close();
System.out.println("03生成完毕");
}
@Test
public void testWrite07() throws IOException {
// 1.创建工作簿
Workbook workbook = new XSSFWorkbook();
// 2.创建一个工作表
Sheet sheet = workbook.createSheet("狂神观众统计表");
// 3.创建一个行
Row row1 = sheet.createRow(0);
// 4.创建一个单元格
Cell cell11 = row1.createCell(0);
cell11.setCellValue("今日新增观众");