java - Java 中的多个结果集到 excel 文件

标签 java export-to-excel resultset

我有一个关于 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 文件,如下图所示;

Excel File

我尝试使用 Apache POI 项目输出 excel 文件,但我不明白很多事情。我想知道你有什么简单的解决方案吗?

最佳答案

查看Comma-separated Values (CSV)作为一个简单的格式。所有版本的 Excel 都支持 CSV 文件,您可以使用基本的 Java FileWriter 轻松获得所需的输出。

关于java - Java 中的多个结果集到 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21433854/

相关文章:

java - rs.last() 给出 Invalid operation for forward only resultset : last

java - 使用 spring boot 应用程序启用 HTTP 严格传输安全 (HSTS)

java - ViewPager 显示页面不按顺序

java - Eclipse 未导入 com.google.api.client。进口?

jquery - 如何将jqgrid的过滤内容导出到Excel?

mysql - MySQL 中 Sphinx 结果集大于 16 MB 的问题

java - 使用 Jaxb2Marshaller 防止解析包含 DTD 的 XML 文件

excel - SSRS 2008 R2 excel 导出在报表服务器上不起作用

javascript - 在alasql(AngularJS)的SQL语句中使用 "AS"子句

java - 在 Java 中关闭 ResultSet 后不允许操作