How to write data from arrayList to multiple excel sheets

mayank bisht

I have data stored in ArrayList ,and i want to write that data in excel workbook in multiple sheets. I am able to write data in excel workbook, but it's writing in same sheet. As of now i am able to write data in the following way, as shown below in the pic:

enter image description here

But I want to break data into multiple sheets, Data should break from headers i,e S.NO,Col1...Col6, when ever this comes up, the data should write to new sheet.

I am using this code to write data into excel

for (int i = 0; i < listOfResults.size(); i++) {
            row = sheet.createRow(i);
            for (int j = 0; j < 7; j++) {
                cell = row.createCell(j);
                if (j + add < listOfResults.size()) {
                    cell.setCellValue(listOfResults.get(j + add));

                }
            }

            add += 7;
        }

Please help..

jmarkmurphy

This is not so much a POI problem as a break logic problem.

Your Code

for (int i = 0; i < listOfResults.size(); i++) {
    row = sheet.createRow(i);
    for (int j = 0; j < 7; j++) {
        cell = row.createCell(j);
        if (j + add < listOfResults.size()) {
            cell.setCellValue(listOfResults.get(j + add));
        }
    }
    add += 7;
}

There is nothing here to detect the header line, and thus you are not creating a new sheet. You need to detect the break, then create a new sheet, and reset the row index to zero for each header.

Modified Code

int rowIndex = 0;
int firstCellInRow = 0;
int cellCount = listOfResults.size();
int cellsPerRow = 7;
int rowCount = cellCount / cellsPerRow;

for (int i = 0; i < rowCount; i++) {

    // detect first header cell
    firstCellInRow = i * cellsPerRow;
    if (listOfResults.get(firstCellInRow).equals("S.NO")) {
        // create a new sheet
        sheet = wb.createSheet();
        // reset the row index to 0
        rowIndex = 0;
    }

    // create the row and increment the row index
    row = sheet.createRow(rowIndex++);

    // now add the data to the cells
    for (int j = 0; j < cellsPerRow; j++) {
        cell = row.createCell(j);
        if (firstCellInRow + j < listOfResults.size()) {
            cell.setCellValue(listOfResults.get(firstCellInRow + j));
        }
    }
}

The other issue is that you are running through the main loop too many times. You are looping through that once for each cell, but you really want to loop through once per row. This is the reason for the division of the length of the list (number of cells) by 7 (number of cells per row) in the first loop.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Write list of data frames to Excel in multiple sheets

From Dev

How to import data from multiple excel sheets in GAMS using loops?

From Dev

Extract data from multiple Excel sheets

From Dev

Write multiple data frames to correct excel sheets using for loops

From Dev

How do I write a countifs function across multiple sheets that pulls from filtered data?

From Java

How to write to a txt data from an ArrayList

From Dev

Pull data from multiple Excel sheets and count specific items

From Dev

Excel - Copy data from multiple sheets to one sheet

From Dev

Exporting Data To Multiple Excel Sheets

From Dev

Excel Chart from Multiple Sheets

From Dev

How to autopopulate data from multiple sheets to one sheet

From Dev

Write data frames from two lists to two separate sheets of an excel file using apply family

From Dev

Copy data from multiple excel sheets and append that to a single excel sheet using VBScript

From Dev

Excel VBA Macro - Copying data from multiple sheets in existing file, and creating new file and pasting selected data into separate sheets

From Dev

Multiple sheets export from Excel to XML

From Dev

transfer from c# to excel with multiple sheets

From Dev

Multiple sheets export from Excel to XML

From Dev

Concatenate cells from multiple sheets in Excel

From Dev

Count values from multiple sheets in Excel

From Dev

Import multiple sheets from excel spreadsheet into r

From Java

How do I write to individual excel sheets for each dataframe generated from for loop?

From Dev

Write multiple sheets in excel file using ByteScout Spreadsheet

From Dev

How to open an excel file with multiple sheets in pandas?

From Dev

How to use Index over multiple sheets Excel

From Dev

Consilidating and Transferring data from multiple sheets

From Dev

VBA - Source data from multiple sheets

From Dev

How to read data from multiple files and write into columns of a single file

From Dev

How to Save, Separate, and Write Data from File to Multiple TextBoxes

From Dev

How to parse DataFrame with specific column and write it to different excel sheets

Related Related

  1. 1

    Write list of data frames to Excel in multiple sheets

  2. 2

    How to import data from multiple excel sheets in GAMS using loops?

  3. 3

    Extract data from multiple Excel sheets

  4. 4

    Write multiple data frames to correct excel sheets using for loops

  5. 5

    How do I write a countifs function across multiple sheets that pulls from filtered data?

  6. 6

    How to write to a txt data from an ArrayList

  7. 7

    Pull data from multiple Excel sheets and count specific items

  8. 8

    Excel - Copy data from multiple sheets to one sheet

  9. 9

    Exporting Data To Multiple Excel Sheets

  10. 10

    Excel Chart from Multiple Sheets

  11. 11

    How to autopopulate data from multiple sheets to one sheet

  12. 12

    Write data frames from two lists to two separate sheets of an excel file using apply family

  13. 13

    Copy data from multiple excel sheets and append that to a single excel sheet using VBScript

  14. 14

    Excel VBA Macro - Copying data from multiple sheets in existing file, and creating new file and pasting selected data into separate sheets

  15. 15

    Multiple sheets export from Excel to XML

  16. 16

    transfer from c# to excel with multiple sheets

  17. 17

    Multiple sheets export from Excel to XML

  18. 18

    Concatenate cells from multiple sheets in Excel

  19. 19

    Count values from multiple sheets in Excel

  20. 20

    Import multiple sheets from excel spreadsheet into r

  21. 21

    How do I write to individual excel sheets for each dataframe generated from for loop?

  22. 22

    Write multiple sheets in excel file using ByteScout Spreadsheet

  23. 23

    How to open an excel file with multiple sheets in pandas?

  24. 24

    How to use Index over multiple sheets Excel

  25. 25

    Consilidating and Transferring data from multiple sheets

  26. 26

    VBA - Source data from multiple sheets

  27. 27

    How to read data from multiple files and write into columns of a single file

  28. 28

    How to Save, Separate, and Write Data from File to Multiple TextBoxes

  29. 29

    How to parse DataFrame with specific column and write it to different excel sheets

HotTag

Archive