2

I'm trying to create two different sheets on a single excel file . But only getting the first sheet.How can I get the second sheet ? where did I mistake ? Please help

Below is the code :

        WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
        WritableWorkbook workbook = Workbook.createWorkbook(new File(filename), ws);
        WritableSheet s = workbook.createSheet("Summary", 0);

        // set font ,border,alignment
        cf.setWrap(true);

        Label l = null;
        l = new Label(0, 0, "Ticket Product", cf);
        s.addCell(l);
        ............................................
        ............................................
        // code to load data on the first sheet


        int columns = s.getColumns();
        for (i = 0; i < columns; i++) 
        {
            //write on excel columnwise
        }
        workbook.write();

        s = workbook.createSheet("Details", 1);

        // set font ,border,alignment
        cf.setWrap(true);

        l = null;
        l = new Label(0, 0, "Ticket Product", cf);
        s.addCell(l);
        ............................................
        ............................................
        //code to load data on the second sheet

        columns = s.getColumns();
        for (i = 0; i < columns; i++) 
        {
          //the same loop as before to write columnwise
        }

        workbook.write();
        workbook.close();
1
  • 1
    What's Workbook? I'm assuming you're using some third party library (could it be Apache POI?). If that's the case, you should mention it in your question, and perhaps include the version number too, it might be relevant to your problem. Commented Nov 25, 2014 at 10:59

2 Answers 2

3
    WorkbookSettings ws = new WorkbookSettings();
    ws.setLocale(new Locale("en", "EN"));
    WritableWorkbook workbook = Workbook.createWorkbook(new File(filename), ws);
    WritableSheet sheet1 = workbook.createSheet("Summary", 0);//Create the first sheet
    WritableSheet sheet2 = workbook.createSheet("Details", 1);//Create the second sheet
    // set font ,border,alignment
    cf.setWrap(true);

    Label l = null;
    l = new Label(0, 0, "Ticket Product", cf);
    sheet1.addCell(l);
    ............................................
    ............................................
    // code to load data on the first sheet


    int columns = sheet1.getColumns();
    for (i = 0; i < columns; i++) 
    {
        //write on excel columnwise in sheet 1
    }

   int columnsheet2 = sheet2.getColumns();
    for (i = 0; i < columns; i++) 
    {
        //write on excel columnwise in sheet 2
    }
    workbook.write();

    s = workbook.createSheet("Details", 1);
    //here is the mistake you are using the same 's' object.

    // set font ,border,alignment
    cf.setWrap(true);`enter code here`

    l = null;
    l = new Label(0, 0, "Ticket Product", cf);
    sheet1.addCell(l);
    ............................................
    ............................................
    //code to load data on the second sheet

    columns = s.getColumns();
    for (i = 0; i < columns; i++) 
    {
      //the same loop as before to write columnwise
    }

    workbook.write();
    workbook.close();
Sign up to request clarification or add additional context in comments.

Comments

1

You have to use workbook.setSheetOrder(sheetName,pos) , and you can have the logic to increment the pos variable as required.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.