-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
234 lines (212 loc) · 8.93 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExcelFontChanger
{
class Program
{
static int Main(string[] args)
{
if (args.Length < 2)
{
return 0;
}
// EXCELファイルパス
string filePath = args[0];
// EXCELファイルフルパス
string fullPath = System.IO.Path.GetFullPath(filePath);
// ファイルが無い場合終了
if (!System.IO.File.Exists(fullPath))
{
return 0;
}
// シート名
string strWorksheetName = "";
// フォント名
string fontName = args[1];
//末端の改行文字を取り除く
fontName.Trim();
Microsoft.Office.Interop.Excel.Application xlApp = null;
Microsoft.Office.Interop.Excel.Workbooks xlBooks = null;
Microsoft.Office.Interop.Excel.Workbook xlBook = null;
Microsoft.Office.Interop.Excel.Sheets xlSheets = null;
Microsoft.Office.Interop.Excel.Worksheet xlSheet = null;
Microsoft.Office.Interop.Excel.Range xlRange = null;
Microsoft.Office.Interop.Excel.Font xlFont = null;
Microsoft.Office.Interop.Excel.Shapes xlShapes = null;
Microsoft.Office.Interop.Excel.Shape xlShape = null;
Microsoft.Office.Interop.Excel.TextFrame2 xlTextFrame2 = null;
Microsoft.Office.Core.TextRange2 xlTextRange2 = null;
Microsoft.Office.Core.Font2 xlFont2 = null;
try
{
// EXCEL起動
xlApp = new Microsoft.Office.Interop.Excel.Application();
// EXCELは非表示にする
xlApp.Visible = false;
// 保存確認なしにする
xlApp.DisplayAlerts = false;
// Workbook開く
xlBooks = xlApp.Workbooks;
xlBook = xlBooks.Open(fullPath);
// Worksheet開く
xlSheets = xlBook.Sheets;
for (int ii = 1; ii <= xlSheets.Count; ++ii)
{
xlSheet = xlSheets.Item[ii];
strWorksheetName = xlSheet.Name;
// セルのフォントを変更する
xlRange = xlSheet.UsedRange;
xlFont = xlRange.Font;
xlFont.Name = fontName;
if (xlFont != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlFont);
xlFont = null;
}
if (xlRange != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange);
xlRange = null;
}
// 図形のフォントを変更する
xlShapes = xlSheet.Shapes;
for (int jj = 1; jj <= xlShapes.Count; ++jj)
{
xlShape = xlShapes.Item(jj);
xlTextFrame2 = xlShape.TextFrame2;
if (xlTextFrame2.HasText == Microsoft.Office.Core.MsoTriState.msoTrue)
{
xlTextRange2 = xlTextFrame2.TextRange;
xlFont2 = xlTextRange2.Font;
// 右から左へ記述するフォントの設定(例:アラビア語)
xlFont2.NameComplexScript = fontName;
// 全角フォントの設定(例:日本語)
xlFont2.NameFarEast = fontName;
// 半角フォントの設定(例:英語)
xlFont2.Name = fontName;
xlRange = xlShape.TopLeftCell;
System.Console.WriteLine(xlRange.Address + " [" + xlShape.Name + "]" + "のフォントを変更しました。");
if (xlRange != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange);
xlRange = null;
}
if (xlFont2 != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlFont2);
xlFont2 = null;
}
if (xlTextRange2 != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlTextRange2);
xlTextRange2 = null;
}
}
if (xlTextFrame2 != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlTextFrame2);
xlTextFrame2 = null;
}
if (xlShape != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlShape);
xlShape = null;
}
}
if (xlShapes != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlShapes);
xlShapes = null;
}
if (xlSheet != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
xlSheet = null;
}
System.Console.WriteLine("処理が終了しました。シート名:" + strWorksheetName);
}
if (xlSheets != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets);
xlSheets = null;
}
// Workbook保存
xlBook.Save();
System.Console.WriteLine("処理が終了しました。ファイル名:" + xlBook.Name);
}
catch
{
System.Console.WriteLine("ランタイムエラーが発生しました。シート名:" + strWorksheetName);
}
finally
{
if (xlFont2 != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlFont2);
xlFont2 = null;
}
if (xlTextRange2 != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlTextRange2);
xlTextRange2 = null;
}
if (xlTextFrame2 != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlTextFrame2);
xlTextFrame2 = null;
}
if (xlShape != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlShape);
xlShape = null;
}
if (xlShapes != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlShapes);
xlShapes = null;
}
if (xlFont != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlFont);
xlFont = null;
}
if (xlRange != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange);
xlRange = null;
}
if (xlSheet != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
xlSheet = null;
}
if (xlSheets != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets);
xlSheets = null;
}
if (xlBook != null)
{
xlBook.Close(false);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
xlBook = null;
}
if (xlBooks != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks);
xlBooks = null;
}
if (xlApp != null)
{
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
}
return 0;
}
}
}