Excel表的文件扩展名有两种格式,分别为.xls和.xlsx。.xls文件扩展名对应的是Microsoft Office EXCEL 2003及以前的版本,最大有65535行。.xlsx文件扩展名对应的是Microsoft Office EXCEL 2007及后期的版本,行数无限制。
-
1.需要的依赖
<!--xls(03)--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <!--xlsx(07)--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <!-- 日期格式化工具 --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.10.10</version> </dependency>
-
2.读写.xls结尾的Excel表
(1)写Excel表
public void testWriter03BigData() throws IOException { //获取当前时间 long begin = System.currentTimeMillis(); //创建一个工作簿 Workbook workbook=new HSSFWorkbook(); //创建一张表 Sheet sheet = workbook.createSheet(); //创建单元格 for (int RowNum = 0; RowNum < 65536; RowNum++) { Row row = sheet.createRow(RowNum); //创建行 for (int cellNum = 0; cellNum < 10; cellNum++) { Cell cell = row.createCell(cellNum); //根据列创建单元格,一行十列 cell.setCellValue(cellNum); //往单元格中写入数据 } } System.out.println("over"); //文件路径:path+表名 FileOutputStream fileOutputStream = new FileOutputStream(path + "表1.xls"); workbook.write(fileOutputStream); //写入数据 fileOutputStream.close(); //关闭流 long end = System.currentTimeMill