Can we update existing xlsx file store in database in Ruby On Rails

55 views Asked by At

We are using gem roo for reading file and paperclip for storing file in the database.

We want to open a same file in local and update a single cell, after that upload/replace the same file in the database using paperclip.

Is this possible to do with roo and paperclip gem?

We have checked spreadsheet gem but this gem is only supports the xls file not xlsx file

1

There are 1 answers

0
nitsas On

According to roo's documentation you cannot use it to write to an xlsx file:

Roo implements read access for all common spreadsheet types.

But you can use rubyXL instead. Here's a short example that edits a cell:

# Read an existing spreadsheet:
workbook = RubyXL::Parser.parse("path/to/Excel/file.xlsx")

# Get the first sheet:
sheet1 = workbook.worksheets[0]

# Set the content of the top-left cell (0, 0) to empty ("")
# while preserve the cell's formula if any:
sheet1[0][0].change_contents("", sheet1[0][0].formula)

# Write the modified spreadsheet to another file:
workbook.write('path/to/output/file.xlsx')

Check out more examples in rubyXL's usage documentation.