繁体   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