03版本excel
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.1.0</version>
</dependency>
/**
* HSSFWorkbook 03版excel 后缀.xls,最大行数65536
* @throws IOException
*/
@Test
public void testWrite03() throws IOException {
//创建工作簿
Workbook workbook = new HSSFWorkbook();
//创建工作表
Sheet sheet = workbook.createSheet("表名");
//创建行
Row row = sheet.createRow(1);
//创建单元格
Cell cell = row.createCell(2);
//单元格赋值
cell.setCellValue("dzz2");
//用一个文件输出流输出
FileOutputStream fileOutputStream = new FileOutputStream("E:\\practice\\poi-test\\" + "test1.xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
}
07版本excel
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.1.0</version>
</dependency>
/**
* XSSFWorkbook 07版excel 后缀.xlsx,最大行数无限制
* @throws IOException
*/
@Test
public void testWrite07() throws IOException {
//创建工作簿
Workbook workbook = new XSSFWorkbook();
//创建工作表
Sheet sheet = workbook.createSheet("表名");
//创建行
Row row = sheet.createRow(1);
//创建单元格
Cell cell = row.createCell(2);
//单元格赋值
cell.setCellValue("dzz2");
//用一个文件输出流输出
FileOutputStream fileOutputStream = new FileOutputStream("E:\\practice\\poi-test\\" + "test1.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
}
/**
* SXSSFWorkbook 07super版excel 后缀.xlsx,最大行数无限制,速度比XSSFWorkbook更快
* @throws IOException
*/
@Test
public void testWrite07s() throws IOException {
//创建工作簿
Workbook workbook = new SXSSFWorkbook();
//创建工作表
Sheet sheet = workbook.createSheet("表名");
//创建行
Row row = sheet.createRow(1);
//创建单元格
Cell cell = row.createCell(2);
//单元格赋值
cell.setCellValue("dzz2");
//用一个文件输出流输出
FileOutputStream fileOutputStream = new FileOutputStream("E:\\practice\\poi-test\\" + "test1.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
}