简体   繁体   English

使用Apache POI写入xlsx文件,在文件的最后一列中获得预期的答案

[英]Using Apache POI to write in xlsx file, Getting expected answer in the last column of the file

I am learning how to write in the xlsx file using Apache poi,Now in the below code i am using 2 array. 我正在学习如何使用Apache poi在xlsx文件中编写代码,现在在下面的代码中我正在使用2数组。 1.Month 2.Logging_Hours. 1.Month.Logging_Hours。

I am using Month array to assign the column name in the first row and it work fine for me.Now what i want the another Array in my code Logging_Hours print in each column but i am not getting the expected answer by using the below code. 我正在使用Month数组在第一行中分配列名称,它对我来说很好。现在我想要在我的代码Logging_Hours中的每个列中打印另一个数组,但是我无法通过使用以下代码得到预期的答案。

For_Expected refer to the screen :"Expected_Xlsx" For_Actual refer to the screen :"Actual_Xlsx" For_Expected参见屏幕:“ Expected_Xlsx” For_Actual参见屏幕:“ Actual_Xlsx”

public class Writing_xlsx {

  public static void main(String[] args) throws IOException {

    Workbook wb = new XSSFWorkbook();

    Sheet sheet1=wb.createSheet("SheetOne");

    String [] Month={"January" , "Feb", "March","Apr"};
    int [] Logging_Hours={ 7 ,5, 9,10};
    int f=0;
    System.out.println(Month[0]);

    Row r=sheet1.createRow(f);

    for(int i=0;i<4;i++){

    r.createCell(i).setCellValue(Month[i]);   

    }

    for(int c=0;c<4;c++){}
        int d=0;
        while(d<4){

        for(int rn=1;rn<=4;rn++)    {

            r=sheet1.createRow(rn);
            r.createCell(d).setCellValue(Logging_Hours[rn-1]);
            System.out.println(Logging_Hours[rn-1]);

        }
        d++;
        }

        FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");

    wb.write(fileOut);
    fileOut.close();    
    wb.close();

}

}

For_Expected refer to the screen :"Expected_Xlsx" For_Expected请参见屏幕:“ Expected_Xlsx”

这是运行程序后的预期屏幕

For_Actual refer to the screen :"Actual_Xlsx" For_Actual参见屏幕:“ Actual_Xlsx”

这是我得到的实际屏幕

Thank You in advance ,Sorry for the bad code i am just in a learning phase. 预先谢谢您,很抱歉我的代码不好,我正处于学习阶段。

You will need two for loops (nested) to write the data, eg: 您将需要两个for循环(嵌套)来写入数据,例如:

for (int rn = 1; rn <= 4; rn++) {
    Row row = sheet1.createRow(rn);
    for (int i = 0; i < 4; i++) {
        row.createCell(i).setCellValue(Month[rn-1]);
    }
}

Current for loop creates only one cell per row and writes the value into it whereas we need to write the values in all 4 cells per row. Current for循环每行仅创建一个cell并将其写入其中,而我们需要在每行所有4个单元格中写入值。

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

相关问题 使用Java中的Apache POI将16位字符写入.xlsx文件 - Write 16 bits character to .xlsx file using Apache POI in Java 使用Apache POI将数据写入Java中的xls,xlsx,csv文件 - Write data to xls,xlsx,csv file in java using apache POI 对XLSX Apache poi使用Java临时文件 - using Java temporary file for XLSX Apache poi 使用Apache POI加载xlsx文件时发生NullpointerException - NullpointerException on loading xlsx file using Apache POI 使用apache POI将xlsx文件分页到XSSFworkbook - pagination of xlsx file to XSSFworkbook using apache POI Java - Apache POI - 读取/写入.xlsx 文件 - 文件损坏并变为空 - Java - Apache POI - Read/Write .xlsx file - File getting corrupted and becomes empty Apache POI SXSSFWorkbook 在 XLSX 文件 Memory 问题中写入大约 500.000 - Apache POI SXSSFWorkbook write around 500.000 in XLSX file Memory issue 如何在android中使用apache POI在现有的xls或xlsx文件上写入数据 - how to write data on existing xls or xlsx file using apache POI in android 使用 apache poi 根据列名将 CSV/XLSX 文件拆分为多个工作表 - Split CSV/XLSX file into multiple sheets based on column names using apache poi 如何使用 apache poi 验证 java 中每个 header 的 xlsx 文件 Header 名称和列值? - How to validate xlsx File Header Name and column value for each header in java using apache poi?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM