我有一个关于 Java 中的结果集到 Excel 文件的问题。让我解释一下场景;
我有很多sql查询和不同类下的很多结果集。
该结果集返回; 示例1 50 20
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package program;
import java.sql.*;
/**
*
* @author Lacrymae_Ev
*/
public class SQLQueries1{
loginscreen logindetails = new loginscreen();
private static Statement stmt;
private static final String query = "select 'Example1' as Example1,sum(dur) as dur,sum(tot)as tot from table1 with(nolock)\n" +
"where date between '2013-07-01 00:00:00.000' and '2013-07-01 23:59:59.999'";
/**
* @param args the command line arguments
* @throws java.sql.SQLException
*/
public static void main(String[] args) throws SQLException {
// TODO code application logic here
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://192.168.200.10;" + "databaseName=ExampleDB;" + "user=exampleuser;" + "password=examplepassword;";
Connection con = DriverManager.getConnection(connectionUrl);
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String Example1 = rs.getString("Example1");
int dur = rs.getInt("dur");
int tot = rs.getInt("tot");
System.out.println(Example1 + "\t" + dur + "\t" + tot);
}
}
catch (SQLException e) {
System.out.println("Wrong id or password!");
}
catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
finally {
if (stmt != null) { stmt.close(); }
}
}
该结果集返回; 示例2 100 50
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package program;
import java.sql.*;
/**
*
* @author Lacrymae_Ev
*/
public class SQLQueries2{
loginscreen logindetails = new loginscreen();
private static Statement stmt;
private static final String query = "select 'Example2' as Example1,sum(dur) as dur,sum(tot)as tot from table1 with(nolock)\n" +
"where date between '2013-07-01 00:00:00.000' and '2013-07-01 23:59:59.999'";
/**
* @param args the command line arguments
* @throws java.sql.SQLException
*/
public static void main(String[] args) throws SQLException {
// TODO code application logic here
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://192.168.200.10;" + "databaseName=ExampleDB;" + "user=exampleuser;" + "password=examplepassword;";
Connection con = DriverManager.getConnection(connectionUrl);
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String Example2 = rs.getString("Example2");
int dur = rs.getInt("dur");
int tot = rs.getInt("tot");
System.out.println(Example1 + "\t" + dur + "\t" + tot);
}
}
catch (SQLException e) {
System.out.println("Wrong id or password!");
}
catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
finally {
if (stmt != null) { stmt.close(); }
}
}
我想将这些结果集写入 Excel 文件,如下图所示;
我尝试使用 Apache POI 项目输出 excel 文件,但我不明白很多事情。我想知道你有什么简单的解决方案吗?
最佳答案
查看Comma-separated Values (CSV)作为一个简单的格式。所有版本的 Excel 都支持 CSV 文件,您可以使用基本的 Java FileWriter 轻松获得所需的输出。
关于java - Java 中的多个结果集到 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21433854/