zoukankan      html  css  js  c++  java
  • ruby导出exl方式

    class Demo
    print "hello world"
    
    require "spreadsheet"  
    #设置表格的编码为utf-8  
    Spreadsheet.client_encoding="utf-8"  
    #创建表格对象  
    book=Spreadsheet::Workbook.new  
    
    #创建工作表  
    sheet1=book.create_worksheet :name => "test1"  
    
    #定义一个样式
    format=Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 20
    
    #修改样式
    #sheet1.row(0).set_format(0,format)  #第一行第一列单元格设置样式
    sheet1.row(0).set_format(1,format)   #第一行第二列单元格设置样式
    #sheet1.row(1).set_format(0,format)  #第一行第二列单元格设置样式
    
    #sheet1.row(1).default_format=format  #第一行所有单元格设置样式
    
    #在表格第一行设置分类
    sheet1.row(0)[0]="姓名" 
    sheet1.row(0)[1]="爱好" 
    sheet1.row(0)[2]="国家"  
    5.times do |i|  
        #sheet1.row(i).set_format(0,format)  #前5行的 第一列全部设置样式
        sheet1.row(i+1)[0]="张山#{i}"  
        sheet1.row(i+1)[1]="爱好#{i}"  
        sheet1.row(i+1)[2]="国家#{i}"  
    end  
    #在指定路径下面创建test1.xls表格,并写book对象  
    book.write "#{Dir.pwd}/../../public/test1.xls"
    
    print "#{Dir.pwd}"  #当前文件夹的路径
    
    end

    运行方式
    cmd进入文件位置 ruby demo.rb
    首先要安装插件
    Spreadsheet
    ruby install spreadsheet



    class Demoprint "hello world"
    require "spreadsheet"  #设置表格的编码为utf-8  Spreadsheet.client_encoding="utf-8"  #创建表格对象  book=Spreadsheet::Workbook.new  
    #创建工作表  sheet1=book.create_worksheet :name => "test1"  
    #定义一个样式format=Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 20
    #修改样式#sheet1.row(0).set_format(0,format)  #第一行第一列单元格设置样式sheet1.row(0).set_format(1,format)   #第一行第二列单元格设置样式#sheet1.row(1).set_format(0,format)  #第一行第二列单元格设置样式
    #sheet1.row(1).default_format=format  #第一行所有单元格设置样式
    #在表格第一行设置分类sheet1.row(0)[0]="姓名" sheet1.row(0)[1]="爱好" sheet1.row(0)[2]="国家"  5.times do |i|      #sheet1.row(i).set_format(0,format)  #前5行的 第一列全部设置样式    sheet1.row(i+1)[0]="张山#{i}"      sheet1.row(i+1)[1]="爱好#{i}"      sheet1.row(i+1)[2]="国家#{i}"  end  #在指定路径下面创建test1.xls表格,并写book对象  book.write "#{Dir.pwd}/../../public/test1.xls"
    print "#{Dir.pwd}"  #当前文件夹的路径
    end

  • 相关阅读:
    实现一个WEBIM
    拼写纠错
    UML系列图用例图
    [bzoj1670][Usaco2006 Oct]Building the Moat
    [bzoj3626][LNOI2014]LCA
    转:用JS写的一个树型结构
    一个购物车中修改商品数量的实列
    网站访问统计在Global.asax中的配置的深入讨论
    转:JavaScript中的三级联动
    转:用Sql Server存储上载图片字体
  • 原文地址:https://www.cnblogs.com/lmg-jie/p/7735855.html
Copyright © 2011-2022 走看看