- myaniu
- 等级:


- 性别:

- 文章: 31
- 积分: 144
- 来自: 西安

|
最后更新时间:2007-10-28 关键字: Ruby Excel
这是以前用ruby写的生成Excel的程序。
可以实现插入文字,图像。
excel.rb代码如下
ruby 代码
- require 'win32ole'
- module Excel
- class WorkBook
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- @@worksheets_name =[]
- def initialize(encoding="GB2312")
- @excel = WIN32OLE.new("excel.application")
- @excel.visible = FALSE
- @workbook = @excel.Workbooks.Add()
-
- @encoding = encoding
- create_style
- end
- def add_worksheet(name)
- while @@worksheets_name.include?(name)
- name +="1"
- end
- @@worksheets_name << name
- worksheet = @workbook.Worksheets.Add()
- worksheet.Activate
- worksheet.name = name
- return WorkSheet.new(worksheet)
- end
- def show
- @excel.visible = TRUE
- end
- def close
- @workbook.Close(0)
- @excel.Quit()
- end
- def create_style
- sty=@workbook.Styles.Add('NormalStyle')
- sty.Font.Size = 12
- sty.Borders(7).LineStyle=1
- sty.Borders(8).LineStyle=1
- sty.Borders(9).LineStyle=1
- sty.Borders(10).LineStyle=1
-
- sty=@workbook.Styles.Add('TitleStyle')
- sty.Font.Size = 16
- sty.Font.Bold =true
- sty.Font.ColorIndex =3
-
- end
- end
-
- class WorkSheet
- IMAGE_ROW_NUM = 56
- @@worksheets_name =[]
- def initialize(worksheet)
- @row_count = 1
- @worksheet = worksheet
- end
- def add_space_line(n=1)
- return if n<1
- @row_count +=n
- end
- def add_title(name)
- add_space_line
- add_row.add_cell(name,false,"TitleStyle")
- end
- def add_row()
- @current_row = Row.new(@worksheet,@row_count)
- @row_count +=1
- return @current_row
- end
- def current_row
- return @current_row
- end
- def add_image(image_path)
- if not File.exist?(image_path)
- return
- end
- add_space_line 1
- add_row
- cell_name=current_row.first_cell
- @worksheet.Range(cell_name).Select
- @worksheet.Pictures.Insert(image_path)
- add_space_line IMAGE_ROW_NUM
- end
- end
-
- class Row
- FILL_TYPE = 4
- @@cell_map =["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
- def initialize(worksheet,row_id)
- @row_id =row_id
- @cell_count=0
- @worksheet = worksheet
- end
- def curent_cell
- return cell_name(@cell_count)
- end
- def first_cell
- return cell_name(0)
- end
- def add_cell(value,auto_fit = false,style = "NormalStyle")
- range = @worksheet.Range(cell_name(@cell_count))
- range['Value'] = value.to_s;
- range['Style']=style
- range.Columns.AutoFit if auto_fit
- @cell_count +=1
- end
- def cell_name(index)
- second = index % 26
- first = (index - second) / 26
- if first == 0
- return @@cell_map[second]+@row_id.to_s
- end
- first -=1
- return @@cell_map[first]+@@cell_map[second]+@row_id.to_s
- end
- def set_cell(index,value,auto_fit = false,style = "NormalStyle")
- range=@worksheet.Range(cell_name(index))
- range['Value'] = value;
- range['Style']=style
- range.Columns.AutoFit if auto_fit
- end
- end
- end
测试程序
ruby 代码
- require 'excel'
- excel = Excel::WorkBook.new
- worksheet = excel.add_worksheet("玛雅牛")
- worksheet.add_title('标题')
- row = worksheet.add_row
- row.add_cell("myaniu")
- row.add_cell(0)
- row.add_cell("2006-01-01 01:01:01")
- worksheet.add_image("C:\\AutoTest\\out.bmp")
- row = worksheet.add_row
- row.add_cell("玛雅牛")
- row.add_cell(0)
- row.add_cell("2006-01-01 01:01:01")
- worksheet.add_image("C:\\AutoTest\\out.bmp")
- excel.show
由于当时该程序是具有针对性,没有太考虑通用性,有些地方还是要修改。
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
| 返回顶楼 |
|
|
- glassprogrammer
- 等级:


- 文章: 107
- 积分: 359

|
@excel = WIN32OLE.new("excel.application")
没什么特别的呀
|
| 返回顶楼 |
|
|
- alang
- 等级:


- 性别:

- 文章: 117
- 积分: 221
- 来自: 地球

|
glassprogrammer 写道 @excel = WIN32OLE.new("excel.application")
没什么特别的呀
所谓会者不难。很多人还不会呢。要低调。
|
| 返回顶楼 |
|
|
- grantren
- 等级: 初级会员

- 文章: 18
- 积分: 31

|
alang 写道 glassprogrammer 写道 @excel = WIN32OLE.new("excel.application")
没什么特别的呀
所谓会者不难。很多人还不会呢。要低调。
兄台所言极是,我就不会,受教了。
|
| 返回顶楼 |
|
|
- together
- 等级:


- 性别:

- 文章: 715
- 积分: 865

|
倒也不失为一种解决办法。但仅限于在WINDOWS上使用,未免受限太多。
|
| 返回顶楼 |
|
|
- xiaoyu
- 等级:


- 性别:

- 文章: 773
- 积分: 1319
- 来自: 广东

|
1 Create a HTML file. 2 Rename the HTML file to Excel's suffix.
|
| 返回顶楼 |
|
|
- 不要让我想
- 等级: 初级会员

- 性别:

- 文章: 3
- 积分: 2

|
请教怎么操作 Word 呢?我用下面的脚本: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/60004
运行后,无法执行文本替换操作,只是显示第一个匹配的字符串的选中状态。
|
| 返回顶楼 |
|
|
- libinichen
- 等级: 初级会员

- 性别:

- 文章: 3
- 积分: 0
- 来自: 广州

|
问一下,你这个是创建excel,我想打开一个已有的,怎么实现呢?
我用了这样的代码。但是打不开,正常执行,没有错误,但是就是打不开??
请!
require 'win32ole'
excel = WIN32OLE.new("excel.application")
excel.Visible = false
excel.WorkBooks.Open("d:\\test.xls")
excel.WorkSheets("sheet1").Activate
#excel.Cells(2,3).value = "张三"
#excel.Cells(3,3).value = "王二"
excel.ActiveWorkbook.Close(0)
excel.Quit()
|
| 返回顶楼 |
|
|