问题
Is there a way that I can add a new line to a cell using the Axlsx gem in Rails?
So basically replicating in Excel once you enter a value you can do a Alt + Enter to add additional text to the new line in the cell. I tried
sheet.add_row ["Testing cell row 1" + \r\n + "Testing cell row 2"]
but that throws an error.
回答1:
For a forced line feed use "\x0A" (breaks between paragraphs.)
If you want both carriage return and line feed, use "\x0D\x0A".
回答2:
I recently had the same problem and I found a solution that works.
I used this to setup:
p = Axlsx::Package.new
p.use_shared_strings = true
And this code adds a wrap
style that makes the \r
line breaks work correctly:
wrap = p.workbook.styles.add_style alignment: {wrap_text: true}
sheet.add_row "1\r2\r3", style: wrap
Now the new line in cell works, and the output is:
1
2
3
Notes:
- The new line in cell doesn't work (@Gary Pinkham)
- The
"\x0D\x0A"
didn't work (@noel)
回答3:
I couldn't comment on the "doesn't work in mac excel" comment so adding this as an answer.. use package.use_shared_strings = true.. needed for Mac Excel..
来源:https://stackoverflow.com/questions/25770025/rails-axlsx-new-line-in-cell