您好,登錄后才能下訂單哦!
這篇文章主要介紹了perl中如何讀取和寫出Excel的包,xls,和xlsx,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
介紹兩個perl中的包,分別可以讀取Excel的文件,包括2003 的xls文件,和2007的xlsx的文件。Spreadsheet::ParseExcel; Spreadsheet::ParseXLSX;
介紹兩個perl中的兩個包,分別可以讀取Excel的文件,包括2003 的xls文件Spreadsheet::ParseExcel,和2007的xlsx的文件Spreadsheet::ParseXLSX。
use Spreadsheet::ParseExcel; #讀取Excel2003版本的excel文件 my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse('test.xls'); if ( !defined $workbook ) { die $parser->error(), ".\n"; } my $worksheet = $workbook->worksheet('Sheet1'); my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); printf "%15s %15s %15s",'first_row','second_row','third_row'."\n"; for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); next unless $cell; my $va=$cell->value(); printf "%15s",$va; } say; }
use Spreadsheet::ParseXLSX; #讀取Excel2007版本的excel文件 用法差不多 my $parser = Spreadsheet::ParseXLSX->new; my $workbook = $parser->parse("file.xlsx"); my $worksheet = $workbook->worksheet('Sheet1'); my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); printf "%15s %15s %15s",'first_row','second_row','third_row'."\n"; for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); next unless $cell; my $va=$cell->value(); printf "%15s",$va; } say; }
更多幫助:https://metacpan.org/pod/Spreadsheet::ParseExcel
對應還有寫出excel的包:Spreadsheet::WriteExcel(2003) 和Excel::Writer::XLSX (2007)版本用法是一樣的:
參考代碼:
use Spreadsheet::WriteExcel; # Create a new Excel workbook my $workbook = Spreadsheet::WriteExcel->new('perl.xls'); # Add a worksheet $worksheet = $workbook->add_worksheet(); # Add and define a format $format = $workbook->add_format(); # Add a format $format->set_bold(); $format->set_color('red'); $format->set_align('center'); # Write a formatted and unformatted string, row and column notation. $col = $row = 0; $worksheet->write($row, $col, 'Hi Excel!', $format); $worksheet->write(1, $col, 'Hi Excel!'); # Write a number and a formula using A1 notation $worksheet->write('A3', 1.2345); $worksheet->write('A4', '=SIN(PI()/4)'); |
參考代碼:
use Excel::Writer::XLSX; # Create a new Excel workbook my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' ); # Add a worksheet $worksheet = $workbook->add_worksheet(); # Add and define a format $format = $workbook->add_format(); $format->set_bold(); $format->set_color( 'red' ); $format->set_align( 'center' ); # Write a formatted and unformatted string, row and column notation. $col = $row = 0; $worksheet->write( $row, $col, 'Hi Excel!', $format ); $worksheet->write( 1, $col, 'Hi Excel!' ); # Write a number and a formula using A1 notation $worksheet->write( 'A3', 1.2345 ); $worksheet->write( 'A4', '=SIN(PI()/4)' ); $workbook->close(); |
感謝你能夠認真閱讀完這篇文章,希望小編分享的“perl中如何讀取和寫出Excel的包,xls,和xlsx”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。