回答編集履歴

1

勘違いしていた部分に取り消し線追加など

2018/02/18 10:14

投稿

imihito
imihito

スコア2166

test CHANGED
@@ -1,10 +1,12 @@
1
- ざっくりとしかコードを見てませんがまず一つ。
1
+ ~~ざっくりとしかコードを見てませんがまず一つ。
2
-
3
-
4
2
 
5
3
  **バイナリファイルはテキストファイルではありません**
6
4
 
7
- 出力時はちゃんとByteに変換してあげてください。
5
+ 出力時はちゃんとByteに変換してあげてください。~~
6
+
7
+
8
+
9
+ (Byte配列化されているので、バイナリの形式の問題だと思われます)
8
10
 
9
11
 
10
12
 
@@ -75,3 +77,125 @@
75
77
  }
76
78
 
77
79
  ```
80
+
81
+
82
+
83
+ ###### 蛇足:Excelの選択範囲を画像化して保存するPowerShellスクリプト
84
+
85
+
86
+
87
+ ```
88
+
89
+ # Excelの選択範囲を画像化して保存するスクリプト PowerShell ISE に張り付けて実行
90
+
91
+ $ErrorActionPreference = 'Stop'
92
+
93
+
94
+
95
+ # bmpの保存先
96
+
97
+ [string]$savePath = [IO.Path]::Combine([Environment]::GetFolderPath('MyPictures'), 'pstmp.bmp')
98
+
99
+ # 一セルあたり何ピクセルで表現するか
100
+
101
+ [int]$zoom = 2
102
+
103
+
104
+
105
+ # Excelを取得
106
+
107
+ [__ComObject]$appXl = [Runtime.InteropServices.Marshal]::GetActiveObject('Excel.Application')
108
+
109
+ [__ComObject]$srcRng = $appXl.Selection
110
+
111
+
112
+
113
+ # 範囲の行数取得
114
+
115
+ [int]$rowCnt, [int]$colCnt = $srcRng.Rows.Count, $srcRng.Columns.Count # TODO:COM Release
116
+
117
+
118
+
119
+ # 出力する画像をインスタンス
120
+
121
+ [hashtable]$bmpArg = @{
122
+
123
+ TypeName = 'Drawing.Bitmap'
124
+
125
+ ArgumentList = ($colCnt * $zoom), ($rowCnt * $zoom)
126
+
127
+ }
128
+
129
+ [Drawing.Bitmap]$bmp = New-Object @bmpArg
130
+
131
+
132
+
133
+ # [Excel.Range].Interior.Color => [Drawing.Color]
134
+
135
+ Function XlRGBtoDrawColor([int]$XlRGB) {
136
+
137
+ return [Drawing.Color]::FromArgb( #もっと良い方法がありそう
138
+
139
+ $XlRGB -band 0xFF, # Red
140
+
141
+ ($XlRGB -band 0xFF00) / 0x100, # Green
142
+
143
+ ($XlRGB -band 0xFF0000) / 0x10000 # Blue
144
+
145
+ )
146
+
147
+ }
148
+
149
+
150
+
151
+ # セル範囲ループ
152
+
153
+ for ($r = 1 ; $r -le $rowCnt ; $r += 1) {
154
+
155
+ for ($c = 1 ; $c -le $colCnt ; $c += 1) {
156
+
157
+ [Drawing.Color]$drwClr = XlRGBtoDrawColor -XlRGB ($srcRng.Item($r , $c).Interior.Color) # TODO:COM Release
158
+
159
+ # セル座標は1始まり、画像内座標は0始まり
160
+
161
+ [int]$startX = ($c - 1) * $zoom
162
+
163
+ [int]$startY = ($r - 1) * $zoom
164
+
165
+
166
+
167
+ # zoom分塗りつぶしのループ もっと良い方法がありそう
168
+
169
+ for($x = $startX ; $x -lt $startX + $zoom ; $x += 1) {
170
+
171
+ for($y = $startY ; $y -lt $startY + $zoom ; $y += 1) {
172
+
173
+ $bmp.SetPixel($x, $y, $drwClr)
174
+
175
+ }
176
+
177
+ }
178
+
179
+
180
+
181
+ }
182
+
183
+ }
184
+
185
+
186
+
187
+ # COM解放(適当)
188
+
189
+ [Runtime.InteropServices.Marshal]::FinalReleaseComObject($srcRng) > $null ; $srcRng = $null
190
+
191
+ [Runtime.InteropServices.Marshal]::FinalReleaseComObject($appXl) > $null ; $appXl = $null
192
+
193
+
194
+
195
+ # 保存
196
+
197
+ $bmp.Save($savePath, 'bmp')
198
+
199
+ explorer "/select,$savePath"
200
+
201
+ ```