简体   繁体   中英

Multiple Resultsets to excel file in Java

I have a question about Resultsets to Excel file in Java. Let me explain scenario;

I have many sql queries and many resultsets under different classes.

This resultset returns; Example1 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

/*
 * 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. Do you have any simple solutions i wonder.

Look into Comma-separated Values (CSV) as a simple format. All versions of Excel support CSV files, and you can easily get the output you want using a basic Java FileWriter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM