Axlsx - 在单元格内格式化文本 [英] Axlsx - Formatting text within a cell

查看:59
本文介绍了Axlsx - 在单元格内格式化文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到任何关于是否可以使用多个格式选项填充单个单元格的信息.

I can't seem to find any information on whether it's possible to fill a single cell with multiple formatting options.

例如,我想用文本填充单元格 A1:你好世界,这是excel"

For example, I want cell A1 to be filled with the text: "Hello world, this is excel"

这可能吗?如果可能,我使用什么语法来做到这一点?

Is this possible and if so what syntax do I use to do this?

推荐答案

对于内联样式,请使用富文本.以下是 axlsx 页面的示例:

For inline styling, use rich text. Here is an example from the axlsx page:

  p = Axlsx::Package.new
  p.use_shared_strings = true
  wb = p.workbook
  wrap_text = wb.styles.add_style({:alignment => {:horizontal => :center, :vertical => :center, :wrap_text => true}}  )
  rt = Axlsx::RichText.new
  rt.add_run('I\'m bold, ', :b => true)
  rt.add_run('I\'m italic, ', :i => true)
  rt.add_run('I\'m strike' + "\n", :strike => true)
  rt.add_run('I\'m bold, italic and strike' + "\n", :b => true, :i => true, :strike => true)
  rt.add_run('I\'m style-less :D')
  wb.add_worksheet(:name => "RichText") do | sheet |
    sheet.add_row [rt], :style => wrap_text
  end
  p.serialize 'rich_text.xlsx'

<小时>

如果您只是想对一个单元格应用多种样式,则需要差异化样式.有两种选择:


If you are just wanting to apply multiple styles to a cell, you need differential styling. There are two options:

首先,手动完成.基本上你声明你的样式类型是 :dxf.默认值为 :xf.其他一切都是一样的.来自styles.rb 的文档:

First, do it manually. Basically you state your style type is :dxf. The default is :xf. Everything else is the same. From the docs on styles.rb:

p = Axlsx::Package.new
wb = p.workbook
ws = wb.add_worksheet

# define your styles
profitable = wb.styles.add_style(:bg_color => "FFFF0000",
                           :fg_color=>"#FF000000",
                           :type => :dxf)

ws.add_row ["Genreated At:", Time.now], :styles=>[nil, date_time]
ws.add_row ["Previous Year Quarterly Profits (JPY)"], :style=>title
ws.add_row ["Quarter", "Profit", "% of Total"], :style=>title
ws.add_row ["Q1", 4000, 40], :style=>[title, currency, percent]
ws.add_row ["Q2", 3000, 30], :style=>[title, currency, percent]
ws.add_row ["Q3", 1000, 10], :style=>[title, currency, percent]
ws.add_row ["Q4", 2000, 20], :style=>[title, currency, percent]

ws.add_conditional_formatting("A1:A7", { :type => :cellIs, :operator => :greaterThan, :formula => "2000", :dxfId => profitable, :priority => 1 })
f = File.open('example_differential_styling', 'w')
p.serialize(f)

其次,使用 axlsx_styler gem.它使应用多种样式变得非常容易.

Second, use the axlsx_styler gem. It makes it very easy to apply multiple styles.

这篇关于Axlsx - 在单元格内格式化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆