1¡¢
SQL Serverµ¼³öΪExcel£º
ÒªÓÃT-
SQLÓï¾äÖ±½Óµ¼³öÖÁExcel¹¤×÷±
¡£¬¾Í²»µÃ²»ÓýèÓÃSQL Server¹ÜÀíÆ÷µÄÒ»¸öÀ©Õ¹´æ´¢¹ý³Ì£ºxp_cmdshell
£¬´Ë¹ý³ÌµÄ×÷ÓÃΪ¡°ÒÔ²Ù×÷ϵͳÃüÁîÐнâÊÍÆ÷µÄ·½Ê½Ö´Ðиø¶¨µÄÃüÁî×Ö·û´®
£¬²¢ÒÔÎı¾Ðз½Ê½·µ »ØÈκÎÊä³ö
¡£¡±ÏÂÃæΪ¶¨ÒåʾÀý£º
2¡¢Excelµ¼ÈëSQL Server±í£º
ÔÚSQL ServerÖУ¬Óж¨ÒåÒ»¸öOpenDateSourceº¯Êý£¬ÓÃÓÚÒýÓÃÄÇЩ²»¾³£·ÃÎ浀 OLE DB Êý¾ÝÔ´£¬¶øÎÒÃǵÄÊý¾Ý»¥µ¼²Ù×÷£¬¾ÍÊǽ¨Á¢ÔÚÕâ¸öº¯ÊýÖ®ÉÏ¡£
Ê×ÏÈ¿´Ò»¸öT-SQL°ïÖúÖеÄʾÀý£¬ÃèÊöÈçÏ£º
--ÏÂÃæÊǸö²éѯµÄʾÀý£¬Ëüͨ¹ýÓÃÓÚ Jet µÄ OLE DB Ìṩ³ÌÐò²éѯ Excel µç×Ó±í¸ñ¡£
SELECT *
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:Financeaccount.xls";User ID=Admin;PassWord=;Extended PRoperties=Excel 5.0')xactions
×¢£º--ÔÚpassword£½£»µÄºóÃ棬¼Ó¸ö HDR=NO µÄÑ¡Ïî, ±íʾµÚ1ÐÐÊÇÊý¾Ý, ĬÈÏΪYES, ±íʾµÚ1ÐÐÊÇ×Ö¶ÎÃû
Èç¹ûÄãÖ±½ÓÒýÓÃÕâ¸öʾÀý½øÐвéѯ£¬ÄÇô¿Ï¶¨ÊÇͨ²»¹ýµÄ¡£¹Ø¼üÔÚÓÚÓï¾äÖеÄÁ½¸öµØ·½ÐèÒªÐ޸ģ¬Ò»´¦ÔÚÓÚData Source´¦£¬Ë«ÒýºÅÄÚΪExcel±í¸ñµÄʵ¼Ê´æ·ÅλÖã¬ÒªÐÞ¸ÄΪÄãÏë²éѯµÄExcel±íʵ¼ÊÍêÕû·¾¶£»¶þΪ×îºóµÄ...xactions£¬ÆäʵÕâÀï´ú ±íµÄÊÇÒª½øÐеÄijЩ¶¯×÷£¬ÏÂÃæ»á½²£¬ÕâÀïÐ޸ijÉÓÃÖÐÀ¨ºÅ°üΧµÄExcel±íÖй¤×÷±íÃû×Ö£¨¼ÓÉÏÒ»¸ö$£©¾Í¿ÉÒÔÁË£¬Èç[Sheet1$]¡£µ±È»£¬»¹¿ÉÒÔ½« Excel 5.0¸ÄΪExcel 8.0£¬ÒòΪ5.0ÊÇÒÔÇ°µÄÀÏ°æ±¾ÁË¡£
ÏÂÃæÊÇʵÀý˵Ã÷£º
/**//*1¡¢²åÈëExcelÖеÄ×ÊÁϵ½ÏÖ´æµÄsqlÊý¾Ý¿â±íÖУ¨¼ÙÉèCÅÌÓÐexcel±íbook2.xls£¬book2.xlsÖÐÓиö¹¤×÷±ísheet1£¬sheet1ÖÐÓÐÁ½ÁÐidºÍFName£»¶øͬʱsqlÊý¾Ý¿âÖÐÒ²ÓÐÒ»¸ö±ítest£©£º*/
insert into test SELECT id,FName
FROM OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data Source="c:ook2.xls";User ID=Admin;Password=;Extended properties=Excel 8.0')[sheet1$]
--Èç¹ûÓÃselect * £¬ÔòÁеĴÎÐò»áÂÒ£¬×ÊÁÏÄÚÈÝÒ²»áÂÒ£¬ÎÞ·¨²åÈë³É¹¦£¬ËùÒÔÖ¸¶¨ÁÐÃû
£££££££££££££££££££££££
/**//*2¡¢²åÈëexcel±íÖÐ×ÊÁϵ½sqlÊý¾Ý¿â²¢Ð½¨Ò»¸ösql±í£¨excelµÄ¶¨ÒåºÍÄÚÈÝͬÉÏ£©£º*/
select convert(int,id)as id,FName into test7
FROM OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data Source="c:ook2.xls";User ID=Admin;Password=;Extended properties=Excel 8.0')[sheet1$]
--ÔÚselect ÁÐÖÐ×îºÃÓÃconvert½øÐÐÏÔʾÀàÐÍת»»£¬·ñÔò×ÊÁÏÀàÐͻ᲻ÈçÔ¤ÆÚ¡£
Ìرð×¢Ò⣡£¡£¡£º1£©Èç¹ûÊÇ´ÓÊý¾Ý¿âÖе¼³öµÄexel±í£¬ÀýÈç´Ójobs±íµ¼³öµÄexelÎļþmytest.xls¹¤×÷±íĬÈÏÊÇjobsÉÏÃæÀý×ÓÖеÄ[sheet1$] Ó¦¸ÄΪ[jobs$]
2£©Èç¹û³öÏÖ¡°
·þÎñÆ÷: ÏûÏ¢ 7399£¬¼¶±ð 16£¬×´Ì¬ 1£¬ÐÐ 1
OLE DB Ìṩ³ÌÐò 'MICROSOFT.JET.OLEDB.4.0' ±¨´í¡£Ìṩ³ÌÐòδ¸ø³öÓйشíÎóµÄÈκÎÐÅÏ¢¡£¡±
ÉÏÃæÕâ¸ö´íÎóÊÇÒòΪÄãµÄEXECL Îļþ±»´ò¿ª×Å,¹ØµôÄǸöEXCELÎļþÔÙÊÔÊÔ.
3£©±»µ¼ÈëµÄexel±íµÚÒ»ÐÐÒªÓи÷ÁеÄÁÐÃûÈç
id name age
1 tomclus 35
¡£¡£¡£
Èç¹ûûÓÐÁÐÃû½ö½ö
1 tomclus 35
¡£¡£¡£
¿ÉÄÜ»á³ö´í
Èç¹ûÉÏÃæµÄÀý×ÓÖÐûÓÐÖƶ¨ËùÓÐÁУ¬»òselect*,¶¼»á³ö´í£¬ÈçÁв»ÍêÈ«£¬»òÊý¾ÝÀàÐͲ¼Æ¥
SQL ServerÓëExcelµÄÊý¾Ý»¥µ¼½²½âÍêÁË£¬ÄãÃ÷°×ÁËÂ𣿶øaccessºÍExcelµÄ»ù±¾Ò»Ñù£¬Ö»ÊÇҪȥµôExtended propertiesÉùÃ÷¡£
=======================
DelphiʾÀý£¨Œ§³öžéexcel±í£©£º
ADOQ1.Close;
ADOQ1.SQL.Clear;
sqltrs :=
'INSERT INTO CTable (Name1,Sex,ID)'+
' SELECT'+
' ÐÕÃû,ÐÔ±ð,Éí·ÝÖ¤ºÅ'+
' FROM [excel 8.0;database=' + XlsName + '].[sheet1$]';
ADOQ1.Parameters.Clear;
ADOQ1.ParamCheck:=false;
ADOQ1.SQL.Text := sqltrs;
ADOQ1.Execsql;
//ÖÐÎÄ×Ö¶ÎÁ½±ß²»ÄÜÓпոñ
Áí¸½£º£¨ÏÂÃæµÄ²¿·ÖÄÚÈÝûÓÐÇ××Ôʵ¼ù£©
ÊìϤSQL SERVER 2000µÄÊý¾Ý¿â¹ÜÀíÔ±¶¼ÖªµÀ£¬ÆäDTS¿ÉÒÔ½øÐÐÊý¾ÝµÄµ¼Èëµ¼³ö£¬Æäʵ£¬ÎÒÃÇÒ²¿ÉÒÔʹÓÃTransact-SQLÓï¾ä½øÐе¼Èëµ¼³ö²Ù×÷¡£ÔÚ Transact-SQLÓï¾äÖУ¬ÎÒÃÇÖ÷ҪʹÓÃOpenDataSourceº¯Êý¡¢OPENROWSET º¯Êý£¬¹ØÓÚº¯ÊýµÄÏêϸ˵Ã÷£¬Çë²Î¿¼SQLÁª»ú°ïÖú¡£ÀûÓÃÏÂÊö·½·¨£¬¿ÉÒÔÊ®·ÖÈÝÒ×µØʵÏÖSQL SERVER¡¢ACCESS¡¢EXCELÊý¾Ýת»»£¬Ïêϸ˵Ã÷ÈçÏ£º
Ò»¡¢SQL SERVER ºÍACCESSµÄÊý¾Ýµ¼Èëµ¼³ö
³£¹æµÄÊý¾Ýµ¼Èëµ¼³ö£º
ʹÓÃDTSÏòµ¼Ç¨ÒÆÄãµÄAccessÊý¾Ýµ½SQL Server£¬Äã¿ÉÒÔʹÓÃÕâЩ²½Öè:
¡ð1ÔÚSQL SERVER
ÆóÒµ¹ÜÀíÆ÷ÖеÄTools£¨¹¤¾ß£©²Ëµ¥ÉÏ£¬Ñ¡ÔñData Transformation
¡ð2Services£¨Êý¾Ýת»»·þÎñ£©£¬È»ºóÑ¡Ôñ czdImport Data£¨µ¼ÈëÊý¾Ý£©¡£
¡ð3ÔÚChoose a Data Source£¨Ñ¡ÔñÊý¾ÝÔ´£©¶Ô»°¿òÖÐÑ¡ÔñMicrosoft Access as the Source£¬È»ºó¼üÈëÄãµÄ.mdbÊý¾Ý¿â(.mdbÎļþÀ©Õ¹Ãû)µÄÎļþÃû»òͨ¹ýä¯ÀÀÑ°ÕÒ¸ÃÎļþ¡£
¡ð4ÔÚChoose a Destination£¨Ñ¡ÔñÄ¿±ê£©¶Ô»°¿òÖУ¬Ñ¡ÔñMicrosoft OLE¡¡DB Prov ider for SQL¡¡Server£¬Ñ¡ÔñÊý¾Ý¿â
·þÎñÆ÷£¬È»ºóµ¥»÷±ØÒªµÄÑéÖ¤·½Ê½¡£
¡ð5ÔÚSpecify Table Copy£¨Ö¸¶¨±í¸ñ¸´ÖÆ£©»òQuery£¨²éѯ£©¶Ô»°¿òÖУ¬µ¥»÷Copy tables£¨¸´ÖƱí¸ñ£©¡£
¡ð6ÔÚSelect Source Tables£¨Ñ¡ÔñÔ´±í¸ñ£©¶Ô»°¿òÖУ¬µ¥»÷Select All£¨È«²¿Ñ¡¶¨£©¡£ÏÂÒ»²½£¬Íê³É¡£
Transact-SQLÓï¾ä½øÐе¼Èëµ¼³ö£º
1.ÔÚSQL SERVERÀï²éѯaccessÊý¾Ý:
SELECT *
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:DB.mdb";User ID=Admin;Password=')...±íÃû
2.½«accessµ¼ÈëSQL server
ÔÚSQL SERVER ÀïÔËÐÐ:
SELECT *
INTO newtable
FROM OPENDATASOURCE ('Microsoft.Jet.OLEDB.4.0',
'Data Source="c:DB.mdb";User ID=Admin;Password=' )...±íÃû
3.½«SQL SERVER±íÀïµÄÊý¾Ý²åÈëµ½Access±íÖÐ
ÔÚSQL SERVER ÀïÔËÐУº
insert into OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source=" c:DB.mdb";User ID=Admin;Password=')...±íÃû
(ÁÐÃû1,ÁÐÃû2)
select ÁÐÃû1,ÁÐÃû2 from sql±í
ʵÀý£º
insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'C:db.mdb';'admin';'', Test)
select id,name from Test
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'c: rade.mdb'; 'admin'; '', ±íÃû)
SELECT *
FROM sqltablename
¶þ¡¢SQL SERVER ºÍEXCELµÄÊý¾Ýµ¼Èëµ¼³ö
1¡¢ÔÚSQL SERVERÀï²éѯExcelÊý¾Ý:
SELECT *
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:ook1.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$]
ÏÂÃæÊǸö²éѯµÄʾÀý£¬Ëüͨ¹ýÓÃÓÚ Jet µÄ OLE DB Ìṩ³ÌÐò²éѯ Excel µç×Ó±í¸ñ¡£
SELECT *
FROM OpenDataSource ( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:Financeaccount.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions
2¡¢½«ExcelµÄÊý¾Ýµ¼ÈëSQL server :
SELECT * into newtable
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:ook1.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$]
ʵÀý:
SELECT * into newtable
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:Financeaccount.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions
3¡¢½«SQL SERVERÖвéѯµ½µÄÊý¾Ýµ¼³ÉÒ»¸öExcelÎļþ
T-SQL´úÂ룺
EXEC master..xp_cmdshell 'bcp ¿âÃû.dbo.±íÃûout c:Temp.xls -c -q -S"servername" -U"sa" -P""'
²ÎÊý£ºS ÊÇSQL·þÎñÆ÷Ãû£»UÊÇÓû§£»PÊÇÃÜÂë
˵Ã÷£º»¹¿ÉÒÔµ¼³öÎı¾ÎļþµÈ¶àÖÖ¸ñʽ
ʵÀý:EXEC master..xp_cmdshell 'bcp saletesttmp.dbo.CusAccount out c: emp1.xls -c -q -S"pmserver" -U"sa" -P"sa"'
EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout C: authors.xls -c -Sservername -Usa -Ppassword'
ÔÚVB6ÖÐÓ¦ÓÃADOµ¼³öEXCELÎļþ´úÂ룺
Dim cn As New ADODB.Connection
cn.open "Driver={SQL Server};Server=WEBSVR;DataBase=WebMis;UID=sa;WD=123;"
cn.execute "master..xp_cmdshell 'bcp "SELECT col1, col2 FROM ¿âÃû.dbo.±íÃû" queryout E:DT.xls -c -Sservername -Usa -Ppassword'"
4¡¢ÔÚSQL SERVERÀïÍùExcel²åÈëÊý¾Ý:
insert into OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:Temp.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...table1 (A1,A2,A3) values (1,2,3)
T-SQL´úÂ룺
INSERT INTO
OPENDATASOURCE('Microsoft.JET.OLEDB.4.0',
'Extended Properties=Excel 8.0;Data source=C: raininginventur.xls')...[Filiale1$]
(bestand, produkt) VALUES (20, 'Test')
×ܽ᣺ÀûÓÃÒÔÉÏÓï¾ä£¬ÎÒÃÇ¿ÉÒÔ·½±ãµØ½«SQL SERVER¡¢ACCESSºÍEXCELµç×Ó±í¸ñÈí¼þÖеÄÊý¾Ý½øÐÐת»»£¬ÎªÎÒÃÇÌṩÁ˼«´ó·½±ã£¡
EXEC master..xp_cmdshell 'bcp ¿âÃû.dbo.±íÃûout c:Book3.xls -c -q -S"servername" -U"sa" -P""'
--²ÎÊý£ºS ÊÇSQL·þÎñÆ÷Ãû£»UÊÇÓû§Ãû£»PÊÇÃÜÂë,ûÓоͿÕ×Å
--˵Ã÷£ºÆäʵÓÃÕâ¸ö¹ý³Ìµ¼³öµÄ¸ñʽʵÖÊÉϾÍÊÇÎı¾¸ñʽµÄ£¬²»ÐŵĻ°ÔÚµ¼³öµÄExcel±íÖиĶ¯Ò»ÏÂÔÙ±£´æ¿´¿´¡£
ʵ¼ÊÀý×ÓÓë˵Ã÷ÈçÏ£º
/**//*Èç¹ûÒª½«±íÕû¸öµ¼³öÖÁExcelµÄ»°*/
EXEC master..xp_cmdshell 'bcp northwind.dbo.orders out c:Book1.xls -c -q -S"(local)" -U"sa" -P""'
--×¢Òâ¾äÖеÄnorthwind.dbo.orders£¬ÎªÊý¾Ý¿âÃû£«ÓµÓÐÕߣ«±íÃû
--Ö±½Óµ¼³öÓá°out¡±¹Ø½¡×Ö
-------------------------------------------
/**//*Èç¹ûÒªÀûÓòéѯÀ´µ¼³ö²¿·Ö×Ö¶ÎÖÁExcelµÄ»°*/
EXEC master..xp_cmdshell 'bcp "SELECT orderid,cutomerid,freight FROM northwind..orders ORDER BY orderid" queryout C: Book2.xls -c -S"(local)" -U"sa" -P""'
--ÕâÀïÔÚbcpºóÃæ¼ÓÁËÒ»¸ö²éѯÓï¾ä£¬²¢ÓÃË«ÒýºÅÀ¨ÆðÀ´
--ÀûÓòéѯҪÓá°queryout¡±¹Ø¼ü×Ö
¹ØÓÚSQL ServerÓëExcel¡¢AccessÊý¾Ý»¥µ¼ÎÊÌâµÄ²¹³ä£º
1¡¢½«excelÖеÄÊý¾Ýµ¼ÈësqlÖÐʱ£¬Êý×Ö±äΪ¿Æѧ¼ÆÊý·¨µÄ½â¾ö°ì·¨£º
È磺
excelÖеÄÊý¾ÝΪ£º8630890
µ¼Èësqlºó±äΪ£º8.63089e+006
×¢£ºsqlÖиÃ×Ö¶ÎÊý¾ÝÀàÐÍΪnvchar¡£
¿ÉÒԲο¼ÏÂÃæµÄ·½·¨×ª»»ÒѾµ¼ÈëµÄÊý¾Ý,µ«Òò¾«¶ÈÎÊÌâµ¼ÖµÄÊý¾Ý²»×¼È·²»Äܱ»´¦Àí,ÁíÍâ,excelÊý¾ÝÖÐ,Èç¹ûÓÐÇ°µ¼µÄ0,ÄÇôµ¼ÈëºóµÄÊý¾ÝÓÉÓÚÊÇfloatÊý×Ö,ËùÒԻᶪʧǰµ¼0
declare @a float
set @a=8.63089e+006
select cast(@a as decimal(38))
--½á¹û£º8630890
½áºÏ×Ô¼ºµÄʵÀý£¬¸ø´ó¼ÒÒ»¶Î´úÂ룺
file1=request("file")
sql="insert into student(studyid,yourname,yourpass,yourclass,courseid) SELECT cast(ѧºÅ as decimal(18)),ÐÕÃû,cast(ÃÜÂë as decimal(18)),°à¼¶,cast(Ñ¡¿Î°àºÅ as decimal(18)) FROM OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data Source="&file1&";User ID=Admin;Password=;Extended properties=Excel 8.0')...[sheet1$]"
conn.Execute sql
2¡¢ÈçºÎµÃµ½EXCELµÄ±íÃû£¨
aspÖУ©£º
set app=server.CreateObject("Excel.application")
app.Workbooks.Open(""&file1&"")
for i =1 to app.worksheets.count
response.write app.worksheets(i).name
next
²âÊÔµÄʱºò£¬²»ÖªÊ²Ã´ÔÒò£¬Ê±ºÃʱ²»ºÃµÄ£¬Óдý½øÒ»²½½â¾ö£¡