У меня есть вопрос о результатах в Excel файл в Java. Позвольте мне объяснить сценарий;Несколько результатов для Excel файла в Java
У меня есть много запросов 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(); }
}
}
Этот ResultSet возвращает; 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(); }
}
}
Я хочу, чтобы написать эти результирующие в файл Excel, как показано ниже скриншота;
Я пытаюсь использовать Apache POI Project для вывода первенствует файл, но я не понимаю многих вещей. У вас есть какие-то простые решения.
Hi jordan, Если у меня есть только один лист, я могу использовать CSV-решение. Но я хочу подготовить этот файл excel двумя или тремя листами. Можем ли мы определить отдельные листы в CSV, которых я не знаю. – Lacrymae
Нет, вы не можете определить несколько листов. Но спросите себя, почему вам нужно несколько листов ... на практике они не добавляют большого значения, если вы не планируете позже связывать листы. –