欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

poi生成Excel文件

程序员文章站 2022-07-13 13:01:31
...
[b][size=medium][color=indigo]本实例功能为生成10万条记录,值为18位长的整数,并写入Excel文件。仅供参考,谢谢![/color][/size][/b]
[size=medium][b][color=red]注意,excel表的sheet最多65535条记录,故超过此值要分多个sheet创建。[/color][/b][/size]

public class GenerateExcel {
private static int records = 100000;
private static BigInteger start = BigInteger.valueOf(100000000000000001L);
private static String filePath = "D:\\test.xls";

public static void main(String[] args) {
try {
Workbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream(filePath);

int sheetNum = 1;
if (records>50000) {
sheetNum = (records%50000)==0?(records/50000):((records/50000)+1);
}
System.out.println("开始写入Excel文件,记录数:"+records+",sheet数:"+sheetNum);
int rowNums = 0;
for (int i = 1; i <= sheetNum; i++) {
Sheet sheet = wb.createSheet();
sheet.setDefaultColumnWidth(25);
if(i==sheetNum){
rowNums = records-((sheetNum-1)*50000);
for (int j = 0; j < rowNums; j++) {
Row row = sheet.createRow(j);
row.createCell(0).setCellValue(String.valueOf(start));
start=start.add(BigInteger.valueOf(1L));
}
}else{
for (int j = 0; j < 50000; j++) {
Row row = sheet.createRow(j);
row.createCell(0).setCellValue(String.valueOf(start));
start=start.add(BigInteger.valueOf(1L));
}
}
System.out.println("第"+i+"张sheet写入完成...");
}
wb.write(fileOut);
fileOut.close();
wb.close();
System.out.println("写入完成!");
} catch (Exception e) {
e.printStackTrace();
}
}
}

相关标签: poi excel java