DocxBuilder的使用
DocxBuilder的使用 .一款ruby创建word文档的工具git://github.com/bagilevi/docx_builder.git
require 'docx_builder'
plan_struct = Struct.new(:name, :areas, :goals_by_area,:objectives_by_goal)
area_struct = Struct.new(:description, :id)
goal_struct = Struct.new(:description, :id)
objective_struct = Struct.new(:description)
@plan = plan_struct.new
@plan.name = 'Business Plan for 2011'
@plan.areas =
@plan.goals_by_area = {
1 => [ goal_struct.new('Create a new app', 1), goal_struct.new('Create another app', 2)],
2 => [ goal_struct.new('Make a new recipe', 3), goal_struct.new('Open a restaurant', 4)],
}
@plan.objectives_by_goal = {
1 => [ objective_struct.new('It should be interesting'), objective_struct.new('It should be simple') ],
2 => [ objective_struct.new('It should be revolutionary'), objective_struct.new('It should be unique') ],
3 => [ objective_struct.new('Make a unique recipe'), objective_struct.new('Make a tasty recipe') ],
4 => [ objective_struct.new('Serve high quality food'), objective_struct.new('Make it cheap') ],
}
file_path = "#{File.dirname(__FILE__)}/example/plan_report_template.xml"
dir_path = "#{File.dirname(__FILE__)}/example/plan_report_template"
report = DocxBuilder.new(file_path, dir_path).build do |template|
template['head']['Plan Name'] = @plan.name
template['area'] =
@plan.areas.map do |area|
area_slice = template['area'].clone
area_slice['Area Name'] = area.description
area_slice['goal'] =
@plan.goals_by_area.map do |goal|
goal_slice = template['goal'].clone
goal_slice['Goal Name'] = goal.description
goal_slice['objective'] =
@plan.objectives_by_goal.map do |objective|
objective_slice = template['objective'].clone
objective_slice['Objective Name'] = objective.description
objective_slice
end
goal_slice
end
area_slice
end
end
open("example.docx", "w") { |file| file.write(report) }
# ... or in a Rails controller:
# response.headers['Content-disposition'] = 'attachment; filename=plan_report.docx'
# render :text => report, :content_type => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' 貌似最难的是如何调整doc文件的格式, 没视觉看, 很难想像打印出来的doc整体感觉,
页:
[1]