Operate (generate) Excel on Linux or Unix. CPAN provides the Spreadsheet: WriteExcel and Spreadsheet: ParseExcel modules.
Let's take a look at how to use Spreadsheet: WriteExcel and Spreadsheet: ParseExcel.
First, install the corresponding modules on the server.
Install the PPM command of the Excel ModuleCopy codeThe Code is as follows: ppm> install OLE: Storage_Lite
Ppm> install Spreadsheet: ParseExcel
Ppm> install Spreadsheet: WriteExcel
Let's look at two examples.
Example 1: read an excel fileCopy codeThe Code is as follows :#! /Usr/bin/perl-w
Use strict;
Use Spreadsheet: ParseExcel;
My $ parser = Spreadsheet: ParseExcel-> new ();
My $ workbook = $ parser-> Parse('Book1.xls ');
For my $ worksheet ($ workbook-> worksheets ()){
My ($ row_min, $ row_max) = $ worksheet-> row_range ();
My ($ col_min, $ col_max) = $ worksheet-> col_range ();
For my $ row ($ row_min .. $ row_max ){
For my $ col ($ col_min... $ col_max ){
My $ cell = $ worksheet-> get_cell ($ row, $ col );
Next unless $ cell;
Print "Row, Col = ($ row, $ col) \ n ";
Print "Value =", $ cell-> value (), "\ n ";
Print "Unformatted =", $ cell-> unformatted (), "\ n ";
Print "\ n ";
}
}
}
Example 2: generate an EXCEL fileCopy codeThe Code is as follows :#! /Usr/bin/perl-w
Use Spreadsheet: WriteExcel;
# Create a new EXCEL File
My $ workbook = Spreadsheet: WriteExcel-> new('test.xls ');
# Add a worksheet
$ Worksheet = $ workbook-> add_worksheet ();
# Create a style
$ Format = $ workbook-> add_format (); # Add a format
$ Format-> set_bold (); # Set the font to bold
$ Format-> set_color ('red'); # Set the foreground color of a cell to red.
$ Format-> set_align ('center'); # Set the cell to center
# Use the row number and column number to write a formatted and last formatted string to the cell.
$ Col = $ row = 0;
$ Worksheet-> write ($ row, $ col, 'Hi Excel! ', $ Format );
$ Worksheet-> write (1, $ col, 'Hi Excel! ');
# Use the cell name (for example, A1) to write a number to the cell.
$ Worksheet-> write ('a3 ', 1.2345 );
$ Worksheet-> write ('a4 ',' = SIN (PI ()/4 )');
Exit;