Spreadsheet是一個用來查看和編輯Excel電子表格文件的控件,它可用在類似Excel的界面上。它結合了很多我們最流行的部件,像網格控件,Ribbon組件,公式引擎,還有很多其他控件。旨在創建一款和Silverlight同類的控件,可以查看和編輯Excel文件。
在Windows上安裝ActivePerl所需要的讀取在線Excel文件一般用Win32::OLE,但對於跨平臺來說,還是選擇另外的 Spreadsheet::ParseExcel及Spreadsheet::WriteExcel最好。前者是讀Excel文件用的,後者用於寫Excel文件。
Spreadsheet::ParseExcel只能讀95-2003格式的Excel文檔,對於office 2007 Excel則要安裝Spreadsheet::XLSX。
Spreadsheet安裝
Windows下安裝
ppm> install OLE::Storage_Lite
ppm> install Spreadsheet::ParseExcel
ppm> install Spreadsheet::WriteExcel
Mac下安裝
sudo perl -MCPAN -e "install 'Spreadsheet::ParseExcel'"
Spreadsheet插入行/列
在owc提供的Spreadsheet api 中,沒有直接添加行列的方法,可以使用執行命令的方式實現
添加新行在第3行,代碼如下:
var ssConstants = Spreadsheet1.Constants;
Spreadsheet1.ActiveSheet.Row(3).Select();
Spreadsheet1.Commands(ssConstants.ssCommandInsertRows).Execute();
添加新列在第3列,代碼如下:
var ssConstants = Spreadsheet1.Constants;
Spreadsheet1.ActiveSheet.cells(2,3).Select();
Spreadsheet1.Commands(ssConstants.ssCommandInsertCols).Execute();
原文來自:http://www.6excel.com/doc/20052
Spreadsheet::WriteExcel
#usr/bin/perl -w
use strict;
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel -> new('perl.xls');
my $worksheet = $workbook -> add_worksheet('sheetname1');
$worksheet -> write("A1","Hello word!");
Format的函數庫
$contentStyle->set_size( 8 );
$contentStyle->set_bold(); #設置字體爲粗體
$contentStyle->set_align( 'center' );#設置單元格居中
$contentStyle->set_text_wrap(); #是否回車換行
$contentStyle->set_color('red'); #設置單元格前景色爲紅色
f_row = workbook.add_format(:color=>"black", :bold=>0, :italic=>false, :text_wrap=>true)
SpreadSheet是支持單元格合併的, http://rubyforge.org/forum/message.php?msg_id=64873
把要合併的單元格的格式屬性設置爲:align => :merge就行了