溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

perl中如何讀取和寫出Excel的包,xls,和xlsx

發(fā)布時間:2022-02-24 11:41:25 來源:億速云 閱讀:788 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了perl中如何讀取和寫出Excel的包,xls,和xlsx,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

介紹兩個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

對應(yīng)還有寫出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();

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“perl中如何讀取和寫出Excel的包,xls,和xlsx”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI