1

I'm trying to create some multi-line cells with the axlsx gem in Ruby. I found this issue which suggested a fix using to_xml_string - but no matter what I try I can't get those multi-line cells!

Here is my current code:

def saveAsXlsx
    Axlsx::Package.new do |p|
        p.workbook.add_worksheet(:name => "Basic Worksheet") do |sheet|
            sheet.add_row @headers
            @headers.each_with_index do |line, i|
                @headerValues[i].unshift @noteValues[i]
                sheet.add_row  @headerValues[i]
                sheet.to_xml_string
            end
        end
        p.serialize('simple.xlsx')
    end
end

If anyone can help me out here I would be most appreciative...

4

2 回答 2

1

面趾 -

我有一种感觉,如果您将代码从包中取出, :preserve xml_space 的值会在工作簿中正确初始化。

您在库中突出显示了一个隐藏的假设,该假设假设您永远不会直接从生成的包中构建。

显然我会努力支持这个用例。

同时,您将通过执行以下操作为您的处理器节省大量工作:

p = Axlsx::package.new
p.workbook.add_worksheet do |sheet|
    # - your code - styles is available via sheet 
    # !! you do _not_ need to call to_xml_string
end
p.serialize('foo')

老实说,我从来没有想过有人会用一种方法完成所有的报告处理,所以这很有启发性!

最好的,

随机的

于 2013-05-28T21:16:59.607 回答
0

万一其他人有这个问题,我to_xml_string在错误的地方使用...

这是更新的代码:

#Save to excel spreadsheet found at file
def saveAsXlsx(file)
    Axlsx::Package.new do |p|
        p.workbook.styles do |s|

            #Column header format
            black_cell = s.add_style :bg_color => "00", :fg_color => "FF", :sz => 14, :alignment => { :horizontal=> :center }
            headerFormat = []
            @headers.size.times {headerFormat << black_cell}

            p.workbook.add_worksheet(:name => "Basic Worksheet") do |sheet|
                sheet.add_row @headers, :style => headerFormat
                numEntries = @headerValues.size
                #Add the values to the spreadsheet
                0.upto(numEntries-1) do |i|
                    sheet.add_row  @headerValues[i], :height => 60, :widths=>[50, :auto, :auto, :auto, :auto]
                end
                #This is necessary to preserve newlines
                sheet.to_xml_string
            end
            p.serialize(file)
        end
    end
end
于 2013-05-26T10:06:05.873 回答