當前位置:九游会j9娱乐平台-九游ag登录中心网址 » 操作系統 » excel導入資料庫c

excel導入資料庫c-九游会j9娱乐平台

發布時間: 2024-06-25 14:01:45

『壹』 把excel工作表中數據導入資料庫

這是第二次了,市場部那邊又來要求改數據。他們要改的是資料庫某張表中類似商品價格等的數據,需要改的地方又多,我們上次是靠新來的兄弟一個個給update進去的,這次老大去教了他們update語句,把煩人的皮球踢給他們了。但這樣一個個更新很明顯不是辦法,我想通過excel直接把數據導入資料庫應該是可行的吧,就開始找方法了。
我想至少有這樣兩種比較容易實現的方法:
1、直接用sql語句查詢
2、先用excle中的數據生成xml文件,再把xml導入資料庫
第一種方法(方法二以後再試),找到聯機叢書里實現此功能的sql語句如下:
select* fromopendatasource('microsoft.jet.oledb.4.0', 'datasource="c:financeaccount.xls";userid=admin;password=;extendedproperties=excel5.0')…xactions
語句是有了,但我還是試了很久,因為各個參數具體該怎麼設置它沒有說。data source就是excel文件的路徑,這個簡單;userid、password和extended properties這三個屬性我改成了各種各樣的與本機有關的用戶名、密碼以及excel版本都不對,最後用上面例子里的值“user id=admin;password=;extended properties=excel 5.0”才成功了,暈啊;最後個“xactions”更是查了很多資料,其實就僅僅是excel文件里所選的工作表名而已,怪我對excel不夠熟悉了,另外注意默認的sheet1要寫成[sheet1$]
最後,看看我成功的測試
資料庫里建好一個表testtable_1,有5個欄位id, name, date, money, content,c盤下book1.xls文件的sheet1工作表裡寫好對應的數據並設好數據類型,執行如下插入語句:
insertintotesttable_1([name],[date],[money],[content]) select[姓名],[日期],[金額],[內容] fromopendatasource('microsoft.jet.oledb.4.0', 'datasource="c:book1.xls"; userid=admin;password=;extendedproperties=excel5.0')…[sheet1$]
select里的列名我一開始用*代替,但發現輸出順序與我預期的不同,是“金額、內容、日期、姓名”,不知道具體有什麼規律,就老老實實寫名字了。操作成功
回過頭來看看市場部的要求,假設在我這張表裡實現,可以先判斷如excel里存在與記錄相同的name欄位(name要唯一非空)時就刪除記錄,之後再插入,這樣簡單,但自增的id欄位會因為插入而改變,那是不行的了。可行的方法是先讀出excel里全部記錄,然後用游標一條條分析,如果存在這個name就更新否則就插入。ok,下次就不用讓他們再對著文檔一條條update了

『貳』 如何將excel的數據導入到mysql資料庫中

第一步:建立資料庫和數據表(按照自己的excel數據設立欄位)。
[sql] view plain print?
create database php_excel;
use php_excel;
create table if not exists php_excel(
id int(20) not null auto_increment primary key,
gid varchar(20) not null,
stu_no varchar(20) not null,
name varchar(45) not null,
age int(4) not null
)engine=myisam default charset=utf8;

第二步:前台index.php文件。
[html] view plain print?




phpexcel導入excel數據到mysql資料庫










第三步:向資料庫插入數據的insertdb.php文件。
[php] view plain print?
session_start();
header("content-type:text/html;charset:utf-8");
//全局變數

$succ_result=0;
$error_result=0;
$file=$_files['filename'];
$max_size="2000000"; //最大文件限制(單位:byte)
$fname=$file['name'];
$ftype=strtolower(substr(strrchr($fname,'.'),1));
//文件格式
$uploadfile=$file['tmp_name'];
if($_server['request_method']=='post'){
if(is_uploaded_file($uploadfile)){
if($file['size']>$max_size){
echo "import file is too large";
exit;
}
if($ftype!='xls'){
echo "import file type is error";
exit;
}
}else{
echo "the file is not empty!";
exit;
}
}
require("./conn.php"); //連接mysql資料庫

//調用phpexcel類庫
require_once 'phpexcel.php';
require_once 'phpexcel\iofactory.php';
require_once 'phpexcel\reader\excel5.php';

$objreader = phpexcel_iofactory::createreader('excel5');//use excel2007 for 2007 format
$objphpexcel = $objreader->load($uploadfile);
$sheet = $objphpexcel->getsheet(0);
$highestrow = $sheet->gethighestrow(); // 取得總行數
$highestcolumn = $sheet->gethighestcolumn(); // 取得總列數
$arr_result=array();
$strs=array();

for($j=2;$j<=$highestrow;$j )
{
unset($arr_result);
unset($strs);
for($k='a';$k<= $highestcolumn;$k )
{
//讀取單元格
$arr_result .= $objphpexcel->getactivesheet()->getcell("$k$j")->getvalue().',';
}
$strs=explode(",",$arr_result);
$sql="insert into php_excel(gid,stu_no,name,age) values ($strs[0],'$strs[1]','$strs[2]',$strs[3])";
echo $sql."
";
mysql_query("set names utf8");
$result=mysql_query($sql) or die("執行錯誤");

$insert_num=mysql_affected_rows();
if($insert_num>0){
$succ_result =1;
}else{
$error_result =1;
}

}
echo "插入成功".$succ_result."條數據!!!
";
echo "插入失敗".$error_result."條數據!!!";

其中conn.php代碼如下:
[php] view plain print?
$mysql=mysql_connect("localhost","root","") or die("資料庫連接失敗!");
mysql_select_db("php_excel",$mysql);
mysql_query("set names utf8");

我的導入效果如下:
至此,從excel文件讀取數據批量導入到mysql資料庫完成。

『叄』 c#中如何將excel中的數據批量導入到sql server

1.本文實現在c#中可高效的將excel數據導入到sqlserver資料庫中,很多人通過循環來拼接sql,這樣做不但容易出錯而且效率低下,最好的辦法是使用bcp,也就是system.data.sqlclient.sqlbulkcopy 類來實現。不但速度快,而且代碼簡單,下面測試代碼導入一個6萬多條數據的sheet,包括讀取(全部讀取比較慢)在我的開發環境中只需要10秒左右,而真正的導入過程只需要4.5秒。x0dx0a2.代碼如下:x0dx0ausing system; x0dx0ausing system.data; x0dx0ausing system.windows.forms; x0dx0ausing system.data.oledb; x0dx0anamespace windowsapplication2 x0dx0a{ x0dx0a public partial class form1 : form x0dx0a { x0dx0a public form1() x0dx0a { x0dx0a initializecomponent(); x0dx0a } x0dx0ax0dx0a private void button1_click(object sender, eventargs e) x0dx0a { x0dx0a //測試,將excel中的sheet1導入到sqlserver中 x0dx0a string connstring = "server=localhost;uid=sa;pwd=sqlgis;database=master"; x0dx0a system.windows.forms.openfiledialog fd = new openfiledialog(); x0dx0a if (fd.showdialog() == dialogresult.ok) x0dx0a { x0dx0a transferdata(fd.filename, "sheet1", connstring); x0dx0a } x0dx0a } x0dx0ax0dx0a public void transferdata(string excelfile, string sheetname, string connectionstring) x0dx0a { x0dx0a dataset ds = new dataset(); x0dx0a tryx0dx0a { x0dx0a //獲取全部數據 x0dx0a string strconn = "provider=microsoft.jet.oledb.4.0;" "data source=" excelfile ";" "extended properties=excel 8.0;"; x0dx0a oledbconnection conn = new oledbconnection(strconn); x0dx0a conn.open(); x0dx0a string strexcel = ""; x0dx0a oledbdataadapter mycommand = null; x0dx0a strexcel = string.format("select * from [{0}$]", sheetname); x0dx0a mycommand = new oledbdataadapter(strexcel, strconn); x0dx0a mycommand.fill(ds, sheetname); x0dx0ax0dx0a //如果目標表不存在則創建 x0dx0a string strsql = string.format("if object_id('{0}') is null create table {0}(", sheetname); x0dx0a foreach (system.data.datacolumn c in ds.tables[0].columns) x0dx0a { x0dx0a strsql = string.format("[{0}] varchar(255),", c.columnname); x0dx0a } x0dx0a strsql = strsql.trim(',') ")"; x0dx0ax0dx0a using (system.data.sqlclient.sqlconnection sqlconn = new system.data.sqlclient.sqlconnection(connectionstring)) x0dx0a { x0dx0a sqlconn.open(); x0dx0a system.data.sqlclient.sqlcommand command = sqlconn.createcommand(); x0dx0a command.commandtext = strsql; x0dx0a command.executenonquery(); x0dx0a sqlconn.close(); x0dx0a } x0dx0a //用bcp導入數據 x0dx0a using (system.data.sqlclient.sqlbulkcopy bcp = new system.data.sqlclient.sqlbulkcopy(connectionstring)) x0dx0a { x0dx0a bcp.sqlrowscopied = new system.data.sqlclient.sqlrowscopiedeventhandler(bcp_sqlrowscopied); x0dx0a bcp.batchsize = 100;//每次傳輸的行數 x0dx0a bcp.notifyafter = 100;//進度提示的行數 x0dx0a bcp.destinationtablename = sheetname;//目標表 x0dx0a bcp.writetoserver(ds.tables[0]); x0dx0a } x0dx0a } x0dx0a catch (exception ex) x0dx0a { x0dx0a system.windows.forms.messagebox.show(ex.message); x0dx0a }x0dx0a } x0dx0ax0dx0a //進度顯示 x0dx0a void bcp_sqlrowscopied(object sender, system.data.sqlclient.sqlrowscopiedeventargs e) x0dx0a { x0dx0a this.text = e.rowscopied.tostring(); x0dx0a this.update(); x0dx0a }x0dx0a } x0dx0a} x0dx0a3.上面的transferdata基本可以直接使用,如果要考慮周全的話,可以用oledb來獲取excel的表結構,並且加入columnmappings來設置對照欄位,這樣效果就完全可以做到和sqlserver的dts相同的效果了。

『肆』 求一個c# excel導入資料庫的源碼

環境:c#.2005 access sql
一、把datatable插入資料庫
public static void datatabletodb()
{
string _strexcelfilename = @"d:\example.xls";
datatable dtexcel = exceltodatatable(_strexcelfilename,"sheet1");
for (int i = 0; i < dtexcel.rows.count; i )
{
insertdatatoaccess(dtexcel.rows[i][0].tostring(), float.parse(dtexcel.rows[i][1].tostring()));
}
}

二、把excel數據讀入datatable
public static datatable exceltodatatable(string strexcelfilename, string strsheetname)
{
string strconn = "provider=microsoft.jet.oledb.4.0;" "data source=" strexcelfilename ";" "extended properties=excel 5.0;";
string strexcel = string.format("select * from [{0}$]", strsheetname);
dataset ds = new dataset();

using (oledbconnection conn = new oledbconnection(strconn))
{
conn.open();
oledbdataadapter adapter = new oledbdataadapter(strexcel, strconn);
adapter.fill(ds, strsheetname);
conn.close();
}

return ds.tables[strsheetname];
}

三、向access資料庫表插入數據
public static void insertdatatoaccess(string _strpara,float _fpara)
{
oledbconnection oledbconn = new oledbconnection();
oledbconn.connectionstring = @"provider=microsoft.jet.oledb.4.0;data source=c:\exceldata.mdb;user id=admin;password=;";
oledbconn.open();

string strinsertstring = "insert into tb_exceldata (strcollumn1,fcollumn2) values (@strcollumn1,@fcollumn2)";
oledbcommand ocomm = new oledbcommand(strinsertstring, oledbconn);
ocomm.parameters.add("@strcollumn1", oledbtype.char , 50);
ocomm.parameters["@strcollumn1"].value = _strpara;
ocomm.parameters.add("@fcollumn2", oledbtype.double);
ocomm.parameters["@fcollumn2"].value = _fpara;

ocomm.executenonquery();
oledbconn.close();
}

『伍』 如何將excel表格的數據導入到mysql數據中去

工具:office excel、mysql

步驟:

1、打開mysql,用mysql命令創建一個表。

『陸』 怎麼把excel導入到oracle資料庫中

工具:plsqldeveloper
步驟:
①:登錄需要導入的資料庫
②tool-->odbc
import...
③在下圖中填入以下三項:第一個選excels
files,下面的用戶名密碼,就要看你要導入到哪個用戶裡面了,這里我用scott用戶為例子,用戶名密碼為scott/tiger
④點擊下面的connect進行選擇excel文件然後點擊確定,然後會彈出下面的圖,選擇你數據所在的sheet即可
⑤然後進行配置
data
to
oracle
選項頁,需要填入以下信息,並將列一一對應上
⑥最後點擊下面的import即可
恭喜你,excel導入oracle成功了。

熱點內容
發布:2024-07-17 17:13:27 瀏覽:872
phpjava交互 發布:2024-07-17 16:58:57 瀏覽:356
resin下jsp不能正常編譯 發布:2024-07-17 16:34:44 瀏覽:229
sqlserver如何切換主備伺服器 發布:2024-07-17 16:23:02 瀏覽:299
mc18伺服器ip 發布:2024-07-17 16:23:02 瀏覽:379
仙境傳說手游腳本 發布:2024-07-17 16:09:24 瀏覽:691
matlab命令窗口和新建腳本 發布:2024-07-17 15:51:26 瀏覽:375
建ftp文件夾 發布:2024-07-17 15:51:26 瀏覽:955
魔獸撿物腳本 發布:2024-07-17 15:27:56 瀏覽:130
開發ip伺服器 發布:2024-07-17 15:24:42 瀏覽:388
网站地图