使用Apache POI将mysql数据导出为Java

时间:2022-05-03 20:24:38

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

我从这里复制了MysqlToXls类: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?

但是没有错误,文件utenti.xls仍然是空白的。有谁知道问题出在哪里?

2 个解决方案

#1


1  

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

您可能必须显式关闭outputStream,因此必须这样做:

xlsWorkbook.write(new FileOutputStream(filename));

you should try to do something like this:

你应该尝试做这样的事情:

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

#2


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!

唯一的问题是文件的路径。我试图将文件保存在项目的一个文件夹中(带有相对路径),而如果我给出绝对路径(f.e.在桌面上)它完美地工作!

#1


1  

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

您可能必须显式关闭outputStream,因此必须这样做:

xlsWorkbook.write(new FileOutputStream(filename));

you should try to do something like this:

你应该尝试做这样的事情:

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

#2


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!

唯一的问题是文件的路径。我试图将文件保存在项目的一个文件夹中(带有相对路径),而如果我给出绝对路径(f.e.在桌面上)它完美地工作!