1

I am using perl module WriteExcel to convert a | delimited text file into xls file, I am using below code to do so

#!/usr/bin/perl -w
use strict;
use Spreadsheet::WriteExcel;

# Create a new workbook and add a worksheet
my $workbook  = Spreadsheet::WriteExcel->new($filename);
my $worksheet = $workbook->add_worksheet("Colorful Example");

open(FH,"<$my_path/source_file.txt")
   or die "Cannot open file: $!\n";
my ($x,$y) = (0,0);
while (<FH>){
   chomp;
   my @list = split /\t/,$_;
   foreach my $c (@list){
      $worksheet->write($x, $y++, $c);
   }
   $x++; $y=0;
}
close(FH);
$workbook->close();     # Close Workbook

By this code i can convert the file into single tab excel. I am wondering how can i convert the text file into multi tab xls (excel file having multiple worksheets) file when number of rows exceed 65000.

1
  • yes by using same package. Commented Dec 28, 2014 at 9:04

1 Answer 1

1

You are adding your worksheet with this line.

my $worksheet = $workbook->add_worksheet("Colorful Example");

What you need to do is check in your loop where you process lines if you have exceeded the line limit, and if you did, replace the worksheet handle.

$worksheet = $workbook->add_worksheet('foo') if $rows > 65_000;
Sign up to request clarification or add additional context in comments.

2 Comments

in your code $rows is getting calculated from where??
That would be your $x I guess. Remember to always chose good descriptive variable names so you don't get confused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.