请生成poi导出百万级数据到Excel的代码,要求速度最快
时间: 2024-05-05 19:18:49 浏览: 11
以下是生成poi导出百万级数据到Excel的代码,速度较快:
```
// 创建一个Excel文件对象
Workbook excel = new XSSFWorkbook();
// 创建一个工作表对象
Sheet sheet = excel.createSheet("Sheet1");
// 创建数据行对象
Row row;
// 写入表头
String[] headers = {"列1", "列2", "列3"};
row = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
Cell cell = row.createCell(i);
cell.setCellValue(headers[i]);
}
// 写入数据
long startTime = System.currentTimeMillis();
for (int i = 1; i <= 1000000; i++) {
row = sheet.createRow(i);
for (int j = 0; j < headers.length; j++) {
Cell cell = row.createCell(j);
cell.setCellValue("数据" + i + "-" + j);
}
}
long endTime = System.currentTimeMillis();
System.out.println("生成Excel文件耗时:" + (endTime - startTime) + "毫秒");
// 输出Excel文件
FileOutputStream out = null;
try {
out = new FileOutputStream("data.xlsx");
excel.write(out);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)