当前位置: 首页 > 工具软件 > Caracal > 使用案例 >

Ruby使用Caracal库生成word文档

鲍宁
2023-12-01

快速开始

Caracal::Document.save 'example.docx' do |docx|
  # page 1
  docx.h1 'Page 1 Header'
  docx.hr
  docx.p
  docx.h2 'Section 1'
  docx.p  'Lorem ipsum dolor....'
  docx.p
  docx.table @my_data, border_size: 4 do
    cell_style rows[0], background: 'cccccc', bold: true
  end

  # page 2
  docx.page
  docx.h1 'Page 2 Header'
  docx.hr
  docx.p
  docx.h2 'Section 2'
  docx.p  'Lorem ipsum dolor....'
  docx.ul do
    li 'Item 1'
    li 'Item 2'
  end
  docx.p
  docx.img 'https://www.example.com/logo.png', width: 500, height: 300
end

需要注意的是
1、官方文档提供的docx.img的图片可能会导致404 Not Found错误
2、docx.table@my_data 必须是一个二维数组,类似于[['Header 1','Header 2'],['Cell 1', 'Cell 2']]

在Rails项目中

安装 gem 包

添加以下代码到你的项目根目录下的Gemfile

gem 'caracal'

然后执行以下命令安装依赖

bundle install

参阅

 类似资料: