Create Excel Sheet(.xls/.xlsx)

Hi,
I need to create a excel sheet(.xls or .xlsx).
My Code:

if File.exists?(“test.xls”)
puts “File EXISTS”
-----my code----
else
File.open(fileToTest, File::WRONLY|File::CREAT|File::EXCL)
puts “-------FILE CREATED--------”
----my code----
end

It creates an excel file(test.xls), but when I try to open the file I
get an alert box as shown in the attachment.

I working on Windows 7, MS Office 2007

Thanks

On Nov 23, 2011, at 1:33 AM, Mamba B. wrote:

It creates an excel file(test.xls), but when I try to open the file I
get an alert box as shown in the attachment.

I working on Windows 7, MS Office 2007

Thanks

Attachments:
http://www.ruby-forum.com/attachment/6783/excel.png

At first glance without seeing what you’re actually writing, I would say
this is because you’re not actually writing out the binary .xls format
Excel is expecting. Your best option is to write out the XML version of
Excel files (.xlsx). You can get the complete specification for the XML
you need here:

Wayne

Are you sure you want to write your own code for handling Excel binary
format?
I suggest you try ‘spreadsheet’ gem.

require ‘spreadsheet’
book = Spreadsheet::Workbook.new
sheet = book.create_worksheet
book.write ‘test.xls’

Junegunn Choi wrote in post #1033475:

Are you sure you want to write your own code for handling Excel binary
format?
I suggest you try ‘spreadsheet’ gem.

require ‘spreadsheet’
book = Spreadsheet::Workbook.new
sheet = book.create_worksheet
book.write ‘test.xls’

Thanks Choi.

But when I use require ‘spreadsheet’ in my code I get the following
error:- Even though I have installed the spreadsheet gem.

`require’: no such file to load – spreadsheet (LoadError)
from excel.rb:1

Which version of Ruby are you using?
If it’s not 1.9.x, try loading rubygems first

require ‘rubygems’
require ‘spreadsheet’

Mamba B. wrote in post #1033268:

Hi,
I need to create a excel sheet(.xls or .xlsx).
My Code:

if File.exists?(“test.xls”)
puts “File EXISTS”
-----my code----
else
File.open(fileToTest, File::WRONLY|File::CREAT|File::EXCL)
puts “-------FILE CREATED--------”
----my code----
end

It creates an excel file(test.xls), but when I try to open the file I
get an alert box as shown in the attachment.

I working on Windows 7, MS Office 2007

Thanks

You should look at axlsx and act_as_xlsx
It supports ruby 1.8.7 - 1.9.3, is fully documented and supports much
more that spreadsheet does.

http://rubygems.org/gems/axlsx

or if you are building a rails app:
http://rubygems.org/gems/acts_as_xlsx

The examples in the README will show you how to do it.
It requires rmagick to automatically format column widths so be sure to
read this:
rmagick.rubyforge.org/install-faq.html

Admin, this guy please.

On Dec 29, 2011, at 2:47 PM, Luca (Email) wrote:

I suggest you try ‘spreadsheet’ gem.


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e SMTP
autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Capodanno al parco Oltremare Riccione: Pacchetto hotel 3 stelle in centro +
ingresso al parco.

Best regards,
Zhi-Qiang L.
[email protected]

-----Messaggio originale-----
Da: Junegunn Choi [mailto:[email protected]]
Inviato: gioved 24 novembre 2011 05:58
A: ruby-talk ML
Oggetto: Re: Create Excel Sheet(.xls/.xlsx)

Are you sure you want to write your own code for handling Excel binary
format?
I suggest you try ‘spreadsheet’ gem.

require ‘spreadsheet’
book = Spreadsheet::Workbook.new
sheet = book.create_worksheet
book.write ‘test.xls’


cheers,
junegunn choi.


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Capodanno al parco Oltremare Riccione: Pacchetto hotel 3 stelle in
centro + ingresso al parco.

-----Messaggio originale-----
Da: Zhi-Qiang L. [mailto:[email protected]]
Inviato: gioved 29 dicembre 2011 09:06
A: ruby-talk ML
Oggetto: Re: Create Excel Sheet(.xls/.xlsx)


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Capodanno a Riccione, Pacchetto Relax: Mezza Pensione + bagno turco +
solarium + massaggio. Wifi e parcheggio gratis. 2 giorni euro 199 a
persona
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid978&d)-12