如何格式化POI创建的Excel文档

时间:2015-09-12 17:46:23

标签: java excel apache-poi

我正在使用POI创建Excel文档的应用程序中工作。问题是,当生成没有任何数据的Excel时很好,但是当我用它放入数据时,标题会下降。我的代码:

            HSSFWorkbook hwb = null;
            hwb = new HSSFWorkbook();
            HSSFSheet sheet = hwb.createSheet("new sheet");

            HSSFRow rowhead = sheet.createRow((short) largest);

            HSSFCell intervieweeName = rowhead.createCell(0);
            intervieweeName.setCellValue("Interviee Name");

            HSSFCell interviewTiming = rowhead.createCell(1);
            interviewTiming.setCellValue("Interview Timing");

    for (int large = 0; large < largest; large++) {

            int i=0;
            HSSFRow array1 = sheet.createRow((short)large);
            for ( i = i + large; i < interviewerNameList.size(); i++) { 
            HSSFCell tracker1 = array1.createCell(0);
            HSSFCell tracker2 = array1.createCell(1);

            tracker1.setCellValue(interviewerNameList.get(large));
            tracker2.setCellValue(interviewTimingToFrom1.get(large));

            }
            }

问题在于标题Interviee Name和Interview Timing正在追踪值。

1 个答案:

答案 0 :(得分:1)

您的问题是您将标题写入最后一行

HSSFRow rowhead = sheet.createRow((short) largest);
...
for (int large = 0; large < largest; large++) {
    HSSFRow array1 = sheet.createRow((short)large);

将标题放在最后,并从0循环到该标题以创建数据行

将其更改为:

HSSFRow rowhead = sheet.createRow(0);
...
for (int large = 0; large < largest; large++) {
    HSSFRow array1 = sheet.createRow(large+1);

这样,large变量对于数据查找保持不变,但是从第1行开始填充