Export mysql data for Excel using Apache POI

I have a class MysqlToXls

copied here: http://mikescode.wordpress.com/2008/02/16/exporting-a-mysql-table-to-excel-xls-in-java/

I edited a class that creates a constructor that doesn't need any parameter like this:

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);
}

      

There is no tutorial yet, I am trying to do it myself and I cannot.

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

      

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

+3


source to share


2 answers


the only problem was the file path. I tried to save the file in the same project folder (with a relative path) and if I give an absolute path (like on the desktop) it works great!



+1


source


Maybe you need to explicitly close the outputStream, so you need to do this:

xlsWorkbook.write(new FileOutputStream(filename));

      



you should try to do something like this:

FileOutputStream fos = new FileOutputStream(filename);
xlsWorkbook.write(fos);
fos.close();

      

+1


source







All Articles