Class: ExcelWps::WorkBook

Inherits:
Object
  • Object
show all
Defined in:
lib/excel_wps.rb

Constant Summary collapse

@@worksheets_name =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encoding = "utf-8") ⇒ WorkBook

Returns a new instance of WorkBook.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/excel_wps.rb', line 5

def initialize(encoding = "utf-8")

    if OS.windows?
      require "win32ole"
    else
      print "只有Windows系统才能使用Excel模块"
      exit 0
    end 


	@excel = WIN32OLE.new("excel.Application")
	@excel.visible = false
	@workbook = @excel.workbooks.add
	@encoding = encoding
	create_style
end

Class Method Details

.bold_style(sty) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/excel_wps.rb', line 61

def self.bold_style(sty)
	sty.font.size = 9
	sty.font.bold = true
	sty.borders(7).linestyle = 1
	sty.borders(8).linestyle = 1
	sty.borders(9).linestyle = 1
	sty.borders(10).linestyle = 1
	sty.HorizontalAlignment = -4108
end

.normal_style(sty) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/excel_wps.rb', line 52

def self.normal_style(sty)
	sty.font.size = 9
	sty.borders(7).linestyle = 1
	sty.borders(8).linestyle = 1
	sty.borders(9).linestyle = 1
	sty.borders(10).linestyle = 1
	sty.HorizontalAlignment = -4108
end

.title_style(sty) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/excel_wps.rb', line 71

def self.title_style(sty)
	sty.font.size = 20
	sty.font.bold = true
	sty.borders(7).linestyle = 1
	sty.borders(8).linestyle = 1
	sty.borders(9).linestyle = 1
	sty.borders(10).linestyle = 1
	sty.HorizontalAlignment = -4108
end

Instance Method Details

#add_worksheet(name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/excel_wps.rb', line 27

def  add_worksheet(name)
	while @@worksheets_name.include?(name)
		name += "1"
	end
	@@worksheets_name << name
	worksheet = @workbook.worksheets.add
	worksheet.activate

	# 在同一进程中多次打开会出现name的问题, 所以干脆全部使用sheet
	# worksheet.name = name
	worksheet.name = "sheet"
	return WorkSheet.new(worksheet)
end

#closeObject



89
90
91
92
# File 'lib/excel_wps.rb', line 89

def close
	@workbook.close
	@excel.quit
end

#create_styleObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/excel_wps.rb', line 41

def create_style
	sty = @workbook.styles.add("NormalStyle")
	self.class.normal_style(sty)

	sty = @workbook.styles.add("BoldStyle")
	self.class.bold_style(sty)

	sty = @workbook.styles.add("TitleStyle")
	self.class.title_style(sty)
end

#display_alerts=(bool) ⇒ Object

警告提示开关



23
24
25
# File 'lib/excel_wps.rb', line 23

def display_alerts=(bool)
	@excel.DisplayAlerts = bool
end

#save(path) ⇒ Object



85
86
87
# File 'lib/excel_wps.rb', line 85

def save(path)
	@workbook.saveas(path)
end

#showObject



81
82
83
# File 'lib/excel_wps.rb', line 81

def show
	@excel.visible = true
end