Can the ruby spreadsheet gem wrap text?

我是研究僧i 提交于 2019-12-06 08:25:28

问题


Is there a way to use the ruby spreadsheet gem to produce a spreadsheet with a cell whose text is wrapped? (If not, is there some other way to do it?)

Thanks, --Paul

p.s. In response to two good suggestions that unfortunately won't work for me, I should note: 1) I cannot convert my ruby application to JRuby at this time. 2) I am developing on Linux, not Windows.


回答1:


Please try this for Ruby spreadsheet gem:

 fmt = Spreadsheet::Format.new :text_wrap => true
 sheet.row(y).set_format(x, fmt)

form here:

http://rubyforge.org/forum/forum.php?set=custom&forum_id=2920&style=nested&max_rows=50&submit=Change+View




回答2:


I would agree with Phrogz in that you should use Win32OLE instead of the spreadsheet gem, if that is possible. This is how you would wrap a cell's text using Win32OLE:

worksheet.Range("A1").WrapText = true     where worksheet references a specific worksheet.

Here is a more complete example:

xl = WIN32OLE.new('Excel.Application')      # => opens Excel
wb = xl.Workbooks.Add();                    # => adds a workbook
worksheet = wb.Worksheets(3)                # => 3rd sheet (Excel starts at 1)
worksheet.Range("A1").value = "Hello, how do you do?"
worksheet.Range("A1").WrapText = true       # => wraps the text    

I hope this helps. I have used Win32OLE to work with Excel and like it.

P.S. Unless I missed it somewhere, it doesn't appear as if you can wrap text using the spreadsheet gem.


EDIT: Since question has added a Linux requirement, I'm adding this as a Linux answer.

Here is some code that I believe will work with POI Ruby Bindings:

h = Poi4r::HSSFWorkbook.new
s = h.createSheet("Sheet1")
r = s.createRow(0)
c = r.createCell(0)

t = h.createCellStyle()
t.setWrapText(true)

I have not tested this. It has been adapted from the POI Ruby page and quickguide for POI spreadsheet.




回答3:


If JRuby is an option, then you can use the excellent POI library, which can do just about anything Excel can in its file format. It can indeed set cell wrapping option.

The following blog post, which I've had bookmarked for years, shows how you can make a Java API much more Ruby-like. It uses POI as an example. http://mysterycoder.blogspot.com/2007/04/api-unickifying-with-jruby.html

It looks like the jruby-poi project has taken these ideas and made them a gem. https://github.com/kameeoze/jruby-poi

Edit: no need for JRuby. There are bindings for MRI: http://poi.apache.org/poi-ruby.html




回答4:


Instead of using the spreadsheet gem--which generates a spreadsheet from scratch with limited capabilities--I suggest using Win32OLE to script Excel to modify an existing file. This is only suitable if you are on Windows and have a copy of Excel installed that can be opened by Ruby, and hence is usually unsuitable for a server environment.



来源:https://stackoverflow.com/questions/4599555/can-the-ruby-spreadsheet-gem-wrap-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!