免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2072 | 回复: 1
打印 上一主题 下一主题

DocxBuilder的使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-15 14:31 |只看该作者 |倒序浏览
DocxBuilder的使用
  1. .一款ruby创建word文档的工具

  2. git://github.com/bagilevi/docx_builder.git

  3. require 'docx_builder'


  4. plan_struct = Struct.new(:name, :areas, :goals_by_area,:objectives_by_goal)
  5. area_struct = Struct.new(:description, :id)
  6. goal_struct = Struct.new(:description, :id)
  7. objective_struct = Struct.new(:description)
  8. @plan = plan_struct.new
  9. @plan.name = 'Business Plan for 2011'
  10. @plan.areas = [area_struct.new('Software Development', 1), area_struct.new('Cooking', 2)]
  11. @plan.goals_by_area = {
  12.         1 => [ goal_struct.new('Create a new app', 1), goal_struct.new('Create another app', 2)],
  13.         2 => [ goal_struct.new('Make a new recipe', 3), goal_struct.new('Open a restaurant', 4)],
  14. }
  15. @plan.objectives_by_goal = {
  16.         1 => [ objective_struct.new('It should be interesting'), objective_struct.new('It should be simple') ],
  17.         2 => [ objective_struct.new('It should be revolutionary'), objective_struct.new('It should be unique') ],
  18.         3 => [ objective_struct.new('Make a unique recipe'), objective_struct.new('Make a tasty recipe') ],
  19.         4 => [ objective_struct.new('Serve high quality food'), objective_struct.new('Make it cheap') ],
  20. }


  21. file_path = "#{File.dirname(__FILE__)}/example/plan_report_template.xml"
  22. dir_path = "#{File.dirname(__FILE__)}/example/plan_report_template"

  23. report = DocxBuilder.new(file_path, dir_path).build do |template|

  24.   template['head']['Plan Name'] = @plan.name
  25.   template['area'] =
  26.     @plan.areas.map do |area|

  27.       area_slice = template['area'].clone
  28.       area_slice['Area Name'] = area.description
  29.       area_slice['goal'] =
  30.         @plan.goals_by_area[area.id].map do |goal|

  31.           goal_slice = template['goal'].clone
  32.           goal_slice['Goal Name'] = goal.description
  33.           goal_slice['objective'] =
  34.             @plan.objectives_by_goal[goal.id].map do |objective|
  35.               objective_slice = template['objective'].clone
  36.               objective_slice['Objective Name'] = objective.description
  37.               objective_slice
  38.             end
  39.           goal_slice
  40.         end
  41.       area_slice
  42.     end
  43. end


  44. open("example.docx", "w") { |file| file.write(report) }

  45. # ... or in a Rails controller:
  46. # response.headers['Content-disposition'] = 'attachment; filename=plan_report.docx'
  47. # render :text => report, :content_type => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-11-15 15:28 |只看该作者
貌似最难的是如何调整doc文件的格式, 没视觉看, 很难想像打印出来的doc整体感觉,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP