1

package com.xxxx;

import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import jxl.read.biff.BiffException;

import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class U_WriteExcel {
    static WritableWorkbook wworkbook;
    static WritableSheet wsheet;
    static {
         try {
            wworkbook = Workbook.createWorkbook(new File("output.xls"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
       public static void execute(int startRow, int startColumn, String content) throws IOException, RowsExceededException, WriteException, BiffException
         // throws BiffException, IOException, WriteException
       {

          wsheet = wworkbook.createSheet("Test Result", 0);
          Label label = new Label(0, 0, "Test Case Name");
          Label label1 = new Label(1, 0, "Result");
          wsheet.addCell(label);
          wsheet.addCell(label1);


          Label writeResult = new Label(startRow,startColumn, content);
          System.out.println(startRow);
          System.out.println(startColumn);
          System.out.println(content);
          wsheet.addCell(writeResult);
          wworkbook.write();                  
       }

    }

I am calling the above class from another class...

U_WriteExcel.execute(0,1,"Test Case 1");
U_WriteExcel.execute(1,1,"PASSED");
U_WriteExcel.execute(0,2,"Test Case 2");
U_WriteExcel.execute(1,2,"PASSED");

It is nothing written on the sheet. How to resolve this??

ChanGan
  • 4,254
  • 11
  • 74
  • 135

1 Answers1

0

As I see here and there in those examples always WritableWorkbook.close() is called at the end.

I guess that you are missing just that so

        ...
        wworkbook.write();
        wworkbook.close();
    }
}

and you should be just fine.

Community
  • 1
  • 1
TobiMcNamobi
  • 4,687
  • 3
  • 33
  • 52
  • Hi, I tried earlier this one Wwrork.close() but if i add, it print first Test Case 1 and failed with some exception as gave static workbook.. – ChanGan Apr 30 '14 at 11:40
  • That's maybe because you called `Workbook.createWorkbook(new File("output.xls"));` only once. OR you have to do `wworkbook.close();` at the very end somehow (i.e. after all the `U_WriteExcel.execute()` calls). – TobiMcNamobi Apr 30 '14 at 13:56