微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我尝试使用循环在 Excel 工作表中写入行和列时,仅打印最后一个循环值 这是我得到的结果这就是我想要的结果

如何解决当我尝试使用循环在 Excel 工作表中写入行和列时,仅打印最后一个循环值 这是我得到的结果这就是我想要的结果

我想打印我的 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();
    }
}

这是我得到的结果

This is the result I got

这就是我想要的结果

This is the result I want

解决方法

看看评论。

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();
 }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。