簡體   English   中英

當我嘗試使用循環在 excel 表中寫入行和列時,僅打印最后一個循環值

[英]When i Try to write row and column in an excel sheet using a loop only the last loop values gets print

我想打印 excel 中的所有行和列,但它總是單獨打印最后一列(即在最后一個循環中寫入的任何內容)。

有沒有辦法打印所有的值而不是一次又一次地覆蓋 excel。 以下是我使用的程序,請讓我知道我在哪里弄錯了。

我希望先打印第一列,然后再打印第二列。 不希望該行首先打印


public class test {

    public static void main(String[] args) throws IOException, Exception  {
        XSSFWorkbook wb=new XSSFWorkbook();
        XSSFSheet spreadsheet=wb.createSheet();
        FileOutputStream fos=null;

        for(int j=0; j<4; j++) {
            for(int i=0; i<10; i++) {
                Row row=spreadsheet.createRow(i);
                Cell value=row.createCell(j);
                value.setCellValue(i+" Test");
                fos=new FileOutputStream("Testexcel.xlsx");
                wb.write(fos);
            }
        }

        wb.close();
        fos.close();
    }
}

這是我得到的結果

這是我得到的結果

這是我想要的結果

這是我想要的結果

也看看評論。

public static void main(String[] args) throws IOException {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet spreadsheet = wb.createSheet();
    FileOutputStream fos = null;

    for (int row = 0; row < 10; row++) {
        XSSFRow xssfRow = spreadsheet.createRow(row); // row should be created only once per iteration
        for (int col = 0; col < 4; col++) {
            XSSFCell value = xssfRow.createCell(col);
            value.setCellValue(row + " Test"); // changes as per your need
            // value.setCellValue("("+ row + "," + col + ") Test");

        }
    }

    fos = new FileOutputStream("Testexcel.xlsx"); // this should be done at the end, not within the loop
    wb.write(fos);
    wb.close();
    fos.close();

}

請嘗試此代碼

public class test {

    public static void main(String[] args) throws IOException, Exception  {
        XSSFWorkbook wb=new XSSFWorkbook();
        XSSFSheet spreadsheet=wb.createSheet();
        FileOutputStream fos=null;
        for(int i=0; i<10; i++) {
            Row row=spreadsheet.createRow(i);
            for(int j=0; j<4; j++) {
                Cell value=row.createCell(j);
                value.setCellValue(i+" Test");
                
            }
        }
       fos=new FileOutputStream("Testexcel.xlsx");
       wb.write(fos);

        wb.close();
        fos.close();
    }
}

好吧,你試試這個,然后回復我,我無法測試這個。 因為我沒有源代碼。

public class test {

public static void main(String[] args) throws IOException, Exception  {
    XSSFWorkbook wb=new XSSFWorkbook();
    XSSFSheet spreadsheet=wb.createSheet();
    FileOutputStream fos=null;
    for(int i=0; i<10; i++) {
        Row row=spreadsheet.createRow(i);
    }
    for(int j=0; j<4; j++) {
         for(int i=0; i<10;i++){
           Row row = spreadsheet.getRow(i);
            Cell value=row.createCell(j);
            value.setCellValue(i+" Test");
            }
        }
   fos=new FileOutputStream("Testexcel.xlsx");
   wb.write(fos);

    wb.close();
    fos.close();
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM