首先导入依赖
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
03版本xls比07版本xlsx的容量大,对应的工作类也不同
分别有:工作簿,工作表,行,列
生成03版本
String PATH="D:\\";
@Test
public void testWrite03() throws Exception {
//1.创建一个工作簿
Workbook workbook=new HSSFWorkbook();
//2.创建一个工作表
Sheet sheet=workbook.createSheet("sheet1");
//3.创建一个行
Row row1=sheet.createRow(0);
//4.创建一个单元格
Cell cell11=row1.createCell(0);
cell11.setCellValue("11");
Cell cell12=row1.createCell(1);
cell12.setCellValue("12");
//第二行
Row row2=sheet.createRow(1);
Cell cell21=row2.createCell(0);
cell21.setCellValue("21");
Cell cell22=row2.createCell(1);
cell22.setCellValue("22");
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "xxx.xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("生成完毕");
}
生成07版本
@Test
public void testWrite07() throws Exception {
//1.创建一个工作簿
Workbook workbook=new XSSFWorkbook();
//2.创建一个工作表
Sheet sheet=workbook.createSheet("sheet1");
//3.创建一个行
Row row1=sheet.createRow(0);
//4.创建一个单元格
Cell cell11=row1.createCell(0);
cell11.setCellValue("11");
Cell cell12=row1.createCell(1);
cell12.setCellValue("12");
//第二行
Row row2=sheet.createRow(1);
Cell cell21=row2.createCell(0);
cell21.setCellValue("21");
Cell cell22=row2.createCell(1);
cell22.setCellValue("22");
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "xxx.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("生成完毕");
}