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

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

···

--
Posted via http://www.ruby-forum.com/.

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: [MS-XLSX]: Excel (.xlsx) Extensions to the Office Open XML SpreadsheetML File Format | Microsoft Learn

Wayne

···

On Nov 23, 2011, at 1:33 AM, Mamba Black 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

Mamba Black 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

···

--
Posted via http://www.ruby-forum.com/\.

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.

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

···

--
Posted via http://www.ruby-forum.com/\.

-----Messaggio originale-----

···

Da: Junegunn Choi [mailto:junegunn.c@gmail.com]
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.
* Mezza pensione, Internet gratis, animazione per bimbi. Scopri l'offerta!
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid980&d)-12

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

require 'rubygems'
require 'spreadsheet'

···

--
cheers,
junegunn choi.

Admin, this guy please.

···

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

-----Messaggio originale-----
Da: Junegunn Choi [mailto:junegunn.c@gmail.com]
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.
* Mezza pensione, Internet gratis, animazione per bimbi. Scopri l'offerta!
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid980&d)-12

Best regards,
Zhi-Qiang Lei
zhiqiang.lei@gmail.com

-----Messaggio originale-----

···

Da: Zhi-Qiang Lei [mailto:zhiqiang.lei@gmail.com]
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