{"id":2241,"date":"2012-07-10T10:10:55","date_gmt":"2012-07-10T10:10:55","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=2241"},"modified":"2012-07-12T07:43:47","modified_gmt":"2012-07-12T07:43:47","slug":"%e5%af%bc%e5%85%a5%e5%a4%9a%e4%b8%aaexcel%e5%85%a5%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2012\/07\/10\/%e5%af%bc%e5%85%a5%e5%a4%9a%e4%b8%aaexcel%e5%85%a5%e6%95%b0%e6%8d%ae%e5%ba%93\/","title":{"rendered":"\u5bfc\u5165\u591a\u4e2aexcel\u5165\u6570\u636e\u5e93"},"content":{"rendered":"<pre class=\"brush:sql;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;\">\r\n\r\n\/*\u5bfc\u5165\u591a\u4e2aexcel\u5165\u6570\u636e\u5e93\r\n 1\uff0c\u7528xp_cmdshell\u83b7\u53d6\u6240\u6709excel\u7684\u540d\u79f0\r\n 2, \u56e0sheet\u9700\u8981\u6307\u5b9a\uff0c\u8981\u83b7\u53d6\u5176\u6bcf\u4e2a\u6587\u4ef6\u8981\u5bfc\u5165\u7684\u7b2c\u4e00\u4e2asheet\u540d\u79f0\r\n 3\uff0c\u5229\u7528OpenDataSource\u4e0a\u4f20excel\u6570\u636e\r\n*\/\r\n\r\n--excel\u6240\u5728\u8def\u5f84\r\nDECLARE @filepath NVARCHAR(1000)\r\nSET @filepath = &#39;D:\\nocode\\liaoxiaoman\\DDS\\&#39;\r\n\r\n--\u83b7\u53d6\u6240\u6709excel\u540d\u79f0\r\nDROP TABLE duifang_file_dds\r\nCREATE TABLE duifang_file_dds([filename] NVARCHAR(1000))\r\n\r\nDECLARE @cmd VARCHAR(1000)\r\nSET @cmd = N&#39;dir &quot;&#39; + @filepath + &#39;\\*&#39; + &#39;*.xls&quot; \/B&#39;\r\nINSERT INTO duifang_file_dds\r\nEXEC master..xp_cmdshell @cmd\r\n\r\n--\u6e05\u9664\u65e0\u6548\u6587\u4ef6\u540d\u79f0\r\nDELETE a\r\nFROM duifang_file_dds a\r\nWHERE filename IS NULL\r\n\r\n--\u52a0\u5b57\u6bb5sheetname,ID,\u4ee5\u4fbf\u505a\u8c03\u8bd5\r\nALTER TABLE duifang_file_dds ADD sheetname NVARCHAR(50),ID INT IDENTITY(1,1)\r\n\r\n--\u53d6\u51fasheet\u540d\u79f0 .\u6b64\u5904\u6700\u597d\u7528\u5de5\u5177\u5c06\u6240\u6709excel\u7684sheetname\u8bbe\u7f6e\u4e3a\u7edf\u4e00\r\nUPDATE a\r\n--SET a.sheetname = replace(replace(replace(replace(dbo.SUBSTRING2(FILENAME,&#39;\u7eff\u7626\u5546\u8d38&#39;,&#39;\u7b2c&#39;,3),&#39;\u7b2c&#39;,&#39;&#39;),&#39;\u660e\u7ec6&#39;,&#39;&#39;),&#39;(&#39;,&#39;&#39;),&#39;\uff08&#39;,&#39;&#39;)\r\nSET a.sheetname = replace(replace(dbo.SUBSTRING2(FILENAME,&#39;&#39;,&#39;\u7b2c&#39;,3),&#39;(&#39;,&#39;&#39;),&#39;\uff08&#39;,&#39;&#39;)\r\nFROM duifang_file_dds a\r\n\r\n--\u683c\u5f0f\u5316\r\n--\u5bfc\u5165\u65f6\u4e0d\u8ba4\u70b9,\u6545\u8981\u5c06\u70b9\u66ff\u4ee3\u4e3a#\r\nUPDATE a\r\nSET a.sheetname = replace(replace(sheetname,&#39;.&#39;,&#39;#&#39;),&#39; &#39;,&#39;&#39;) + &#39;$&#39; --sheet\u4ee5$\u7ed3\u5c3e\r\nFROM duifang_file_dds a\r\n\r\n--\u9996\u5b57\u6bcd\u4e3a\u6570\u5b57\uff0c\u8981\u524d\u540e\u52a0\u5f15\u53f7\r\nUPDATE a\r\nSET a.sheetname = CASE(ISNUMERIC(SUBSTRING(sheetname,1,1))) WHEN 1 THEN QUOTENAME(a.sheetname,&#39;&#39;&#39;&#39;) ELSE a.sheetname END \r\nFROM duifang_file_dds a\r\n\r\n\r\nSELECT *\r\nFROM duifang_file_dds a\r\n\r\n\r\n--\u5efa\u8868\r\nDROP TABLE duifang_bill_dds\r\nSELECT   \u4ee3\u6536\u6b3e,F2,F3,F4,F5, REPLICATE(&#39;&#39;,100) FileName INTO   duifang_bill_dds \r\nFROM   OpenDataSource( &#39;Microsoft.Jet.OLEDB.4.0&#39;, \r\n&#39;Data Source= &quot;D:\\nocode\\liaoxiaoman\\DDS\\5.21\u7b2c\u4e09\u6279.xls&quot;;\r\nExtended Properties= &quot;Excel 5.0;HDR=no;IMEX=1;Excel 8.0&quot;;Persist Security Info=False &#39;)...[&#39;5#21$&#39;]\r\nWHERE 1=2\r\n\r\n\r\n\r\nTRUNCATE TABLE duifang_bill_dds\r\n\r\nDECLARE pcur CURSOR FOR\r\nSELECT  filename,sheetname,ID\r\nFROM duifang_file_dds\r\nWHERE ID &gt;=1\r\nORDER BY ID\r\n\r\nDECLARE @filename NVARCHAR(1000), @sheetname NVARCHAR(50),@s NVARCHAR(1000),@ID INT\r\n\r\nOPEN pcur\r\nFETCH NEXT FROM pcur INTO @filename,@sheetname,@ID\r\nWHILE @@FETCH_STATUS = 0\r\nBEGIN \r\n\t--\u4ee3\u6536\u6b3e\u8f6c\u6210VARCHAR(50),\u9632\u6b62\u5305\u88f9\u53f7\u51fa\u73b0e+\r\n\tSET @S = &#39;INSERT duifang_bill_dds  \r\n\tSELECT CONVERT(VARCHAR(50),convert(bigint,\u4ee3\u6536\u6b3e)),F2,F3,F4,F5, &#39;&#39;&#39; + @filename + &#39;&#39;&#39; FROM OpenDataSource(&#39;&#39;Microsoft.Jet.OLEDB.4.0&#39;&#39;, \r\n\t&#39;&#39;Data Source= &quot;&#39; + @filepath + @filename + &#39;&quot;;\r\n\tExtended Properties= &quot;Excel 5.0;HDR=no;IMEX=1;Excel 8.0&quot;;Persist Security Info=False &#39;&#39;)...[&#39; + @sheetname + &#39;]\r\n\tWHERE ISNUMERIC(\u4ee3\u6536\u6b3e) = 1&#39;\r\n\tPRINT &#39;ID=&#39; + CONVERT(VARCHAR(50),@ID) + CHAR(13) +@S \r\n\tEXEC (@s)\r\n\tFETCH NEXT FROM pcur INTO @filename,@sheetname,@ID\t\r\nEND \r\nCLOSE pcur\r\nDEALLOCATE pcur\r\n\r\n--\u54ea\u4e2a\u6587\u4ef6\u6ca1\u6709\u5bfc\u5165\r\nSELECT *\r\nFROM duifang_file_dds a\r\nWHERE  NOT EXISTS( SELECT 1 FROM duifang_bill_dds b WHERE a.FILENAME = b.filename )\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/*\u5bfc\u5165\u591a\u4e2aexcel\u5165\u6570\u636e\u5e93 1\uff0c\u7528xp_cmdshell\u83b7\u53d6\u6240\u6709excel\u7684\u540d\u79f0 2, \u56e0sheet\u9700\u8981\u6307 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-2241","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/2241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/comments?post=2241"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/2241\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=2241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=2241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=2241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}