03版本 new HSSWorkbook
public void TestWrite03() throws IOException {
// 创建工作铺 03版本 HSSFWorkbook() 后缀名.xls
Workbook workbook = new HSSFWorkbook();
// 创建工作表
Sheet sheet = workbook.createSheet("人数表");
// 创建一行
Row row1 = sheet.createRow(0);
// 创建一个单元格
// 1-1
Cell cell11 = row1.createCell(0);
cell11.setCellValue("今日观众");
// 1-2
Cell cell12 = row1.createCell(1);
cell12.setCellValue("666");
// 创建第二行
Row row2 = sheet.createRow(1);
// 2-1
Cell row2Cell21 = row2.createCell(0);
row2Cell21.setCellValue("昨日观众");
// 2-2
Cell row2Cell22 = row2.createCell(1);
row2Cell22.setCellValue("777");
// 生成一张表 io流 03版的就是 xls结尾
FileOutputStream fileOutputStream = new FileOutputStream(path + "第一个excel.xls");
//生成一张表
workbook.write(fileOutputStream);
// 关闭流
fileOutputStream.close();
System.out.println("文件生成完毕");
}
07版本 new XSSFWorkbook
public void TestWrite07() throws IOException {
// 创建工作铺 07 XSSFWorkbook() 后缀名.xlsx
Workbook workbook = new XSSFWorkbook();
// 创建工作表
Sheet sheet = workbook.createSheet("人数表");
// 创建一行
Row row1 = sheet.createRow(0);
// 创建一个单元格
// 1-1
Cell cell11 = row1.createCell(0);
cell11.setCellValue("今日观众");
// 1-2
Cell cell12 = row1.createCell(1);
cell12.setCellValue("666");
// 创建第二行
Row row2 = sheet.createRow(1);
// 2-1
Cell row2Cell21 = row2.createCell(0);
row2Cell21.setCellValue("昨日观众");
// 2-2
Cell row2Cell22 = row2.createCell(1);
row2Cell22.setCellValue("777");
// 生成一张表 io流 03版的就是 xls结尾
FileOutputStream fileOutputStream = new FileOutputStream(path + "第二个excel.xlsx");
//生成一张表
workbook.write(fileOutputStream);
// 关闭流
fileOutputStream.close();
System.out.println("文件生成完毕");
}