{"id":3003,"date":"2019-06-16T02:10:33","date_gmt":"2019-06-15T17:10:33","guid":{"rendered":"https:\/\/1bed.allright.life\/?p=3003"},"modified":"2020-04-18T18:39:09","modified_gmt":"2020-04-18T09:39:09","slug":"post-3003","status":"publish","type":"post","link":"https:\/\/1bed.allright.life\/?p=3003","title":{"rendered":"PowerShell \u3092\u4f7f\u3063\u3066\u3001ExcelDataReader \u3067 Excel \u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080"},"content":{"rendered":"<h2>\u7d4c\u7def<\/h2>\n<p>\u304a\u5ba2\u3055\u307e\u304b\u3089\u3044\u305f\u3060\u304f Excel \u30d5\u30a1\u30a4\u30eb\u3092\u3001\u3042\u308b\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u306b\u3059\u308b\u3068\u3044\u3046\u3053\u3068\u3092\u3001\u624b\u4f5c\u696d\u3067\u3061\u307e\u3061\u307e\u3084\u308b\u306e\u3092\u3084\u3081\u305f\u3044\u306e\u3067\u3001\u3061\u3087\u3063\u3068\u30b9\u30af\u30ea\u30d7\u30c8\u66f8\u3044\u3066\u307e\u3059<\/p>\n<p>\u30b0\u30b0\u3063\u305f\u3089\u3001\u300c\u8aad\u307f\u8fbc\u3080\u3060\u3051\u306a\u3089 ExcelDataReader \u304c\u901f\u304f\u3066\u3044\u3044\u305e\u30fc\u300d\u300cExcelDataReader \u306f xlsx \u3060\u3051\u3067\u306f\u306a\u304f\u3066\u3001xls \u3082\u8aad\u3081\u308b\u305e\u30fc\u300d\u7684\u306a\u3053\u3068\u3092\u66f8\u3044\u3066\u3044\u3066\u3001\u3061\u3087\u3063\u3068\u89e6\u3063\u3066\u307f\u305f\u304f\u306a\u3063\u305f\u306e\u3067\u3059<\/p>\n<h2>\u74b0\u5883<\/h2>\n<ul>\n<li>Windows 10 Pro<\/li>\n<li>PowerShell 5.1<\/li>\n<li>ExcelDataReader 3.6.0<\/li>\n<li>ExcelDataReader.DataSet 3.6.0<\/li>\n<\/ul>\n<h2>\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9<\/h2>\n<ul>\n<li>\n<p>ExcelDataReader<br \/>\n<a href=\"https:\/\/www.nuget.org\/packages\/ExcelDataReader\/\">https:\/\/www.nuget.org\/packages\/ExcelDataReader\/<\/a><\/p>\n<\/li>\n<li>\n<p>ExcelDataReader.Dataset<br \/>\n<a href=\"https:\/\/www.nuget.org\/packages\/ExcelDataReader.DataSet\/\">https:\/\/www.nuget.org\/packages\/ExcelDataReader.DataSet\/<\/a><\/p>\n<\/li>\n<\/ul>\n<p>\u305d\u308c\u305e\u308c\u3001\u53f3\u30e1\u30cb\u30e5\u30fc\u306b\u3042\u308b Download package \u304b\u3089 nuget \u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u3001\u62e1\u5f35\u5b50\u3092 zip \u306b\u5909\u3048\u3066\u89e3\u51cd\u5f8c\u3001DLL \u3092\u53d6\u308a\u51fa\u3057\u305f<\/p>\n<h2>Excel \u30d5\u30a1\u30a4\u30eb\u306e\u6e96\u5099<\/h2>\n<p>\u30b5\u30f3\u30d7\u30eb\u3067 Excel \u30d5\u30a1\u30a4\u30eb (test.xlsx) \u3092\u4f5c\u308a\u307e\u3057\u305f<\/p>\n<p>\u300c\u5546\u54c1\u300d\u3068\u300c\u5f93\u696d\u54e1\u300d\u3068\u3044\u3046\u30b7\u30fc\u30c8\u3092\u4f5c\u3063\u3066\u3001\u305d\u308c\u305e\u308c\u9069\u5f53\u306a\u30c7\u30fc\u30bf\u3092\u5165\u308c\u3066\u3044\u307e\u3059<\/p>\n<p><a href=\"https:\/\/1bed.allright.life\/wp-content\/uploads\/2019\/06\/1.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" src=\"https:\/\/1bed.allright.life\/wp-content\/uploads\/2019\/06\/1.png\" alt=\"\" \/><\/a><\/p>\n<p><a href=\"https:\/\/1bed.allright.life\/wp-content\/uploads\/2019\/06\/2.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" src=\"https:\/\/1bed.allright.life\/wp-content\/uploads\/2019\/06\/2.png\" alt=\"\" \/><\/a><\/p>\n<h2>Excel \u30c7\u30fc\u30bf\u306e\u8aad\u307f\u8fbc\u307f<\/h2>\n<p>DLL 2 \u3064\u3001Excel \u30d5\u30a1\u30a4\u30eb 1 \u3064\u3001 PowerShell \u30d5\u30a1\u30a4\u30eb\u3092\u3059\u3079\u3066\u540c\u3058\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u7f6e\u304d\u307e\u3057\u305f<\/p>\n<p>\u30b3\u30b3\u307e\u3067\u306e\u72b6\u614b<br \/>\n<a href=\"https:\/\/1bed.allright.life\/wp-content\/uploads\/2019\/06\/3.png\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" src=\"https:\/\/1bed.allright.life\/wp-content\/uploads\/2019\/06\/3.png\" alt=\"\" \/><\/a><\/p>\n<p>\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080\u3068\u3053\u308d\u307e\u3067\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u307f\u307e\u3059<\/p>\n<pre><code class=\"language-powershell\">$ErrorActionPreference = &#039;Stop&#039;\n\n# \u30ab\u30ec\u30f3\u30c8\u306e\u30d1\u30b9\n$currentPath = Split-Path -Parent $MyInvocation.MyCommand.Path\n\n# DDL \u306e\u8aad\u307f\u8fbc\u307f\n[void][Reflection.Assembly]::LoadFile((Join-Path $currentPath &#039;.\\ExcelDataReader.dll&#039;))\n[void][Reflection.Assembly]::LoadFile((Join-Path $currentPath &#039;.\\ExcelDataReader.DataSet.dll&#039;))\n\ntry {\n    # FileStream \u3092\u958b\u304f\n    $stream = [System.IO.File]::Open(&#039;.\\test.xlsx&#039;, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)\n\n    # ExcelReader \u306e\u751f\u6210 \n    $reader = [ExcelDataReader.ExcelReaderFactory]::CreateReader($stream)\n\n    # ExcelDataSetConfiguration \u751f\u6210\n    $config = [ExcelDataReader.ExcelDataSetConfiguration]::new()\n    $config.UseColumnDataType = $true\n    $config.ConfigureDataTable = {\n        $conf = [ExcelDataReader.ExcelDataTableConfiguration]::new()\n        $conf.UseHeaderRow = $true\n        return $conf\n    }\n\n    # \u30c7\u30fc\u30bf\u306e\u8aad\u307f\u8fbc\u307f\n    $data = [ExcelDataReader.ExcelDataReaderExtensions]::AsDataSet($reader, $config)\n} catch {\n    throw\n} finally {\n    # \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u89e3\u653e\n    if ($reader -is [ExcelDataReader.IExcelDataReader]) {\n        $reader.Close()\n        $reader.Dispose()\n        $reader = $null\n    }\n    if ($stream -is [System.IO.FileStream]) {\n        $stream.Close()\n        $stream.Dispose()\n        $stream = $null\n    }\n}<\/code><\/pre>\n<p><code>AsDataSet<\/code> \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3068\u3053\u308d\u304c\u3061\u3087\u3063\u3068\u5927\u5909\u3067\u3057\u305f&#8230; (C# \u306e\u4f8b\u306f\u8150\u308b\u307b\u3069\u3042\u3063\u305f\u3093\u3067\u3059\u304c\uff57)<\/p>\n<p>\u3053\u3053\u307e\u3067\u3067\u3001 <code>$data<\/code> \u306b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3067\u304d\u3066\u3044\u307e\u3059\u306e\u3067\u3001\u30c7\u30fc\u30bf\u30bf\u30a4\u30d7\u3092\u898b\u3066\u307f\u307e\u3059<br \/>\n<a href=\"https:\/\/1bed.allright.life\/wp-content\/uploads\/2019\/06\/4.png\" data-rel=\"lightbox-image-3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" src=\"https:\/\/1bed.allright.life\/wp-content\/uploads\/2019\/06\/4.png\" alt=\"\" \/><\/a><br \/>\nDataSet \u3067\u8fd4\u3063\u3066\u304d\u3066\u3044\u308b\u306e\u304c\u308f\u304b\u308a\u307e\u3059<\/p>\n<p>\u4e2d\u3092\u96d1\u306b\u304b\u304f\u306b\u3093\u3057\u3066\u307f\u307e\u3059<\/p>\n<pre><code class=\"language-powershell\"># \u96d1\u306b\u4e2d\u3092\u78ba\u8a8d\n$data.Tables | %{\n    Write-Output (&#039;SheetName: {0}&#039; -f $_.TableName)\n    Format-Table -InputObject $_.Rows\n}<\/code><\/pre>\n<p><a href=\"https:\/\/1bed.allright.life\/wp-content\/uploads\/2019\/06\/5.png\" data-rel=\"lightbox-image-4\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" src=\"https:\/\/1bed.allright.life\/wp-content\/uploads\/2019\/06\/5.png\" alt=\"\" \/><\/a><\/p>\n<p>\u6700\u5f8c\u306b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u89e3\u653e\u3057\u3066\u7d42\u308f\u308a\u307e\u3059<\/p>\n<pre><code class=\"language-powershell\"># ExcelDataReader.DataSet \u306f .NET 3.5 \u3067\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u3067\u3001System.Type \u306b\u6bd4\u8f03\u6f14\u7b97\u5b50\u304c\u5b9f\u88c5\u3055\u308c\u3066\u3044\u306a\u3044\u306e\u3067 FullName \u3067\u5224\u65ad\nif ($data.GetType().FullName -eq &quot;System.Data.DataSet&quot;) {\n    $data.Clear()\n    $data.Dispose()\n    $data = $null\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7d4c\u7def \u304a\u5ba2\u3055\u307e\u304b\u3089\u3044\u305f\u3060\u304f Excel \u30d5\u30a1\u30a4\u30eb\u3092\u3001\u3042\u308b\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u306b\u3059\u308b\u3068\u3044\u3046\u3053\u3068\u3092\u3001\u624b\u4f5c\u696d\u3067\u3061\u307e\u3061\u307e\u3084\u308b\u306e\u3092\u3084\u3081\u305f\u3044\u306e\u3067\u3001\u3061\u3087\u3063\u3068\u30b9\u30af\u30ea\u30d7\u30c8\u66f8\u3044\u3066\u307e\u3059 \u30b0\u30b0\u3063\u305f\u3089\u3001\u300c\u8aad\u307f\u8fbc\u3080\u3060\u3051\u306a\u3089 ExcelDataReader  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[97,62],"class_list":["post-3003","post","type-post","status-publish","format-standard","hentry","category-program","tag-excel","tag-powershell"],"_links":{"self":[{"href":"https:\/\/1bed.allright.life\/index.php?rest_route=\/wp\/v2\/posts\/3003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/1bed.allright.life\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/1bed.allright.life\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/1bed.allright.life\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/1bed.allright.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3003"}],"version-history":[{"count":6,"href":"https:\/\/1bed.allright.life\/index.php?rest_route=\/wp\/v2\/posts\/3003\/revisions"}],"predecessor-version":[{"id":3084,"href":"https:\/\/1bed.allright.life\/index.php?rest_route=\/wp\/v2\/posts\/3003\/revisions\/3084"}],"wp:attachment":[{"href":"https:\/\/1bed.allright.life\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1bed.allright.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1bed.allright.life\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}