我从这里复制了类 MysqlToXls
:
http://mikescode.wordpress.com/2008/02/16/exporting-a-mysql-table-to-excel-xls-in-java/
我以这种方式编辑了一个不需要任何参数的构造函数的类:
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);
}
虽然没有任何指南,但我尝试自己做,但我做不到。
MysqlToXls m=new MysqlToXls();
m.generateXls("utente", "utenti.xls");
但是没有错误,文件 utenti.xls 仍然是空白的。 有人知道问题出在哪里吗?
最佳答案
您可能必须显式关闭 outputStream,所以不要这样做:
xlsWorkbook.write(new FileOutputStream(filename));
你应该尝试做这样的事情:
FileOutputStream fos = new FileOutputStream(filename);
xlsWorkbook.write(fos);
fos.close();
关于java - 使用 Apache POI 从 Java 将 mysql 数据导出到 excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14788407/