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

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("");
--------------------------------------------------------
方法二:
http://www.ibm.com/developerworks/cn/java/l-javaExcel/index.html
--------------------------------------------------------------------
方法三

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值