Open_YYYY Open_MM MetricMonth BaseTXN_Issued AllTXN_Issued
2013 4 Apr-2013 24990 42179
2013 5 May-2013 37049 58037
2013 6 Jun-2013 33491 54869
2013 7 Jul-2013 34359 62047
我在Excel文件中有上述信息,我想选择并输出2013年7月AllTXN_Issued
的值作为上个月交易的总和。在使用jdbc-odbc连接连接Excel电子表格后,如何在Java中执行此操作?
答案 0 :(得分:3)
试试这个,它适用于所有平台。
Fillo fillo=new Fillo();
Connection connection=fillo.getConnection("C:\\Test.xlsx");
String strQuery="Select * from Sheet1 where ID=100";
Recordset recordset=connection.executeQuery(strQuery);
while(recordset.next()){
System.out.println(recordset.getField("Details"));
}
recordset.close();
connection.close();
答案 1 :(得分:1)
以下代码适用于我。它假设Excel中的[MetricMonth]列包含文本值(而不是格式化为MMM-yyyy的真实日期),但它至少应该为您提供一般的想法。
import java.sql.*;
public class jdbcTest {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(
"jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" +
"Dbq=C:\\__tmp\\Book1.xlsx;");
PreparedStatement s = conn.prepareStatement(
"SELECT * FROM [Sheet1$] WHERE [MetricMonth] = ?");
s.setString(1, "Jul-2013");
s.execute();
ResultSet rs = s.getResultSet();
if (rs!=null) {
while (rs.next()) {
System.out.println(rs.getInt("AllTXN_Issued"));
}
}
s.close();
conn.close();
} catch( Exception e ) {
e.printStackTrace();
}
}
}
答案 2 :(得分:0)
Fillo fillo=new Fillo();
Connection connection=fillo.getConnection("C:\\Test.xlsx");//Path to your excel workbook
String strQuery="Select * from SheetName where Open_YYYY = 2013 and MetricMonth like '%Jul%'";
Recordset recordset=connection.executeQuery(strQuery);
while(recordset.next()){
System.out.println(recordset.getField("AllTXN_Issued"));
}
recordset.close();
connection.close();