简体   繁体   English

使用Java,Apache POI在每3列之后在Excel中创建列标题

[英]Create column headers in Excel after every 3 Columns using java, Apache POI

I'm trying to create an excel sheet wherein I'm getting all the Header values from an ArrayList and looping it to print the headers after every 3 cells. 我正在尝试创建一个Excel工作表,其中要从ArrayList中获取所有Header值,并使其循环以在每3个单元格后打印标题。 This is my code. 这是我的代码。

    for (int x = 4; x < projectLocationList.size(); x++) {
        columnHeaderCell = regionList.createCell(x+3);
        columnHeaderCell.setCellValue(projectLocationList.get(x-3));
        columnHeaderCell.setCellStyle(columnHeaderStyle);
    }

For x=4 because I have to print column from 7th cell. 对于x=4因为我必须从第7个单元格打印列。 The problem I'm facing is I'm getting only the last three values. 我面临的问题是仅获取最后三个值。 Please help me fix this for loop. 请帮助我解决此循环问题。 Thanks. 谢谢。

initialize for loop from 0 only and take a different variable outside the for loop for creating cell number. 仅从0初始化for循环,并在for循环外采用其他变量来创建单元号。

static int cellNum = 7;
for (int x = 0; x < projectLocationList.size(); x++) {
    columnHeaderCell = regionList.createCell(cellNum);
    columnHeaderCell.setCellValue(projectLocationList.get(x));
    columnHeaderCell.setCellStyle(columnHeaderStyle);
cellNum = cellNum + 3;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM