2014-11-25 51 views
0

我試圖在一個excel文件上創建兩個不同的工作表。但只能得到第一張。如何獲得第二張?我在哪裏錯了?請幫助在excel上創建多個工作表在

下面是代碼:

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

什麼是「工作簿」?我假設你正在使用一些第三方庫(可能是Apache POI?)。如果是這樣的話,你應該在你的問題中提及它,也許還包括版本號,它可能與你的問題有關。 – AJPerez 2014-11-25 10:59:46

回答

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

它的工作。感謝很多。 – Shahrin 2014-11-25 11:11:26

1

你必須使用workbook.setSheetOrder(SHEETNAME,POS),你可以有遞增POS變量作爲邏輯需要。