1
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 文件中有上述信息,我想仅选择并输出AllTXN_Issued2013 年 7 月的值作为上个月交易的总额。使用 jdbc-odbc 连接连接 Excel 电子表格后,如何在 Java 中执行此操作?

4

3 回答 3

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();

http://www.codoid.com/products/view/2/29

于 2013-10-31T10:20:41.853 回答
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();
        }

    }

}
于 2013-09-05T19:02:26.017 回答
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();
于 2018-05-14T12:11:06.777 回答