{"id":6041,"date":"2017-09-21T08:35:57","date_gmt":"2017-09-20T23:35:57","guid":{"rendered":"http:\/\/excelwork.info\/excel\/?p=6041"},"modified":"2017-09-21T08:35:57","modified_gmt":"2017-09-20T23:35:57","slug":"excelado","status":"publish","type":"post","link":"https:\/\/excelwork.info\/excel\/excelado\/","title":{"rendered":"Excel\u30d5\u30a1\u30a4\u30eb\u306b\u63a5\u7d9a\uff08ADO\uff09"},"content":{"rendered":"<h2>ADO\u3067Excel\u30d5\u30a1\u30a4\u30eb\u306b\u63a5\u7d9a<\/h2>\n<div class=\"m30-l\">\n<p>ADO \u3092\u4f7f\u7528\u3057\u3066Excel\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002Excel\u30d5\u30a1\u30a4\u30eb\u3092\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u307f\u306a\u3057\u3001SQL\u6587\u3092\u4f7f\u3046\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<\/p>\n<br \/>\n<div class=\"kkbox\"><strong>\u3010\u63a5\u7d9a\u6587\u5b57\u5217\u3011<\/strong><\/p>\n<p>    <span class=\"red\"><strong>\uff1cConnectionString\uff08ADO\uff09\u306b\u8a2d\u5b9a\u3059\u308b\u6587\u5b57\u5217\uff1e<\/strong><\/span><br \/>\n    <strong>Provider<\/strong> = <em>OLEDBProvider<\/em><br \/>\n    <strong>Data Source<\/strong> = <em>DBFileName<\/em>;<br \/>\n    <strong>Extended Properties<\/strong> = <em>EXProperties<\/em>;<\/p><\/div>\n<br \/>\n<h4>\u5f15\u6570\u30fb\u623b\u308a\u5024<\/h4>\n<div class=\"m30-l\">\n<ul style=\"margin-top:0;margin-bottom:0;padding-top:0;padding-bottom:0;\">\n<li>DBFileName&nbsp;&nbsp;\u30fb\u30fb\u30fb&nbsp;&nbsp;\u5bfe\u8c61\u3068\u306a\u308bExcel\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u3068\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\uff09<\/li>\n<li>OLEDBProvider&nbsp;&nbsp;\u30fb\u30fb\u30fb&nbsp;&nbsp;Excel2007\u4ee5\u964d\u3067\u4f5c\u6210\u3057\u305f\u30d6\u30c3\u30af\u306b\u63a5\u7d9a\u3059\u308b\u5834\u5408\u306f\u3001<strong>Microsoft.ACE.OLEDB.12.0;<\/strong>\u3001Excel2002\/2003\u3067\u4f5c\u6210\u3057\u305f\u30d6\u30c3\u30af\u306b\u63a5\u7d9a\u3059\u308b\u5834\u5408\u306f\u3001<strong>Microsoft.Jet.OLEDB.4.0;<\/strong> \u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/li>\n<li>EXProperties&nbsp;&nbsp;\u30fb\u30fb\u30fb&nbsp;&nbsp;\uff13\u3064\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u5024\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\u5404\u30d7\u30ed\u30d1\u30c6\u30a3\u5024\u306f\u300c;\uff08\u30bb\u30df\u30b3\u30ed\u30f3\uff09\u300d\u3067\u533a\u5207\u308a\u307e\u3059\u3002\u5168\u4f53\u3092\u300c&#8221;\u300d\u3067\u62ec\u308a\u307e\u3059\u3002<br \/>\n\t\uff08\u4f8b\uff09Extended Properties = &#8220;Excel 12.0;HDR=Yes;&#8221;<\/li>\n<\/ul><\/div>\n<br \/>\n<p class=\"caution4\"><strong>Extended Properties<\/strong> \u306b\u6307\u5b9a\u3059\u308b\u5404\u30d7\u30ed\u30d1\u30c6\u30a3\u5024\u306b\u3064\u3044\u3066<br \/>\n    \u30fb<strong>Excel 12.0<\/strong> \u306e\u90e8\u5206\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u7a2e\u985e\u3092\u8868\u3057\u3066\u3044\u307e\u3059\u3002Excel2007\u4ee5\u964d\u3067\u4f5c\u6210\u3057\u305f\u30d6\u30c3\u30af\u306b\u63a5\u7d9a\u3059\u308b\u5834\u5408\u306f\u3001<strong>Excel 12.0<\/strong>\u3001Excel2002\/2003\u3067\u4f5c\u6210\u3057\u305f\u30d6\u30c3\u30af\u306b\u63a5\u7d9a\u3059\u308b\u5834\u5408\u306f\u3001<strong>Excel 8.0<\/strong> \u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<br \/>\n    \u30fb<strong>HDR<\/strong> \u306f\u3001\u30b7\u30fc\u30c8\u306e\uff11\u884c\u76ee\u3092\u30d5\u30a3\u30fc\u30eb\u30c9\u540d\u3068\u3057\u3066\u6271\u3046\u304b\u3069\u3046\u304b\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<strong>No<\/strong> \u3092\u6307\u5b9a\u3057\u305f\u5834\u5408\u3001\u30d5\u30a3\u30fc\u30eb\u30c9\u540d\u306f\u3001F1\u3001F2\u3001F3 \u306e\u3088\u3046\u306b <strong>F[\u5217\u756a\u53f7]<\/strong> \u3067\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n<p class=\"caution4\">Excel\u306e\u30b7\u30fc\u30c8\u3001\u30bb\u30eb\u7bc4\u56f2\u3092\u3001\u30c6\u30fc\u30d6\u30eb\u3068\u3057\u3066\u6271\u3044\u307e\u3059\u3002<br \/>\n    \u30ef\u30fc\u30af\u30b7\u30fc\u30c8\u5168\u4f53\uff1aSelect * from [Sheet1$]<br \/>\n    \u540d\u524d\u306a\u3057\u30bb\u30eb\u7bc4\u56f2\uff1aSelect * from [Sheet1$A2:E10]<br \/>\n    \u540d\u524d\u4ed8\u304d\u30bb\u30eb\u7bc4\u56f2\uff1aSelect * from \u7bc4\u56f2\u540d\n  <\/p>\n<\/div>\n<br \/><br \/>\n<h2>Excel\u30d5\u30a1\u30a4\u30eb\u3078\u63a5\u7d9a\u3059\u308b\u4f8b<\/h2>\n<div class=\"m30-l\">\n<h3 id=\"vba_sample\">\u30b5\u30f3\u30d7\u30ebVBA\u30bd\u30fc\u30b9<\/h3>\n<pre class=\"lang:vb mark:0 decode:true \" title=\"Excel \u3078\u63a5\u7d9a\" data-url=\"http:\/\/example.co.jp\" >\r\n\u3000\r\nSub Sample_ADO_Excel()\r\n\r\n    Dim cn As New ADODB.Connection\r\n    Dim rs As New ADODB.Recordset\r\n    Dim EXProperties As String\r\n    Dim constr As String\r\n    Dim DBFile As String\r\n    Dim strSQL As String\r\n    Dim i As Long\r\n    Dim j As Long\r\n    \r\n    DBFile = ActiveWorkbook.Path & \"\\mydb1.xlsx\"\r\n    EXProperties = \"\"\"Excel 12.0;HDR=No;\"\"\"\r\n    \r\n    constr = \"Provider=Microsoft.ACE.OLEDB.12.0;\" & _\r\n             \"Data Source=\" & DBFile & \";Extended Properties=\" & EXProperties\r\n    \r\n    cn.ConnectionString = constr\r\n    cn.Open\r\n    \r\n    strSQL = \"Select * from [mydb1$];\"\r\n    rs.Source = strSQL\r\n    rs.ActiveConnection = cn\r\n    rs.Open\r\n    \r\n    With Worksheets(\"excel\")\r\n        \r\n        .Cells.Clear\r\n        i = 1\r\n        Do Until rs.EOF\r\n            For j = 0 To rs.Fields.Count - 1\r\n                If i = 1 Then .Cells(i, j + 1) = rs.Fields(j).Name\r\n                .Cells(i + 1, j + 1) = rs(j).Value\r\n            Next j\r\n            rs.MoveNext\r\n            i = i + 1\r\n        Loop\r\n    \r\n    End With\r\n    \r\n    rs.Close\r\n    Set rs = Nothing\r\n    \r\n    cn.Close\r\n    Set cn = Nothing\r\n    \r\nEnd Sub\r\n\u3000\r\n  <\/pre>\n<br \/>\n<h3>\u5b9f\u884c\u7d50\u679c<\/h3>\n<p><a href=\"https:\/\/i1.wp.com\/excelwork.info\/excel\/wp\/wp-content\/uploads\/2017\/07\/excelado_01.jpg?ssl=1\" rel=\"lightbox[6041]\"><img loading=\"lazy\" src=\"https:\/\/i1.wp.com\/excelwork.info\/excel\/wp\/wp-content\/uploads\/2017\/07\/excelado_01.jpg?resize=534%2C334&#038;ssl=1\" alt=\"Excel\u306b\u63a5\u7d9a\u3059\u308b\uff08ADO\uff09\" width=\"534\" height=\"334\" class=\"alignnone size-full wp-image-6043\" srcset=\"https:\/\/i1.wp.com\/excelwork.info\/excel\/wp\/wp-content\/uploads\/2017\/07\/excelado_01.jpg?w=534&amp;ssl=1 534w, https:\/\/i1.wp.com\/excelwork.info\/excel\/wp\/wp-content\/uploads\/2017\/07\/excelado_01.jpg?resize=400%2C250&amp;ssl=1 400w\" sizes=\"(max-width: 534px) 100vw, 534px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<\/div>\n<br \/><br \/>\n","protected":false},"excerpt":{"rendered":"<p>ADO\u3067Excel\u30d5\u30a1\u30a4\u30eb\u306b\u63a5\u7d9a ADO \u3092\u4f7f\u7528\u3057\u3066Excel\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u307f &#8230; <\/p>\n","protected":false},"author":1,"featured_media":6043,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"amp_status":"","spay_email":"","jetpack_publicize_message":""},"categories":[948],"tags":[950,954,965,964,952,951,814,955,949],"jetpack_featured_media_url":"https:\/\/i1.wp.com\/excelwork.info\/excel\/wp\/wp-content\/uploads\/2017\/07\/excelado_01.jpg?fit=534%2C334&ssl=1","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s4Ye9i-excelado","_links":{"self":[{"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/posts\/6041"}],"collection":[{"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/comments?post=6041"}],"version-history":[{"count":5,"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/posts\/6041\/revisions"}],"predecessor-version":[{"id":6422,"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/posts\/6041\/revisions\/6422"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/media\/6043"}],"wp:attachment":[{"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/media?parent=6041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/categories?post=6041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/tags?post=6041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}