tags:

views:

137

answers:

3
public static void write () throws IOException, WriteException { 
  WorkbookSettings settings = new WorkbookSettings(); 
  File seurantaraportti = new File("ta.xls"); 
  WritableWorkbook seurw = Workbook.createWorkbook(ta,settings); 
  seurw.createSheet("ta", 0); 
  WritableSheet ws = seurw.getSheet(0); 
  addNumber(ws,0,0,100.0); 
  seurw.close(); 
} 

private static void addNumber(WritableSheet sheet, int column, int row, Double d) 
    throws WriteException, RowsExceededException { 
  Number number=new Number(column, row,d); 
  sheet.addCell(number); 
} 

This creates a excel file which gives file format is not valid error when trying to handle it, not proper excel file with a number added on it. Help is indeed needed!

A: 

writableWorkbook seurw = Workbook.createWorkbook(ta,settings);

must be

writableWorkbook seurw = Workbook.createWorkbook("ta",settings);

Grumpy
-1: It's obvious that the code wouldn't even compile if it really looked like that, so it's a typo. If you really want to correct this, write it as a comment. Posting answers that aren't an answer only prevents other people who might be able to help from looking at the question.
chiccodoro
wont it compile with a undefined var? Did the other answer not mention exactly the same??
Grumpy
Not it won't compile with an undefined variable in Java (except if you use the Eclipse compiler which allows partial compilation, but you still wouldn't be able to run the program). Chiccodoro's point is valid, though I find the reaction a bit harsh as we were just trying to lead the OP to clarify his question. That being said I deleted my answer as someone posted a solution now that the problem has been clarified.
haylem
@haylem: I don't mean to be harsh, but if as you say my point is valid, what's wrong with downvoting? It's nothing else than rating the answer, and since the answer doesn't help at all but turns a question with "0 answers" to one with "2 answers" or more which can mislead some people not to view it at all, it would be better for the OP if there was no answer at all. Thus my rating.
chiccodoro
@chiccodoro: and thus my own deletion :) I do agree with you, and that's also why after posting my question I realized that simply asking for details in a comment to the question itself was better (I'm new here). I didn't mind the intent of your downvote and comment. Harsh was maybe too strong a word. Maybe just "blunt" :)
haylem
@haylem: Maybe "harsh" was a bit "harsh", or is that too harsh? :-)
chiccodoro
@chiccodoro: see, you see my point.
haylem
hmmm, trying to help here. I dont care about the downvote. I recognized a error in the code, posted it and get a whole bunch of crap over me.
Grumpy
A: 

Yes I edited a bit when posting here and the orginal code really is that File ta =new File... sORRY. But the problem is still the same...

jaana
Sorry I could not add above as comment for some reason...
jaana
It works! Thank you so much!!!!
jaana
@jaana..what is working? If my answer helped you please accept it as answer.
johnbk
+1  A: 

You are not writing anything to the workbook. you are missing

seurm.write()

before closing the workbook

seurw.close();

Below is the working code.

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

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class WriteExcel {

    public static void write() throws IOException, WriteException {
        WorkbookSettings settings = new WorkbookSettings();
        // settings.setLocale(new Locale("en", "EN"));
        File ta = new File("ta.xls");
        WritableWorkbook seurw = Workbook.createWorkbook(ta, settings);
        seurw.createSheet("ta", 0);
        WritableSheet ws = seurw.getSheet(0);
        addNumber(ws, 0, 0, 100.0);
        seurw.write(); // You missed this line.
        seurw.close();
    }

    private static void addNumber(WritableSheet sheet, int column, int row,
            Double d) throws WriteException, RowsExceededException {
        Number number = new Number(column, row, d);
        sheet.addCell(number);
    }

    public static void main(String[] args) {
        try {
            write();
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
johnbk