1826333 Members
3665 Online
109692 Solutions
New Discussion

PERL .csv file new sheet

 
Anand_30
Regular Advisor

PERL .csv file new sheet

Hi,

I have written a PERL script to create a .csv file and write some data in it. Now I want to use a different file handle and create a new worksheet in the same .csv file and write data in it.

Can anyone please let me know how to accomplish the task.

Thanks,
Anand
4 REPLIES 4
H.Merijn Brand (procura
Honored Contributor

Re: PERL .csv file new sheet

You can't. CSV does not support multiple sheets.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
spex
Honored Contributor

Re: PERL .csv file new sheet

Hi Anand,

You'll need the Spreadsheet::WriteExcel perl module.

my $workbook = Spreadsheet::WriteExcel->new('filename.xls');
my $worksheet = $workbook->add_worksheet();
$worksheet->write(0, 0, "Hi Excel!");

http://search.cpan.org/dist/Spreadsheet-WriteExcel/

PCS
Hein van den Heuvel
Honored Contributor

Re: PERL .csv file new sheet



Anand>> create a new worksheet in the same .csv file

Merijn>> You can't. CSV does not support multiple sheets.

Right, but I don't like the wording.

You can do anything you darn well please in a .csv file.
It supports anything you can imagine and code up.
It could easily support a notion that a row/line/record with starts a section corresponding to a worksheet to be terminated by:

However, do not expect EXCEL to interpret the data thusly.

Microsoft EXCEL only supports a single worksheet in a csv file.
If you want more, you'll have to code to a format for which excel support worksheets.

Anand,
May we assume that you want to feed the csv file to excel? Then use the perl Spreadsheet::WriteExcel moduel as Spex indicate, or figure out how to write XML instead of CSV.
Hint: Go into EXCEL, create a simple spreadsheet with two or three worksheets, a few columns and data lines each, no formatting or formulas (to begin with) and SAVE AS ... .XML.
No open the result with NOTEPAD.

Hope this helps,
Hein.

H.Merijn Brand (procura
Honored Contributor

Re: PERL .csv file new sheet

Hein, nice(er) words, but that will violate the (very simple) csv specs.

CSV does not support multiple sheets. period.

Any other format M$Excel, OpenOffice Calc, Improv, Oleo, XML, ... does, but csv does not

For all the others there are indeed fine modules, well, maybe not for oleo, but still. And CSV is not, and does not support, (embedded) XML.

Write two files: sheet1.csv, and sheet2.csv: see: two sheets, both obeing the specs (if you've used COMMA's and not M$'s semi-colons, and correct quoting, which is easily achieved using Text::CSV_XS or Text::CSV.

BTW *REAL* CSV has \r\n on the line ending, something violated quite often too.

Enjoy, Have FUN! H.Merijn [ A heavy CSV user ]
Enjoy, Have FUN! H.Merijn