博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ruby1.9.2之——关联Excel
阅读量:4052 次
发布时间:2019-05-25

本文共 3536 字,大约阅读时间需要 11 分钟。

这算是一个通用模块吧,excel.rb

    require 'win32ole'   // 引用gem

    module
Excel 
//模块名字
        class
WorkBook  
//类名, 工作薄
            #xlEdge  
            #xlEdgeBottom =9   
            #xlEdgeLeft  = 7   
            #xlEdgeRight = 10   
            #xlEdgeTop  = 8   
            #xlColor  
            #xlColorBlank = 1  
            #xlColorWhite =2  
            #xlColorRed = 3  
            #xlColorGreen =10  
            #xlColorBlue =5  
            #xlColorYellow =6  
            #xlColorPurple = 7 # zi se  
            #xlColorCyan =8 #qing se  
            #xlBgColorYellow =19  
            #xlBgColorCyan =20  
            #xlBgColorPurple =24  
            #xlDefaultLineStyle = 1   
            @@worksheets_name =[] 
            def
initialize(encoding="GB2312") 
//初始化,设置编码
                @excel =
WIN32OLE.new("excel.application")  
                @excel.visible = FALSE  
                @workbook =
@excel.Workbooks.Add()  
                #@style_id   = 0  
                @encoding = encoding  
                create_style  
            end  
            def add_worksheet(name) 
//添加worksheet, 如果重名,则名字后面加1
                while @@worksheets_name.include?(name)  
                    name +="1"  
                end  
                @@worksheets_name << name 
//将worksheet name存放在数组worksheets_name中。
                worksheet = @workbook.Worksheets.Add()  
                worksheet.Activate  
                worksheet.name = name  
                return WorkSheet.new(worksheet)  
            end  
            def show 
//显示表格
                @excel.visible = TRUE  
            end  
            def save(name) 
//保存表格
                @workbook.save("#{name}")
 
            end
            def saveas(name) 
//另存为表格
                @workbook.saveas("#{name}")
             
            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  
                #sty.Interior.ColorIndex = 20  
            end  
        end  
        #worksheet  
        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) 
//添加title
                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  
        #row  
        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 

excel_test.rb

    require './excel'  

    excel = Excel::WorkBook.new  
    worksheet = excel.add_worksheet("joe")  
    worksheet.add_title('title')
    row = worksheet.add_row  
    row.add_cell("myaniu")  
    row.add_cell(0)  
    row.add_cell("2011-01-01 01:01:01")  
    
    worksheet = excel.add_worksheet("Lix")  
    worksheet.add_title('first')
    row = worksheet.add_row  
    row.add_cell("shangshu")  
    row.add_cell(0)  
    row.add_cell("2011-01-01 01:01:01")
 
    excel.saveas("test_excel.xls")
        
    excel.close

PS: 其中一部分参考

转载地址:http://bgcci.baihongyu.com/

你可能感兴趣的文章
考研前夜涂笔
查看>>
英语复试自我介绍
查看>>
什么是熵?
查看>>
拼凑、摘抄-评李代平的软件工程第二版
查看>>
误传了数千年的几个名句
查看>>
韩复榘经典语录
查看>>
厅、部、局、司区分大小
查看>>
VS2005中使用C#编写MDI窗口根据子窗口个数控制菜单项的enabled属性
查看>>
北川邓家“刘汉小学”无一死亡奇迹背后的真相
查看>>
救灾,从来没有胜利
查看>>
.net 2.0中ConfigurationManager替代了原来的ConfigurationSettings
查看>>
Asp.net 2.0中使用Datawindow.net2.0
查看>>
常用命名法:骆驼命名法,匈牙利命名法和帕斯卡命名法
查看>>
Server.MapPath方法测试结果
查看>>
Asp.net 默认配置下,Session莫名丢失的原因及解决办法
查看>>
Datawindow.net中如何使用Calendar控件
查看>>
如何在Datawindow.net中实现让当前行选中,并且当前行以其他颜色显示
查看>>
Datawindow.net如何使用导航栏
查看>>
如何利用Datawindow.net提取Sequence数据
查看>>
小诗,纪念我即将到来的结婚两周年
查看>>