axlsx icon indicating copy to clipboard operation
axlsx copied to clipboard

Alxsx Gem package.to_stream fails

Open jonathanread opened this issue 7 years ago • 10 comments

The ultimate goal here is to create a stream so that I can attached the xlsx doc generated from the axlsx package object. I am able to serialize the package and it writes to the file system just fine. I don't really have the need or want to write the document the file system. The error I get when I call #to_stream is:

NoMethodError: undefined method `reopen' for "streamed":String Did you mean? prepend

What am I doing wrong here?

Axlsx::Package.new do |p|
  p.workbook do |wb|
    wb.add_worksheet(name: 'Time Cards') do |ws|
      title_style = ws.styles.add_style(sz: 24)
      ws.add_row ["Week #{week}, #{year} Time Cards", '', ''], style: [title_style], height: 30
    end
  end
  p.to_stream
end

Ruby v 2.4.0 Rails v5.0.2

jonathanread avatar May 11 '17 11:05 jonathanread

@jonathanread We are saving the report contents in a database record as a blob (Don't ask, we inherited the situation), and this is what we are doing: p.to_stream.read. That may be what you're looking for. We don't save to the file system at all.

Petercopter avatar May 22 '17 19:05 Petercopter

I have tried that also and get the same error. When I call serialize the workbook is generated correctly into the file system. I don't recall running any migrations maybe I missed a setup that also it to be written to a table? What table does it get written too?

jonathanread avatar May 22 '17 21:05 jonathanread

+1 I have the same issue on ruby 2.3.3 with the version 1.3.6

tfluehmann avatar Jun 21 '17 09:06 tfluehmann

This issue seems to be similar to https://github.com/straydogstudio/axlsx_rails/issues/18 Probably, you don't have this issue with the latest version.

sato-s avatar Jun 21 '17 14:06 sato-s

Thx, updating to the newest version fixed it for me, @sato-s the problem is the rubyzip dependency (~>1.0.0), thats why bundler didn't updated automatically

tfluehmann avatar Jun 22 '17 10:06 tfluehmann

I had the same issue and switched to the xlsxtream gem. It worked really nice for basic csv-like data in xlsx-format!

Fewer features than axlsx, but less memory usage and a simpler code-base. Perhaps it can be useful to others too.

sandstrom avatar Aug 24 '17 15:08 sandstrom

@sandstrom Hey, thanks for the suggestion! I checked it out and it's actually a lot easier to use than axlsx if you're just trying to get a basic .csv output but in .xlsx format. Much appreciated.

Another option is the spreadsheet gem. What I like about this and the xlsxtream gem is that they work very simply as Rails views.

For an example, here's how you can easily output a view to xls just by adding the .xls at the end of the URL.

controllers/tasks_controller.rb

def index
  @tasks = Task.all

  respond_to do |format|
    format.html
    format.xls { headers['Content-Disposition'] = "attachment; filename=\"tasks.xls\"" }
  end
end

views/tasks/index.xls

<%=
workbook = Spreadsheet::Workbook.new
worksheet = workbook.create_worksheet

worksheet.row(0).replace( [ "Task Name", "Due On" ] )

@tasks.each_with_index do |task, index|
  row_number = index + 1

  worksheet.row( row_number ).replace( [ task.name, task.due_on ] )
end

file = StringIO.new

workbook.write( file )

file.string.html_safe
%>

By going to /tasks.xls it will automatically download tasks.xls file. Boom.

joshuapinter avatar Mar 03 '18 02:03 joshuapinter

Thanks @joshuapinter, but I got an error: undefined method set_file_name

any solution?

khoerodin avatar Mar 15 '18 10:03 khoerodin

@khoerodin What version of Rails are you on?

joshuapinter avatar Mar 15 '18 15:03 joshuapinter

@khoerodin Nevermind, I forgot that's a helper that we have internally. That helper code is simple, though. It just does this:

headers['Content-Disposition'] = "attachment; filename=\"tasks.xls\""

I'll update my code above too.

joshuapinter avatar Mar 15 '18 15:03 joshuapinter