如何使用Alxsx:NumFmt类?
我需要使用这个类来格式化货币值(正值和负值)
我在这里找到了这个类的一些文档-- https://www.rubydoc.info/github/randym/axlsx/Axlsx/NumFmt
但是没有可用的代码样本。请帮助我如何使用这个类。
当前代码:
currency_format_code = '$#,##0_;[Red]($#,##0_)'
@default_style = wb.styles.add_style(:format_code => currency_format_code,
:sz => 10, :font_name => "Arial",
:alignment => {:horizontal => :right})
@italics_style = wb.styles.add_style(:format_code => currency_format_code,
:sz => 10, :font_name => "Arial Narrow", :i => true,
:alignment => {:horizontal => :right})
row_style = [nil, nil, nil, @default_left_style] + ([@default_style] * 25)
sheet.add_row[nil, nil, nil, day1, day2, day3,
day4, day5, day6, day7, day8, day9,
day10, day11, day12, day13, day14,
day15, day16, day17, day18, day19,
day20, day21, day22, day23, day24,
day25], :style => row_style发布于 2019-05-06 21:14:34
我不确定你能不能让它比你已经拥有的性能更好。此问题是样式分别应用于行中的每个单元格array_to_cells
摘录:
def array_to_cells(values, options={})
DataTypeValidator.validate :array_to_cells, Array, values
types, style, formula_values = options.delete(:types), options.delete(:style), options.delete(:formula_values)
values.each_with_index do |value, index|
options[:style] = style.is_a?(Array) ? style[index] : style if style
options[:type] = types.is_a?(Array) ? types[index] : types if types
options[:formula_value] = formula_values[index] if formula_values.is_a?(Array)
self[index] = Cell.new(self, value, options)
end
end如您所见,它遍历行中的所有28个单元格,查找应该应用于每个单元格的样式,然后使用该样式创建一个新的Cell。这可能就是退化的原因。
发布于 2020-10-06 05:25:21
迟到:要使用NumFmt号码,请不使用format_code,而是使用num_fmt选项将它们添加到您的样式中:
number_style = p.workbook.styles.add_style({
num_fmt: 2,
alignment: { horizontal: :center, :vertical => :center},
border: { color: "000000", style: :thin},
bg_color: "F2F2F2"
})https://stackoverflow.com/questions/55966849
复制相似问题