
<!--Google 468*60橫幅廣告開始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "image"; //2007-07-26: CSDN google_ad_channel = "6063905817"; google_color_border = "6699CC"; google_color_bg = "E6E6E6"; google_color_link = "FFFFFF"; google_color_text = "333333"; google_color_url = "AECCEB"; google_ui_features = "rc:6"; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--Google 468*60橫幅廣告結束-->
今天給別人做東西,結果對方提出了這個要求:能不能把excel里的數據直接導入數據庫?
回來在網上一查,還真有一個這樣的java包。叫jxl。也是sourceForge上的一個項目。大家可以搜一下,這里提供下載地址(工程名叫jexcelapi)。 http://www.jspcn.net/htmldown/11248971876251883.html
下載來之后就可以用這提供的類來excel文件了。
先建一個excel文件abc.xls.放到E盤根目錄下。形如下:
name | secondName |
hot1 | leave1 |
hot2 | leave2 |
然后在數據庫里建表。
CREATE TABLE `name` (
`name` varchar(30) default NULL,
`secondname` varchar(20) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
/*
* Excel2Mysql.java
*
* Created on 2006年9月25日, 下午6:48
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package excel;
/**
*
* @author hotleave
*/
import java.io.*;
import jxl.*;
import java.sql.*;
public class Excel2Mysql {
public static Connection conn=null;
public static Statement stmt=null;
/** Creates a new instance of Excel2Mysql */
public Excel2Mysql() {
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=123456&characterEncoding=UTF-8");
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public static void main(String args[]){
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=user&password=pwd&characterEncoding=UTF-8");
stmt=conn.createStatement();
}catch(Exception e){
System.out.println(e.getMessage());
}
try{
InputStream is = new FileInputStream("e:/abc.xls");
Workbook book=Workbook.getWorkbook(is);//獲得一個工作表對象
Sheet sheet=book.getSheet(0);//取得第一個工作表,也可用sheet名字獲得。
int len=sheet.getRows();//取得行數
String sql="";
System.out.println("len is:"+len);
Cell[] cells=null;
System.out.println("讀出來的結果為:");
for(int i=1;i<len;i++){//從1開始,避免插入標題
//System.out.println(i);
cells=null;
cells=sheet.getRow(i);
sql="insert into name values ('"+cells[0].getContents()+"','" +
cells[1].getContents()+"')";
stmt.execute(sql);
System.out.println("ok");
//System.out.println("cells len is:"+cells.length);
for(int j=0;j<cells.length;j++){//打印每行信息
System.out.print(cells[j].getContents()+" ");
}
System.out.println("");
}
book.close();
}catch(Exception e){
System.out.println(e);
}
}
}
呵呵,大家自己試一下吧。可以創造出來更多的東西啊。結合一下smartUpoad也許能做一個在線處理的。
轉自:http://hi.baidu.com/myeking/blog/item/f306a638898ecec6d462256f.html 小李在此鳴謝
<!--新Google 468*60橫幅廣告開始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x60, 創建于 08-8-6 */ google_ad_slot = "7368701459"; google_ad_width = 468; google_ad_height = 60; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--新Google 468*60橫幅廣告結束-->
<!--新Google 468x15 橫鏈接單元開始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x15 橫鏈接單元 */ google_ad_slot = "5785741422"; google_ad_width = 468; google_ad_height = 15; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--新Google 468x15 橫鏈接單元結束-->
<!-- Google Reader shared發布代碼開始 --><script type="text/javascript" src="http://www.google.com/reader/ui/publisher.js"></script><script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/00697638153916680411/state/com.google/broadcast?n=5&callback=GRC_p(%7Bc%3A%22green%22%2Ct%3A%22%5Cu8FD9%5Cu4E9B%5Cu6587%5Cu7AE0%5Cu4E5F%5Cu503C%5Cu5F97%5Cu4E00%5Cu770B%22%2Cs%3A%22false%22%7D)%3Bnew%20GRC"></script><!-- Google Reader shared發布代碼結束 -->
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
