Roo::spreedsheet can not open excel file - ruby-on-rails

I'm trying to open excel file with Roo::Spreadsheet But it does not work.
workbook = Roo::Spreadsheet.open(file)
My file's xls extension.
I can read it only when i open the file on my machine and i save as (.xls).
The thing i can not understand is that my original file is also .xls file.
summary :
original_file.xls
then:
workbook = Roo::Spreadsheet.open(file)
Result: does not work.
then: i open my file with excel, and save as (.xls).
workbook = Roo::Spreadsheet.open(file)
result: IT WORKS
Some help please.

You can manually pass the file extension as param, Like as follows :
extension = 'xls'
workbook = Roo::Spreadsheet.open(file, :extension => extension)
This has worked for me.

Related

web2py changes file extension of the uploaded file to txt

I am using fine-uploader.js and fine-uploader.css for uploading my files using web2py framework.
The callback in the controller is
def upload_callback():
if 'qqfile' in request.vars:
filename = request.vars.qqfile
newfilename = db.doc.filename.store(request.body, filename)
db.doc.insert(filename=newfilename,uploaded_by=auth.user.id)
return response.json({'success': 'true'})
The Model
uploadfolder = os.path.join(request.folder, 'uploads')
db.define_table('doc',
Field('name', requires = IS_NOT_EMPTY()),
Field('filename', 'upload',autodelete=True,uploadfolder=uploadfolder),
Field('uploaded_by', db.auth_user)
)
When I upload file 'test01.xls', web2py stores it in file "doc.filename.bfbf907529358f82.7830302729.txt"
I do not understand why the extension xls is being changed to txt. I have also tried uploading a jpg file. Web2py changes the extension of the uploaded file to txt. Can somebody help me.
As request.vars.qqfile is not the filename but a cgi.FieldStorage object, you cannot use it as the filename but must instead extract the filename from it:
filename = request.vars.qqfile.filename
Alternatively, you can simply pass the FieldStorage object directly to the .insert method, and web2py will automatically handle extracting the filename and saving the file:
def upload_callback():
if 'qqfile' in request.vars:
db.doc.insert(filename=request.vars.qqfile, uploaded_by=auth.user.id)
return response.json({'success': 'true'})

Serving excel sheet (.xls) to browser for download using rails & angular

I am using spreadsheet gem to generate .xls file. After writing it to a file, I am trying to send to client browser for download.
Below is the code in rails
workbook = Spreadsheet::Workbook.new
# Constructed the data
file = "/path/to/file/sheet.xls"
workbook.write file
send_file file
This file when opened contains expected data in ideal format.
Below is the code in js:
CustomRestService.custom_post("report",{report_data: angular.toJson($scope.report_data)},"download_xls",{}).then (data)->
if data
hiddenElement = document.createElement('a')
angular.element(document.body).append(hiddenElement)
hiddenElement.href = 'data:attachment/xls,' + encodeURI(data)
hiddenElement.target = '_blank'
hiddenElement.download = "report.xls"
hiddenElement.click()
hiddenElement.remove()
But the file getting downloaded in browser contains junk data. I tried multiple solutions like below:
Using send_data, instead of send_file
Generated xls data and wrote to StringIO object to directly download
Constructed Blob object in js, with type as "application/vnd.ms-excel" and trying to download it.
All attempts failed, as I am missing something. All suggestions are welcome.
filename = "/path/to/file/sheet.xls"
tempfile = Tempfile.new(filename)
workbook = Spreadsheet::Workbook.new
...
workbook.write(tempfile.path)
send_file tempfile.path, :filename => filename

Launch Excel from MVC with actual file, not download copy

I need to be able to launch Excel and open and eiut a specific file from an MVC application.
The application is for internal use only and users will have access to the folder containing the excel file.
I have looked at the File method below
var dir = location + "Filename.xlsx";
var cd = new ContentDisposition
{
FileName = "Excel Spreadsheet",
Inline = true
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(dir, "application/vnd.ms-excel");
However, this opens Excel with a copy of the file downloaded to the users's download directory. So if they make any changes, these do not update to the original.
Is what I'm trying to do possible? If so, how?
Many thanks,
Chris.

Ole::Storage::FormatError: OLE2 signature is invalid

I want to read an Excel File in my Rails Application.
This is how I open my Excel file and read it.
doc = Spreadsheet.open('./try.xls', "r")
sheet = doc.worksheet 0
sheet.each do |row|
array_rows << row.to_a
end
I have it as a rake task.When I try to Read this file it throws an error.
Ole::Storage::FormatError: OLE2 signature is invalid
What is happening? what should I do?
The .xls file must be saved in EXCEL 2003 format. So
File-->Save As
from All Formats dropdown select the Excel year 2003
This solved my problem
On Mac I had to save it as Excel 97-2004(.xls) to get it to work

FTP getbinaryfile by file extension?

I am using Net::FTP's getbinaryfile functionality to pull in a zip file with FTP. My system is not aware of the full file name, so I would simply like to search the folder for the zip file extension. Usually I would simply input the filename as *.zip. This does not seem to work.
ftp = Net::FTP.new(domain)
path = "#{Rails.root}/public/ftp/#{self.id}.zip"
ftp.getbinaryfile("*.zip", path)
I used the following code to return the zip file name in the FTP folder. Then using the same code as above, I was able to run getbinaryfile with the correct zip file name.
files = ftp.nlst("*.zip")
I use the following to get all zip files (I'm using SFTP but hopefully this will point you in the right direction)
Net::SFTP.start(domain, user, :password => 'pass') do |sftp|
sftp.dir.glob("/yourdirectory","*.zip").each do |file|
sftp.download!(file, "/local/spot")
end
end

Resources