[英]Multiple Resultsets to excel file in Java
I have a question about Resultsets to Excel file in Java. 我对用Java将结果集转换为Excel文件有疑问。 Let me explain scenario;
让我解释一下情况;
I have many sql queries and many resultsets under different classes. 我在不同的类下有很多sql查询和很多结果集。
This resultset returns; 该结果集返回; Example1 50 20
示例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(); }
}
}
This resultset returns; 该结果集返回; Example2 100 50
示例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(); }
}
}
I want write these resultsets to Excel file as below screenshot; 我想将这些结果集写入Excel文件,如下图;
I try use Apache POI Project for output excel file but i don't understand many things. 我尝试将Apache POI项目用于输出excel文件,但我不了解很多事情。 Do you have any simple solutions i wonder.
我想知道您有任何简单的解决方案吗?
Look into Comma-separated Values (CSV) as a simple format. 将逗号分隔值(CSV)作为一种简单格式。 All versions of Excel support CSV files, and you can easily get the output you want using a basic Java FileWriter.
所有版本的Excel都支持CSV文件,并且您可以使用基本的Java FileWriter轻松获得所需的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.