简体   繁体   中英

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).

Is there a way to get print all the values and not overwrite the excel again and again. 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();
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM