3

I have the class MysqlToXls copied from here: http://mikescode.wordpress.com/2008/02/16/exporting-a-mysql-table-to-excel-xls-in-java/

I edited the class making a constructor that doesn't need any parameter in this way:

public MysqlToXls()
throws ClassNotFoundException, SQLException {

    // Create MySQL database connection
    Class.forName("com.mysql.jdbc.Driver");

    String url = "jdbc:mysql://localhost/Spinning?user=root&useUnicode=true&characterEncoding=utf8";
    connection = DriverManager.getConnection(url);
}

While there is not any guide I try to do myself and I'm not able.

  MysqlToXls m=new MysqlToXls();
  m.generateXls("utente", "utenti.xls");

But there are no errors and the file utenti.xls remains blank. Does someone know where is the problem?

2
  • Do you have any data in the table utente? Does the ResultSet in generateXls() contain any results? Commented Feb 9, 2013 at 13:01
  • yes I have data and the rs is not empty.. Commented Feb 9, 2013 at 13:03

2 Answers 2

1

It is possible that you have to explicitly close the outputStream, so insted of doing this:

xlsWorkbook.write(new FileOutputStream(filename));

you should try to do something like this:

FileOutputStream fos = new FileOutputStream(filename);
xlsWorkbook.write(fos);
fos.close();
Sign up to request clarification or add additional context in comments.

2 Comments

Actually I tried the linked code and it worked like a charm. So closing the output stream will not resolve your problem.
Yes, no problem at all with the original code. Sorry, I have no clue.
1

the only problem was the path of the file. I was trying to save the file in one folder of the project (with a relative path), while if I give the absolute path (f.e. on the desktop) it works perfectly!

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.