aio_elin 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +675 -0
  3. data/README.md +9 -0
  4. data/config.rb +39 -0
  5. data/lib/aio/base/debug.rb +30 -0
  6. data/lib/aio/base/toolkit/array.rb +24 -0
  7. data/lib/aio/base/toolkit/date.rb +46 -0
  8. data/lib/aio/base/toolkit/deep_clone.rb +10 -0
  9. data/lib/aio/base/toolkit/diff.rb +104 -0
  10. data/lib/aio/base/toolkit/excel_office.rb +421 -0
  11. data/lib/aio/base/toolkit/excel_wps.rb +437 -0
  12. data/lib/aio/base/toolkit/excel_wps.rb.bak +299 -0
  13. data/lib/aio/base/toolkit/ipaddr.rb +28 -0
  14. data/lib/aio/base/toolkit/os.rb +26 -0
  15. data/lib/aio/base/toolkit/regexp.rb +32 -0
  16. data/lib/aio/base/toolkit/string.rb +28 -0
  17. data/lib/aio/base/toolkit/table.rb +441 -0
  18. data/lib/aio/base/toolkit/test_diff.rb +9 -0
  19. data/lib/aio/base/toolkit/test_excel.rb +118 -0
  20. data/lib/aio/base/toolkit/test_word.rb +58 -0
  21. data/lib/aio/base/toolkit/word_wps.rb +492 -0
  22. data/lib/aio/base/toolkit.rb +18 -0
  23. data/lib/aio/base.rb +10 -0
  24. data/lib/aio/core/device/cisco.rb +13 -0
  25. data/lib/aio/core/device/cmd_switch.rb +113 -0
  26. data/lib/aio/core/device/h3c.rb +12 -0
  27. data/lib/aio/core/device/huawei.rb +10 -0
  28. data/lib/aio/core/device/juniper.rb +10 -0
  29. data/lib/aio/core/device/maipu.rb +11 -0
  30. data/lib/aio/core/device/methods.rb +233 -0
  31. data/lib/aio/core/device/methods.rb.bak +199 -0
  32. data/lib/aio/core/device/parent_device.rb +275 -0
  33. data/lib/aio/core/device.rb +15 -0
  34. data/lib/aio/core/device_manager.rb +207 -0
  35. data/lib/aio/core/module/cmd.rb +160 -0
  36. data/lib/aio/core/module/description.rb +67 -0
  37. data/lib/aio/core/module/input_style.rb +98 -0
  38. data/lib/aio/core/module/output_style.rb +80 -0
  39. data/lib/aio/core/module/platform.rb +30 -0
  40. data/lib/aio/core/module/ranking.rb +24 -0
  41. data/lib/aio/core/module/special_style.rb +30 -0
  42. data/lib/aio/core/module/template.rb +13 -0
  43. data/lib/aio/core/module.rb +15 -0
  44. data/lib/aio/core/module_loader.rb +161 -0
  45. data/lib/aio/core/module_loader.rb.bak +148 -0
  46. data/lib/aio/core/module_manager.rb +112 -0
  47. data/lib/aio/core/module_manager.rb.bak +78 -0
  48. data/lib/aio/core/parse/file.rb +19 -0
  49. data/lib/aio/core/parse/parser.rb +91 -0
  50. data/lib/aio/core/parse/parser_machine/bak/has_cmd_state.rb +14 -0
  51. data/lib/aio/core/parse/parser_machine/bak/has_context_state.rb +11 -0
  52. data/lib/aio/core/parse/parser_machine/bak/no_cmd_state.rb +11 -0
  53. data/lib/aio/core/parse/parser_machine/bak/no_context_state.rb +11 -0
  54. data/lib/aio/core/parse/parser_machine/full_state.rb +12 -0
  55. data/lib/aio/core/parse/parser_machine/has_device_state.rb +44 -0
  56. data/lib/aio/core/parse/parser_machine/no_device_state.rb +12 -0
  57. data/lib/aio/core/parse/parser_machine/parent_state.rb +26 -0
  58. data/lib/aio/core/parse/parser_machine.rb +47 -0
  59. data/lib/aio/core/parse.rb +6 -0
  60. data/lib/aio/core/text/context.rb +211 -0
  61. data/lib/aio/core/text/line_string.rb +74 -0
  62. data/lib/aio/core/text/warning.rb +179 -0
  63. data/lib/aio/core/text.rb +7 -0
  64. data/lib/aio/core/warning/description.rb +47 -0
  65. data/lib/aio/core/warning/warning_summarize.rb +232 -0
  66. data/lib/aio/core/warning.rb +6 -0
  67. data/lib/aio/core.rb +32 -0
  68. data/lib/aio/license.rb +687 -0
  69. data/lib/aio/resource/cover_picture.png +0 -0
  70. data/lib/aio/resource/line.png +0 -0
  71. data/lib/aio/resource/logo.png +0 -0
  72. data/lib/aio/ui/banner.rb +85 -0
  73. data/lib/aio/ui/readable_text.rb +75 -0
  74. data/lib/aio/ui/verbose.rb +29 -0
  75. data/lib/aio/ui/version.rb +9 -0
  76. data/lib/aio/ui.rb +138 -0
  77. data/lib/aio.rb +13 -0
  78. data/lib/modules/cmd/cisco/show_clock.rb +32 -0
  79. data/lib/modules/cmd/cisco/show_cpu.rb +32 -0
  80. data/lib/modules/cmd/cisco/show_env.rb +71 -0
  81. data/lib/modules/cmd/cisco/show_failover.rb +39 -0
  82. data/lib/modules/cmd/cisco/show_interface_ip_brief.rb +24 -0
  83. data/lib/modules/cmd/cisco/show_interfaces.rb +177 -0
  84. data/lib/modules/cmd/cisco/show_inv.rb +43 -0
  85. data/lib/modules/cmd/cisco/show_ip_interface_brief.rb +42 -0
  86. data/lib/modules/cmd/cisco/show_ip_ospf_nei.rb +38 -0
  87. data/lib/modules/cmd/cisco/show_ip_route_summary.rb +81 -0
  88. data/lib/modules/cmd/cisco/show_memory.rb +27 -0
  89. data/lib/modules/cmd/cisco/show_process_cpu.rb +35 -0
  90. data/lib/modules/cmd/cisco/show_process_memory.rb +63 -0
  91. data/lib/modules/cmd/cisco/show_version.rb +97 -0
  92. data/lib/modules/cmd/h3c/display_clock.rb +32 -0
  93. data/lib/modules/cmd/h3c/display_cpu.rb +51 -0
  94. data/lib/modules/cmd/h3c/display_device_manuinfo.rb +45 -0
  95. data/lib/modules/cmd/h3c/display_fan.rb +91 -0
  96. data/lib/modules/cmd/h3c/display_interface.rb +203 -0
  97. data/lib/modules/cmd/h3c/display_ip_interface_brief.rb +34 -0
  98. data/lib/modules/cmd/h3c/display_ip_routing.rb +68 -0
  99. data/lib/modules/cmd/h3c/display_ip_routing_stat.rb +90 -0
  100. data/lib/modules/cmd/h3c/display_memory.rb +35 -0
  101. data/lib/modules/cmd/h3c/display_ospf_peer.rb +38 -0
  102. data/lib/modules/cmd/h3c/display_power.rb +90 -0
  103. data/lib/modules/cmd/h3c/display_version.rb +54 -0
  104. data/lib/modules/cmd/h3c/display_vrrp_verbose.rb +86 -0
  105. data/lib/modules/cmd/maipu/show_env.rb +34 -0
  106. data/lib/modules/cmd/maipu/show_ip_route.rb +74 -0
  107. data/lib/modules/cmd/maipu/show_ip_route_statistic.rb +36 -0
  108. data/lib/modules/cmd/maipu/show_memory.rb +31 -0
  109. data/lib/modules/cmd/maipu/show_system_fan.rb +66 -0
  110. data/lib/modules/cmd/maipu/show_system_power.rb +66 -0
  111. data/lib/modules/cmd/maipu/show_version.rb +50 -0
  112. data/lib/modules/description/style/crc.rb +22 -0
  113. data/lib/modules/description/style/input_errors.rb +22 -0
  114. data/lib/modules/description/style/overrun.rb +22 -0
  115. data/lib/modules/description/style/proc_ratio.rb +22 -0
  116. data/lib/modules/description/style/protocol.rb +22 -0
  117. data/lib/modules/description/style/register.rb +22 -0
  118. data/lib/modules/description/style/runts.rb +22 -0
  119. data/lib/modules/description/style/template.rb +22 -0
  120. data/lib/modules/description/style/total_output_drops.rb +22 -0
  121. data/lib/modules/input/style/compare_xml.rb +89 -0
  122. data/lib/modules/input/style/console.rb +222 -0
  123. data/lib/modules/input/style/console.rb.bak +114 -0
  124. data/lib/modules/input/style/xinhua_nat.rb +70 -0
  125. data/lib/modules/output/style/boc_day.rb +223 -0
  126. data/lib/modules/output/style/cmds.rb +77 -0
  127. data/lib/modules/output/style/compare_json.rb +26 -0
  128. data/lib/modules/output/style/compare_report.rb +31 -0
  129. data/lib/modules/output/style/compare_xml.rb +86 -0
  130. data/lib/modules/output/style/excel_table_office.rb +407 -0
  131. data/lib/modules/output/style/excel_table_wps.rb +400 -0
  132. data/lib/modules/output/style/summary_report.rb +563 -0
  133. data/lib/modules/special/style/compare.rb +110 -0
  134. metadata +176 -0
@@ -0,0 +1,437 @@
1
+ module Aio::Base::Toolkit
2
+ module ExcelWps
3
+ class WorkBook
4
+
5
+ include Aio::Ui::Verbose
6
+
7
+ @@worksheets_name = []
8
+ def initialize(encoding = "utf-8")
9
+
10
+ if Aio::Base::Toolkit::OS.windows?
11
+ require "win32ole"
12
+ else
13
+ print_error "只有Windows系统才能使用Excel模块"
14
+ exit 0
15
+ end
16
+
17
+
18
+ @excel = WIN32OLE.new("excel.Application")
19
+ @excel.visible = false
20
+ @workbook = @excel.workbooks.add
21
+ @encoding = encoding
22
+ create_style
23
+ end
24
+
25
+ # 警告提示开关
26
+ def display_alerts=(bool)
27
+ @excel.DisplayAlerts = bool
28
+ end
29
+
30
+ def add_worksheet(name)
31
+ while @@worksheets_name.include?(name)
32
+ name += "1"
33
+ end
34
+ @@worksheets_name << name
35
+ worksheet = @workbook.worksheets.add
36
+ worksheet.activate
37
+
38
+ # 在同一进程中多次打开会出现name的问题, 所以干脆全部使用sheet
39
+ # worksheet.name = name
40
+ worksheet.name = "sheet"
41
+ return WorkSheet.new(worksheet)
42
+ end
43
+
44
+ def create_style
45
+ sty = @workbook.styles.add("NormalStyle")
46
+ self.class.normal_style(sty)
47
+
48
+ sty = @workbook.styles.add("BoldStyle")
49
+ self.class.bold_style(sty)
50
+
51
+ sty = @workbook.styles.add("TitleStyle")
52
+ self.class.title_style(sty)
53
+ end
54
+
55
+ def self.normal_style(sty)
56
+ sty.font.size = 9
57
+ sty.borders(7).linestyle = 1
58
+ sty.borders(8).linestyle = 1
59
+ sty.borders(9).linestyle = 1
60
+ sty.borders(10).linestyle = 1
61
+ sty.HorizontalAlignment = -4108
62
+ end
63
+
64
+ def self.bold_style(sty)
65
+ sty.font.size = 9
66
+ sty.font.bold = true
67
+ sty.borders(7).linestyle = 1
68
+ sty.borders(8).linestyle = 1
69
+ sty.borders(9).linestyle = 1
70
+ sty.borders(10).linestyle = 1
71
+ sty.HorizontalAlignment = -4108
72
+ end
73
+
74
+ def self.title_style(sty)
75
+ sty.font.size = 20
76
+ sty.font.bold = true
77
+ sty.borders(7).linestyle = 1
78
+ sty.borders(8).linestyle = 1
79
+ sty.borders(9).linestyle = 1
80
+ sty.borders(10).linestyle = 1
81
+ sty.HorizontalAlignment = -4108
82
+ end
83
+
84
+ def show
85
+ @excel.visible = true
86
+ end
87
+
88
+ def save(path)
89
+ @workbook.saveas(path)
90
+ end
91
+
92
+ def close
93
+ @workbook.close
94
+ @excel.quit
95
+ end
96
+ end # class WorkBook
97
+
98
+ class WorkSheet
99
+ IMAGE_ROW_NUM = 56
100
+ @@worksheets_name = []
101
+ def initialize(worksheet)
102
+ @row_count = 1
103
+ @worksheet = worksheet
104
+ @nil_space = []
105
+ end
106
+
107
+ # 增加一个空行
108
+ def add_space_line(n=1)
109
+ return if n < 1
110
+ @row_count += n
111
+ end
112
+
113
+ # 对列进行合并
114
+ def merge(range1, range2)
115
+ @worksheet.range("#{range1}:#{range2}").merge
116
+ end
117
+
118
+ # 产生 ::Range 类
119
+ def range(str)
120
+ @worksheet.range(str)
121
+ end
122
+
123
+ # 添加标题行
124
+ def add_title(name)
125
+ add_row.add_cell(name, false, "BoldStyle")
126
+ end
127
+
128
+ # 设置列的宽度
129
+ def width(col, width)
130
+ @worksheet.Columns("#{col}:#{col}").ColumnWidth = width
131
+ end
132
+
133
+ def height(height)
134
+ @row_height = height
135
+ end
136
+
137
+ # 增加Row类
138
+ def add_row(&block)
139
+ @current_row = Row.new(@worksheet, @row_count)
140
+ @current_row.height = @row_height if @row_height
141
+ @row_count += 1
142
+ yield @current_row if block
143
+ return @current_row
144
+ end
145
+
146
+ # 返回此时的Row类
147
+ def current_row
148
+ return @current_row
149
+ end
150
+
151
+ # 返回此时的行索引
152
+ def current_row_id
153
+ return @current_row.row_id
154
+ end
155
+
156
+ # 添加图像
157
+ def add_image(image_path)
158
+ return unless File.exist?(image_path)
159
+ add_space_line
160
+ add_row
161
+ cell_name = current_row.first_cell
162
+ @worksheet.Range(cell_name).Select
163
+ @worksheet.Pictures.Insert(image_path)
164
+ add_space_line IMAGE_ROW_NUM
165
+ end
166
+
167
+ # 判断是否有垂直分页符存在
168
+ def has_pagebreak?
169
+ @worksheet.VPageBreaks.count > 0
170
+ end
171
+
172
+ # 对列修改分页符
173
+ def pagebreak(col)
174
+ @worksheet.VPageBreaks(1).Location = @worksheet.columns(col)
175
+ end
176
+
177
+ # 返回::WorkSheet
178
+ def worksheet
179
+ @worksheet
180
+ end
181
+
182
+ # 添加图表
183
+ def add_chart(&block)
184
+ ch = @worksheet.Shapes.AddChart
185
+ active = ch.chart
186
+
187
+ # 占位符
188
+ block.call(Chart.new(active))
189
+
190
+ ch.copy
191
+ end
192
+ end # SheetWork Class
193
+
194
+ class Row
195
+ FILL_TYPE = 4
196
+
197
+ attr_reader :row_id
198
+
199
+ @@cell_map = ("A".."Z").to_a
200
+ def initialize(worksheet, row_id)
201
+ @row_id = row_id
202
+ @cell_count = 0
203
+ @worksheet = worksheet
204
+ @nil_space = []
205
+ end
206
+
207
+ # 此时的单元格
208
+ def curent_cell
209
+ return cell_name(@cell_count)
210
+ end
211
+
212
+ # 第一个单元格
213
+ def first_cell
214
+ return cell_name(0)
215
+ end
216
+
217
+ # 设置行高
218
+ def height=(height)
219
+ @worksheet.rows(@row_id).RowHeight = height
220
+ end
221
+
222
+ # 增加单元格
223
+ def add_cell(value, auto_fit = false, style = "NormalStyle")
224
+ range = @worksheet.Range(cell_name(@cell_count))
225
+ range.Value = value.to_s
226
+ range.Style = style
227
+ range.Columns.AutoFit if auto_fit
228
+ @cell_count += 1
229
+ while(@nil_space.include?(to_letter(@cell_count))) do
230
+ range = @worksheet.Range(cell_name(@cell_count))
231
+ range.Value = ""
232
+ range.Style = style
233
+ @cell_count += 1
234
+ end
235
+
236
+ end
237
+
238
+ # 通过索引变成对应的字母
239
+ def to_letter(index)
240
+ @@cell_map.at(index)
241
+ end
242
+
243
+ # 特别注意,由于Toolkit中加入了Array,String的模块
244
+ # 所以判断的时候特别注意要是
245
+ def << (arr)
246
+ case arr
247
+ when ::Array
248
+ arr.size.times do |t|
249
+ add_cell(arr[t])
250
+ end
251
+ when ::String
252
+ add_cell(arr)
253
+ end
254
+ end
255
+
256
+ # 获得单元格的名字(通过索引)
257
+ def cell_name(index)
258
+ second = index % 26
259
+ first = (index - second) / 26
260
+ if first == 0
261
+ return @@cell_map[second] + @row_id.to_s
262
+ end
263
+ first -= 1
264
+ return @@cell_map[first] + @@cell_map[second] + @row_id.to_s
265
+ end
266
+
267
+ # 获得单元格的名字(通过字母)
268
+ def cell_name!(letter)
269
+ return letter + @row_id.to_s
270
+ end
271
+
272
+ def set_cell(index, value, auto_fit = false, style = "NormalStyle")
273
+ range = @worksheet.Range(cell_name(index))
274
+ range.Value = value.to_s
275
+ range.Style = style
276
+ range.Columns.AutoFit if auto_fit
277
+ end
278
+
279
+ # 设置单元格风格
280
+ def set_style(letter, style = "NormalStyle")
281
+ range = @worksheet.range(cell_name!(letter))
282
+ range.Style = style
283
+ end
284
+
285
+ # 合并一行中的单元格
286
+ def merge(idx_begin, idx_end)
287
+ cell_begin = "#{idx_begin}#{@row_id}"
288
+ cell_end = "#{idx_end}#{@row_id}"
289
+ range = @worksheet.Range("#{cell_begin}:#{cell_end}")
290
+ range.merge
291
+ tmp = ((idx_begin.upcase)..(idx_end.upcase)).to_a
292
+ tmp.shift
293
+ @nil_space = (@nil_space | tmp).sort
294
+ end
295
+
296
+ # 开启自动换行
297
+ def wraptext(letter)
298
+ range = @worksheet.range(cell_name!(letter))
299
+ range.WrapText = true
300
+ end
301
+
302
+ # 对此时的行的下方下分页符
303
+ def pagebreak
304
+ @worksheet.rows(@row_id+1).PageBreak = 1
305
+ end
306
+
307
+ # 返回此时的::Row类
308
+ def real_row
309
+ @worksheet.rows(@row_id)
310
+ end
311
+
312
+ alias :style :set_style
313
+ end
314
+
315
+ # 图表
316
+ class Chart
317
+
318
+ # 图形类型
319
+ ColumnClustered = 51 # 簇状柱形图
320
+ ColumnStacked = 52 # 堆积柱状图
321
+ Doughnut = -4120 # 圆环图
322
+ Line = 4 # 折线图
323
+ Pie = 5 # 饼图
324
+ BarClustered = 57 # 簇状条形图
325
+
326
+ # 标签数据
327
+ DataLabelsShowLabel = 4 # 数据点所属的分类
328
+ DataLabelsShowLabelAndPercent = 5 # 占比百分比以及所属分类,仅饼图
329
+ DataLabelsShowPercent = 3 # 百分比, 仅饼图
330
+ DataLabelsShowValue = 2 # 默认值
331
+
332
+
333
+ def initialize(active)
334
+ @chart = active
335
+ end
336
+
337
+ def chart_work
338
+ @chart
339
+ end
340
+
341
+ # 标签选项
342
+ def now_table
343
+ #@chart.SeriesCollection(n).DataLabels
344
+ num = @chart.SeriesCollection.Count
345
+ num.times do |t|
346
+ t += 1
347
+ yield @chart.SeriesCollection(t).DataLabels
348
+ end
349
+ end
350
+
351
+ # 修改标题
352
+ def title=(name)
353
+ @chart.HasTitle = true
354
+ @chart.ChartTitle.Characters.Text = name
355
+ end
356
+
357
+ # 这是原数据地址, 以每列数据作为一个系列
358
+ def source=(range)
359
+ @chart.SetSourceData(range, 2)
360
+ end
361
+
362
+ # 更改图形类型
363
+ def type=(c_type)
364
+ @chart.ChartType = c_type
365
+ end
366
+
367
+ # 设置X轴名称, 只用于条形图
368
+ def axes_x=(name)
369
+ @chart.Axes(1,1).HasTitle = true
370
+ @chart.Axes(1,1).AxisTitle.Characters.Text = name
371
+ end
372
+
373
+ # 设置Y轴名称, 只用于条形图
374
+ def axes_y=(name)
375
+ @chart.Axes(2,1).HasTitle = true
376
+ @chart.Axes(2,1).AxisTitle.Characters.Text = name
377
+ end
378
+
379
+ # 设置坐标轴格式, 刻度单位
380
+ def axes_unit(int)
381
+ @chart.Axes(2).MajorUnit = int
382
+ @chart.Axes(2).MinorUnit = int
383
+ end
384
+
385
+ # 修改样式
386
+ # 通过录制宏可以查看样式编号
387
+ # 条形图中203 比较好看
388
+ # 饼图中 251, 254 比较好看
389
+ def style=(int)
390
+ @chart.ChartStyle = int
391
+ data_label
392
+ end
393
+
394
+ # 添加饼图的百分比
395
+ def data_label(type=DataLabelsShowLabelAndPercent)
396
+
397
+ # 应用标签选项
398
+ @chart.ApplyDataLabels(type)
399
+
400
+ # 取消标签选项的系列名称
401
+ now = @chart.SeriesCollection(1).DataLabels
402
+ now.ShowSeriesName = false
403
+
404
+ # 将图例放到右边
405
+ now = @chart.Legend
406
+ now.Position = -4152
407
+ end
408
+
409
+ # 标签系列名称开关
410
+ def series_name=(bool)
411
+ now_table do |n|
412
+ n.ShowSeriesName = bool
413
+ end
414
+ end
415
+
416
+ # 标签类别名称开关
417
+ def category_name=(bool)
418
+ now_table do |n|
419
+ n.ShowCategoryName = bool
420
+ end
421
+ end
422
+
423
+ # 标签值开关
424
+ def value=(bool)
425
+ now_table do |n|
426
+ n.ShowValue = bool
427
+ n.ShowLegendKey = 0 unless bool
428
+ n.Separator = " " unless bool
429
+ end
430
+ end
431
+
432
+ end
433
+
434
+ end
435
+ end
436
+
437
+
@@ -0,0 +1,299 @@
1
+ module Aio::Base::Toolkit
2
+ module ExcelWps
3
+ class WorkBook
4
+
5
+ include Aio::Ui::Verbose
6
+
7
+ @@worksheets_name = []
8
+ def initialize(encoding = "utf-8")
9
+
10
+ if Aio::Base::Toolkit::OS.windows?
11
+ require "win32ole"
12
+ else
13
+ print_error "只有Windows系统才能使用Excel模块"
14
+ exit 0
15
+ end
16
+
17
+ @excel = WIN32OLE.new("excel.Application")
18
+ @excel.visible = false
19
+ @workbook = @excel.workbooks.add
20
+ @encoding = encoding
21
+ create_style
22
+ end
23
+
24
+ # 警告提示开关
25
+ def display_alerts=(bool)
26
+ @excel.DisplayAlerts = bool
27
+ end
28
+
29
+ def add_worksheet(name)
30
+ while @@worksheets_name.include?(name)
31
+ name += "1"
32
+ end
33
+ @@worksheets_name << name
34
+ worksheet = @workbook.worksheets.add
35
+ worksheet.activate
36
+ worksheet.name = name
37
+ return WorkSheet.new(worksheet)
38
+ end
39
+
40
+ def create_style
41
+ sty = @workbook.styles.add("NormalStyle")
42
+ self.class.normal_style(sty)
43
+
44
+ sty = @workbook.styles.add("BoldStyle")
45
+ self.class.bold_style(sty)
46
+
47
+ sty = @workbook.styles.add("TitleStyle")
48
+ self.class.title_style(sty)
49
+ end
50
+
51
+ def self.normal_style(sty)
52
+ sty.font.size = 9
53
+ sty.borders(7).linestyle = 1
54
+ sty.borders(8).linestyle = 1
55
+ sty.borders(9).linestyle = 1
56
+ sty.borders(10).linestyle = 1
57
+ sty.HorizontalAlignment = -4108
58
+ end
59
+
60
+ def self.bold_style(sty)
61
+ sty.font.size = 9
62
+ sty.font.bold = true
63
+ sty.borders(7).linestyle = 1
64
+ sty.borders(8).linestyle = 1
65
+ sty.borders(9).linestyle = 1
66
+ sty.borders(10).linestyle = 1
67
+ sty.HorizontalAlignment = -4108
68
+ end
69
+
70
+ def self.title_style(sty)
71
+ sty.font.size = 20
72
+ sty.font.bold = true
73
+ sty.borders(7).linestyle = 1
74
+ sty.borders(8).linestyle = 1
75
+ sty.borders(9).linestyle = 1
76
+ sty.borders(10).linestyle = 1
77
+ sty.HorizontalAlignment = -4108
78
+ end
79
+
80
+ def show
81
+ @excel.visible = true
82
+ end
83
+
84
+ def save(path)
85
+ @workbook.saveas(path)
86
+ end
87
+
88
+ def close
89
+ @workbook.close
90
+ @excel.quit
91
+ end
92
+ end # class WorkBook
93
+
94
+ class WorkSheet
95
+ IMAGE_ROW_NUM = 56
96
+ @@worksheets_name = []
97
+ def initialize(worksheet)
98
+ @row_count = 1
99
+ @worksheet = worksheet
100
+ @nil_space = []
101
+ end
102
+
103
+ # 增加一个空行
104
+ def add_space_line(n=1)
105
+ return if n < 1
106
+ @row_count += n
107
+ end
108
+
109
+ # 对列进行合并
110
+ def merge(range1, range2)
111
+ @worksheet.range("#{range1}:#{range2}").merge
112
+ end
113
+
114
+ # 添加标题行
115
+ def add_title(name)
116
+ add_row.add_cell(name, false, "BoldStyle")
117
+ end
118
+
119
+ # 设置列的宽度
120
+ def width(col, width)
121
+ @worksheet.Columns("#{col}:#{col}").ColumnWidth = width
122
+ end
123
+
124
+ def height(height)
125
+ @row_height = height
126
+ end
127
+
128
+ # 增加Row类
129
+ def add_row(&block)
130
+ @current_row = Row.new(@worksheet, @row_count)
131
+ @current_row.height = @row_height if @row_height
132
+ @row_count += 1
133
+ yield @current_row if block
134
+ return @current_row
135
+ end
136
+
137
+ # 返回此时的Row类
138
+ def current_row
139
+ return @current_row
140
+ end
141
+
142
+ # 返回此时的行索引
143
+ def current_row_id
144
+ return @current_row.row_id
145
+ end
146
+
147
+ # 添加图像
148
+ def add_image(image_path)
149
+ return unless File.exist?(image_path)
150
+ add_space_line
151
+ add_row
152
+ cell_name = current_row.first_cell
153
+ @worksheet.Range(cell_name).Select
154
+ @worksheet.Pictures.Insert(image_path)
155
+ add_space_line IMAGE_ROW_NUM
156
+ end
157
+
158
+ # 判断是否有垂直分页符存在
159
+ def has_pagebreak?
160
+ @worksheet.VPageBreaks.count > 0
161
+ end
162
+
163
+ # 对列修改分页符
164
+ def pagebreak(col)
165
+ @worksheet.VPageBreaks(1).Location = @worksheet.columns(col)
166
+ end
167
+
168
+ # 返回::WorkSheet
169
+ def worksheet
170
+ @worksheet
171
+ end
172
+ end
173
+
174
+ class Row
175
+ FILL_TYPE = 4
176
+
177
+ attr_reader :row_id
178
+
179
+ @@cell_map = ("A".."Z").to_a
180
+ def initialize(worksheet, row_id)
181
+ @row_id = row_id
182
+ @cell_count = 0
183
+ @worksheet = worksheet
184
+ @nil_space = []
185
+ end
186
+
187
+ # 此时的单元格
188
+ def curent_cell
189
+ return cell_name(@cell_count)
190
+ end
191
+
192
+ # 第一个单元格
193
+ def first_cell
194
+ return cell_name(0)
195
+ end
196
+
197
+ # 设置行高
198
+ def height=(height)
199
+ @worksheet.rows(@row_id).RowHeight = height
200
+ end
201
+
202
+ # 增加单元格
203
+ def add_cell(value, auto_fit = false, style = "NormalStyle")
204
+ range = @worksheet.Range(cell_name(@cell_count))
205
+ range.Value = value.to_s
206
+ range.Style = style
207
+ range.Columns.AutoFit if auto_fit
208
+ @cell_count += 1
209
+ while(@nil_space.include?(to_letter(@cell_count))) do
210
+ range = @worksheet.Range(cell_name(@cell_count))
211
+ range.Value = ""
212
+ range.Style = style
213
+ @cell_count += 1
214
+ end
215
+
216
+ end
217
+
218
+ # 通过索引变成对应的字母
219
+ def to_letter(index)
220
+ @@cell_map.at(index)
221
+ end
222
+
223
+ # 特别注意,由于Toolkit中加入了Array,String的模块
224
+ # 所以判断的时候特别注意要是
225
+ def << (arr)
226
+ case arr
227
+ when ::Array
228
+ arr.size.times do |t|
229
+ add_cell(arr[t])
230
+ end
231
+ when ::String
232
+ add_cell(arr)
233
+ end
234
+ end
235
+
236
+ # 获得单元格的名字(通过索引)
237
+ def cell_name(index)
238
+ second = index % 26
239
+ first = (index - second) / 26
240
+ if first == 0
241
+ return @@cell_map[second] + @row_id.to_s
242
+ end
243
+ first -= 1
244
+ return @@cell_map[first] + @@cell_map[second] + @row_id.to_s
245
+ end
246
+
247
+ # 获得单元格的名字(通过字母)
248
+ def cell_name!(letter)
249
+ return letter + @row_id.to_s
250
+ end
251
+
252
+ def set_cell(index, value, auto_fit = false, style = "NormalStyle")
253
+ range = @worksheet.Range(cell_name(index))
254
+ range.Value = value.to_s
255
+ range.Style = style
256
+ range.Columns.AutoFit if auto_fit
257
+ end
258
+
259
+ # 设置单元格风格
260
+ def set_style(letter, style = "NormalStyle")
261
+ range = @worksheet.range(cell_name!(letter))
262
+ range.Style = style
263
+ end
264
+
265
+ # 合并一行中的单元格
266
+ def merge(idx_begin, idx_end)
267
+ cell_begin = "#{idx_begin}#{@row_id}"
268
+ cell_end = "#{idx_end}#{@row_id}"
269
+ range = @worksheet.Range("#{cell_begin}:#{cell_end}")
270
+ range.merge
271
+ tmp = ((idx_begin.upcase)..(idx_end.upcase)).to_a
272
+ tmp.shift
273
+ @nil_space = (@nil_space | tmp).sort
274
+ end
275
+
276
+ # 开启自动换行
277
+ def wraptext(letter)
278
+ range = @worksheet.range(cell_name!(letter))
279
+ range.WrapText = true
280
+ end
281
+
282
+ # 对此时的行的下方下分页符
283
+ def pagebreak
284
+ @worksheet.rows(@row_id+1).PageBreak = 1
285
+ end
286
+
287
+ # 返回此时的::Row类
288
+ def real_row
289
+ @worksheet.rows(@row_id)
290
+ end
291
+
292
+ alias :style :set_style
293
+ end
294
+
295
+ end
296
+ end
297
+
298
+
299
+