java中的导出_JAVA中读取和导出数据到EXCEL的方法

本文介绍了一种通过JDBC-ODBC桥动态读取Excel文件的方法,使得能够像操作数据库一样访问Excel表格。首先设置ODBC数据源,然后使用Java的JDBC驱动连接并执行SQL查询。示例代码展示了如何读取Excel的Sheet1$表中的数据,同时也提到了可以使用标准SQL查询。此外,还讨论了第一行未被打印的原因是由于 JDBC 桥将第一行视为列名。

1.   jdbc:odbc:microsoft   excel   driver

2.   3rd   part   package:   jexcel

3.   jacabo

方法一:

This sample code was an outcome of a friend's requirement for reading an excel file dynamically. He had an EJB layer for a flight management system for read/writes to database. It so happened that the data also started coming from various departments in a spread sheet format. So either he had to import the data to his oracle database manually or re-design his EJB's for accomadating this new data input. So what i designed for him was a simple facade pattern classes run by a daemon process which makes use of the existing enterprise java beans which enabled him to treat the spreadsheet data as no different. The scope of the design is beyond this article.

So what i will illustrate in this article is a simple way of accessing spreadsheets as if they were a database. This article holds good for java running on windows-servers. The access itself is through a jdbc-odbc bridge.

okie..so here we go...

Open the odbc data administrator console and click on system dsn. select add and add the Microsoft Excel driver from the list of drivers and give a name to the dsn(say exceltest) and select the workbook.

Then all one needs is to connect through this dsn just like connecting to the database and accessing records.

Here's the sample code

import java.io.*;

import java.sql.*;

public class ExcelReadTest{

public static void main(String[] args){

Connection connection = null;

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection( "jdbc:odbc:exceltest" );

Statement st = con.createStatement();

ResultSet rs = st.executeQuery( "Select * from [Sheet1$]" );

ResultSetMetaData rsmd = rs.getMetaData();

int numberOfColumns = rsmd.getColumnCount();

while (rs.next()) {

for (int i = 1; i <= numberOfColumns; i++) {

if (i > 1) System.out.print(", ");

String columnValue = rs.getString(i);

System.out.print(columnValue);

}

System.out.println("");

}

st.close();

con.close();

} catch(Exception ex) {

System.err.print("Exception: ");

System.err.println(ex.getMessage());

}

}

}

Answer to Comments:

Hey john ur comment is right and well recieved. The reason for the first row not being printed is that the jdbc-bridge assumes the first row to be akin to column names in the database. Hence the first available row is the name of the column...which explains why the third row is printed if the first row is missing.

Hope this helps and thanks for the comment

Standard sql queries like

Select column_name1,column_name2 from [Sheet1$] where column_name3 like '%bob%';

can be used on the spreadsheet.

You can use the following snippet to print the column names(which is the first row of the spread sheet).

for (int i = 1; i <= numberOfColumns; i++) {

if (i > 1) System.out.print(", ");

String columnName = rsmd.getColumnName(i);

System.out.print(columnName);

}

System.out.println("");

--------------------------------------------------------

方法二:

--------------------------------------------------------------------

方法三

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值