< Day Day Up > |
If PDF is the lingua franca of display formatting, then Microsoft Excel is the common tongue of spreadsheet data. Microsoft's ubiquitous Office suite means that nearly every office has some way of reading Microsoft Word documents and exchanging Microsoft Excel spreadsheets. Most spreadsheet programs can accept a format called Comma Separated Values (CSV). This is simply a table where the values are separated by commas. An example of a CSV table is shown below.
You could just hand this table as-is to Excel, but the results are not impressive, as shown in Figure 20.7. Figure 20.7. Your CSV table imported into Excel.What's wrong? For starters, the columns are smashed together, the movie grosses are not shown as monetary values, and there are no discernable column headers. It looks primitive. Using Perl to Create a SpreadsheetLet's make a spiffier spreadsheet with Perl. To follow this example, you'll have to install the module Spreadsheet::WriteExcel . You do not need Microsoft Excel to use the module. Your spreadsheet should
Listing 20.6 contains a program to do just this. Listing 20.6. Convert CSV to a Native Excel Spreadsheet1: #!/usr/bin/perl -w 2: 3: use strict; 4: use Spreadsheet::WriteExcel; 5: 6: my $workbook = new Spreadsheet::WriteExcel("movies.xls"); 7: my $sheet = $workbook->add_worksheet(); 8: 9: my $moneyFormat = $workbook->add_format(); 10: $moneyFormat->set_num_format('$#,##0'); 11: my $boldFormat = $workbook->add_format(); 12: $boldFormat->set_bold(); 13: 14: my $row=0; 15: my @longest=(0,0,0,0,0,0,0); 16: open(MOV, "<movies.csv") die "Can't open movies.csv: $!"; 17: while(<MOV>) { 18: chomp; 19: my @fields = split(/,/, $_); 20: for(my $col = 0; $col < @fields; $col++) { 21: if ($row == 0) { 22: $sheet->write($row, $col, $fields[$col], $boldFormat); 23: } else { 24: if ($col == 2 or $col == 3) { 25: $sheet->write($row, $col, 26: $fields[$col], $moneyFormat); 27: } else { 28: $sheet->write($row, $col, $fields[$col]); 29: } 30: } 31: 32: if ($longest[$col] < length($fields[$col])) { 33: $longest[$col] = length($fields[$col]); 34: } 35: } 36: $row++; 37: } 38: $longest[2]+=3; 39: $longest[3]+=3; 40: for(my $i = 0; $i < @longest; $i++) { 41: $sheet->set_column($i,$i,$longest[$i]); 42: } 43: 44: close(MOV); 45: $workbook->close();
The resulting pretty spreadsheet is shown in Figure 20.8. Figure 20.8. Nicely formatted Excel spreadsheet.Reading the SpreadsheetNow that you've created a nice Excel spreadsheet, how would you take changes and put them back into the original CSV file? Reading the Excel spreadsheet is even easier than writing it, especially because you really don't care about formatting. The Spreadsheet::ParseExcel module enables you to read the spreadsheet as a series of rows and columns, either with or without formatting. You do not need Microsoft Excel to use this module. Listing 20.7 shows an example to read the movies.xls file that you created with Listing 20.6 (see Figure 20.8). This example only prints the spreadsheet as CSV. If you want to write it back into the original file, feel free to make the necessary modifications. Listing 20.7. Reading an Excel Spreadsheet1: #!/usr/bin/perl -w 2: 3: use strict; 4: use Spreadsheet::ParseExcel; 5: 6: my $excelObj = new Spreadsheet::ParseExcel; 7: my $workbook = $excelObj->Parse('movies.xls'); 8: my $sheet = $workbook->{Worksheet}[0]; 9: 10: my $row = $sheet->{MinRow}; 11: while($row <= $sheet->{MaxRow}) { 12: my $col = $sheet->{MinCol}; 13: my @rowval = (); 14: while($col < $sheet->{MaxCol}) { 15: my $cell = $sheet->{Cells}[$row][$col]; 16: push(@rowval, $cell->{Val}); 17: $col++; 18: } 19: print join(',', @rowval), "\n"; 20: $row++; 21: }
| ||
< Day Day Up > |