简体   繁体   English

当我尝试使用循环在 excel 表中写入行和列时,仅打印最后一个循环值

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

I want to print all the rows and column in my excel, but it always print the last column alone(ie whatever writes in the last loop).我想打印 excel 中的所有行和列,但它总是单独打印最后一列(即在最后一个循环中写入的任何内容)。

Is there a way to get print all the values and not overwrite the excel again and again.有没有办法打印所有的值而不是一次又一次地覆盖 excel。 below is the program i used please let me know where i have mistaken.以下是我使用的程序,请让我知道我在哪里弄错了。

I want the 1st Column get printed first and then the 2nd column.我希望先打印第一列,然后再打印第二列。 Don't want the row to get print first不希望该行首先打印


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这是我想要的结果

这是我想要的结果

Look at the comments as well.也看看评论。

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

}

please try this code请尝试此代码

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

ok for you case try this and get back to me i cant test this.好吧,你试试这个,然后回复我,我无法测试这个。 cause i don't have the source code.因为我没有源代码。

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.

相关问题 在for循环中使用row.getlastcellnum()方法在Selenium中写入Excel工作表会导致java.lang.IllegalArgumentException - Write excel sheet in Selenium using row.getlastcellnum() method in for loop causes java.lang.IllegalArgumentException 如何使用jlx在Java的同一Excel工作表中写入数据循环? - How to write loop of data in the same excel sheet in Java using jlx? 无法通过多个for循环写入Excel行列(apache poi) - Not able to write excel row column (apache poi) by multiple for loop 如何在每个循环实例的新行中将数据写入excel工作表而不替换以前的数据,即如何为每个循环实例追加行? - how to write data into excel sheet in new row for every loop instance without replacing previous data i.e how to append rows for every loop instance? 我必须编写一个 for 循环来打印数组中字符的 ASCII 值 - I have to write a for loop to print the ASCII values of chars which are in an array 从webtable中获取值并在循环中的每一行中一一写入excel表中 - Fetching value from webtable and write in excel sheet one by one of each row in a loop 使用for循环写入Excel文件中的单元格,删除上一列 - Using a for loop to write into cells in an Excel file, removes the previous column 如何仅打印循环中的最后一个值 - How to print only last value from the loop 如何使用Apache POI获取java中各行excel表的最后一列值 - how to get last column value of respective row of excel sheet in java using Apache POI 在for循环的最后一次迭代中打印 - Print on last iteration of for loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM