標籤

4GL (1) 人才發展 (10) 人物 (3) 太陽能 (4) 心理 (3) 心靈 (10) 文學 (31) 生活常識 (14) 光學 (1) 名句 (10) 即時通訊軟體 (2) 奇狐 (2) 音樂 (2) 產業 (5) 郭語錄 (3) 無聊 (3) 統計 (4) 新聞 (1) 經濟學 (1) 經營管理 (42) 解析度 (1) 遊戲 (5) 電學 (1) 網管 (10) 廣告 (1) 數學 (1) 機率 (1) 雜趣 (1) 證券 (4) 證券期貨 (1) ABAP (15) AD (1) agentflow (4) AJAX (1) Android (1) AnyChart (1) Apache (14) BASIS (4) BDL (1) C# (1) Church (1) CIE (1) CO (38) Converter (1) cron (1) CSS (23) DMS (1) DVD (1) Eclipse (1) English (1) excel (5) Exchange (4) Failover (1) FI (57) File Transfer (1) Firefox (2) FM (2) fourjs (1) gladiatus (1) google (1) Google Maps API (2) grep (1) Grub (1) HR (2) html (23) HTS (8) IE (1) IE 8 (1) IIS (1) IMAP (3) Internet Explorer (1) java (3) JavaScript (22) jQuery (6) JSON (1) K3b (1) LED (3) Linux (112) Linux Mint (4) Load Balance (1) Microsoft (2) MIS (2) MM (51) MSSQL (1) MySQL (27) Network (1) NFS (1) Office (1) Oracle (125) Outlook (3) PDF (6) Perl (59) PHP (33) PL/SQL (1) PL/SQL Developer (1) PM (3) Postfix (2) postfwd (1) PostgreSQL (1) PP (50) python (1) QM (1) Red Hat (4) Reporting Service (28) ruby (11) SAP (234) scp (1) SD (16) sed (1) Selenium-WebDriver (5) shell (5) SQL (4) SQL server (8) SQuirreL SQL Client (1) SSH (2) SWOT (3) Symantec (2) T-SQL (7) Tera Term (2) tip (1) tiptop (22) Tomcat (6) Trouble Shooting (1) Tuning (5) Ubuntu (33) ufw (1) utf-8 (1) VIM (11) Virtual Machine (2) vnc (3) Web Service (2) wget (1) Windows (19) Windows (1) WM (6) youtube (1) yum (2)

2014年9月17日 星期三

使用Perl 讀取 Excel

使用Spreadsheet::ParseExcel
[root@tiptopap ~]# perl -MCPAN -e shell
cpan> get Spreadsheet::ParseExcel
cpan> install Spreadsheet::ParseExcel
cpan> test Spreadsheet::ParseExcel


http://tc.wangchao.net.cn/bbs/detail_1476947.html

Spreadsheet::WriteExcel 和 Spreadsheet::ParseExcel

  在 2000 年,Takanori Kawai 和 John McNamara 編寫出了 Spreadsheet::WriteExcel 和 Spreadsheet::ParseExcel 模塊並將它們張貼在 CPAN 上,這兩個模塊使得在任何平台上從 Excel 文件抽取數據成爲可能(盡管不容易)。
  正如我們在稍後將看到的,如果您正在使用 Windows,Win32::OLE 仍提供一個更簡單、更可靠的解決方案,並且 Spreadsheet::WriteExcel 模塊建議使用 Win32::OLE 來進行更強大的數據和工作表操縱。Win32::OLE 帶有 ActiveState Perl 工具箱,可以用來通過 OLE 驅動許多其它 Windows 應用程序。請注意,要使用此模塊,您仍需要在機器上安裝和注冊一個 Excel 引擎(通常隨 Excel 本身安裝)。
  需要解析 Excel 數據的應用程序數以千計,但是這裏有幾個示例:將 Excel 導出到 CSV、與存儲在共享驅動器上的電子表格交互、將金融數據移至數據庫以便形成報告以及在不提供任何其他格式的情況下分析數據。
   要演示這裏給出的示例,必須在您的系統上安裝 Perl 5.6.0。您的系統最好是最近(2000 年或以後)的主流 UNIX 安裝(Linux、Solaris 和 BSD)。雖然這些示例在以前版本的 Perl 和 UNXI 以及其他操作系統中也可以使用,但是您應該考慮到您將面對那些它們無法作爲練習發揮作用的情況。
  Windows 示例:解析
  本節僅適用于 Windows 機器。所有其它各節適用于 Linux。
   在進行之前,請安裝 ActiveState Perl(這裏使用版本 628)或 ActiveState Komodo IDE 以編輯和調試 Perl。Komodo 爲家庭用戶提供一個免費許可證,您大概在幾分鍾之內就可以得到它。(有關下載站點,請參閱本文後面的參考資料。)
  使 用 ActiveState PPM 軟件包管理器安裝 Spreadsheet::ParseExcel 和 Spreadsheet::WriteExcel 模塊是困難的。PPM 沒有曆史記錄,難以設置選項,幫助會滾出屏幕並且缺省方式是忽略相關性而安裝。您可以從命令行輸入“ppm”然後發出以下命令來調用 PPM:
  清單 1:安裝 Excel 模塊的 PPM 命令
  ppm install OLE::Storage_Lite
  ppm install Spreadsheet::ParseExcel
  ppm install Spreadsheet::WriteExcel
  在這種情況下,該模塊的安裝將失敗,因爲 IO::Scalar 還不可用,因此,您可能想放棄 PPM 問題的查找,而轉向內置的 Win32::OLE 模塊。然而,在您閱讀本文時,ActiveState 可能已經發布了該問題的修正。
  有了 ActiveState 的 Win32::OLE,您可以使用下面所列的代碼逐個單元地轉儲工作表:
  下載 win32excel.pl
  清單 2:win32excel.pl
  #!/usr/bin/perl -w
  use strict;
  use Win32::OLE qw(in with);
  use Win32::OLE::Const 'Microsoft Excel';
  $Win32::OLE::Warn = 3;
  # die on errors...
  # get already active Excel application or open new
  my $Excel = Win32::OLE-GetActiveObject('Excel.Application')
  || Win32::OLE-new('Excel.Application', 'Quit');
  # open Excel file
  my $Book = $Excel-Workbooks-Open("c:/komodo projects/test.xls");
  # You can dynamically obtain the number of worksheets, rows, and columns
  # through the Excel OLE interface.
  Excel's Visual Basic Editor has more
  # information on the Excel OLE interface.
  Here we just use the first
  # worksheet, rows 1 through 4 and columns 1 through 3.
  # select worksheet number 1 (you can also select a worksheet by name)
  my $Sheet = $Book-Worksheets(1);
  foreach my $row (1..4)
  {
  foreach my $col (1..3)
  {
  # skip empty cells
  next unless defined $Sheet-Cells($row,$col)-{'Value'};
  # print out the contents of a cell
  printf "At ($row, $col) the value is %s and the formula is %s\n",
  $Sheet-Cells($row,$col)-{'Value'},
  $Sheet-Cells($row,$col)-{'Formula'};
  }
  }
  # clean up after ourselves
  $Book-Close;
  請注意,您可以用以下方式很輕松地爲單元分配值:
  $sheet-Cells($row, $col)-{'Value'} = 1;
  Linux 示例:解析
  本節適用于 UNIX,特別適用于 Linux。沒有在 Windows 中測試它。
  很難給出一個比 Spreadsheet::ParseExcel 模塊文檔中所提供的示例更好的 Linux 解析示例,因此我將演示那個示例,然後解釋其工作原理。
  下載 parse-excel.pl
  清單 3:parse-excel.pl
  #!/usr/bin/perl -w
  use strict;
  use Spreadsheet::ParseExcel;
  my $oExcel = new Spreadsheet::ParseExcel;
  die "You must provide a filename to $0 to be parsed as an Excel file" unless @ARGV;
  my $oBook = $oExcel-Parse($ARGV[0]);
  my($iR, $iC, $oWkS, $oWkC);
  print "FILE
  :", $oBook-{File} , "\n";
  print "COUNT :", $oBook-{SheetCount} , "\n";
  print "AUTHOR:", $oBook-{Author} , "\n"
  if defined $oBook-{Author};
  for(my $iSheet=0; $iSheet {SheetCount} ; $iSheet++)
  {
  $oWkS = $oBook-{Worksheet}[$iSheet];
  print "--------- SHEET:", $oWkS-{Name}, "\n";
  for(my $iR = $oWkS-{MinRow} ;
  defined $oWkS-{MaxRow} && $iR {MaxRow} ;
  $iR++)
  {
  for(my $iC = $oWkS-{MinCol} ;
  defined $oWkS-{MaxCol} && $iC {MaxCol} ;
  $iC++)
  {
  $oWkC = $oWkS-{Cells}[$iR][$iC];
  print "( $iR , $iC ) =", $oWkC-Value, "\n" if($oWkC);
  }
  }
  }
  此示例是用 Excel 97 測試的。如果它不能工作,則試著將它轉換成 Excel 97 格式。Spreadsheet::ParseExcel 的 perldoc 頁也聲稱了 Excel 95 和 2000 兼容性。
  電子表格被解析成一個名爲 $oBook 的頂級對象。$oBook 具有輔助程序的特性,例如“File”、“SheetCount”和“Author”。 Spreadsheet::ParseExcel 的 perldoc 頁的工作簿一節中記載了這些特性。
   該工作簿包含幾個工作表:通過使用工作簿 SheetCount 特性叠代它們。每個工作表都有一個 MinRow 和 MinCol 以及相應的 MaxRow 和 MaxCol 特性,它們可以用來確定該工作簿可以訪問的範圍。Spreadsheet::ParseExcel perldoc 頁的工作表一節中記載了這些特性。
  可以通過 Cell 特性從工作表獲得單元;那就是清單 3 中獲得 $oWkC 對象的方式。Spreadsheet::ParseExcel 的 perldoc 頁的 Cell 一節中記載了 Cell 特性。根據文檔,似乎沒有一種方式能夠獲得特定單元中列出的公式。
  Linux 示例:寫入
  本節適用于 UNIX,特別適用于 Linux。沒有在 Windows 中測試它。
   Spreadsheet::WriteExcel 在 Examples 目錄中帶有許多示例腳本,通常可以在 /usr/lib/perl5/site_perl/5.6.0/Spreadsheet/WriteExcel/examples 下找到這些腳本。它可能被安裝在其它各處;如果找不到那個目錄,請與您的本地 Perl 管理員聯系。
  壞消息是 Spreadsheet::WriteExcel 無法用于寫入現有 Excel 文件。必須自己使用 Spreadsheet::ParseExcel 從現有 Excel 文件導入數據。好消息是 Spreadsheet::WriteExcel 與 Excel 5 直至 Excel 2000 兼容。
  這裏有一個程序,它演示如何從一個 Excel 文件抽取、修改(所有數字都乘以 2)數據以及將數據寫入新的 Excel 文件。只保留數據,不保留格式和任何特性。公式被丟棄。
  下載 excel-x2.pl
  清單 4:excel-x2.pl
  #!/usr/bin/perl -w
  use strict;
  use Spreadsheet::ParseExcel;
  use Spreadsheet::WriteExcel;
  use Data::Dumper;
  # cobbled together from examples for the Spreadsheet::ParseExcel and
  # Spreadsheet::WriteExcel modules
  my $sourcename = shift @ARGV;
  my $destname = shift @ARGV or die "invocation: $0 ";
  my $source_excel = new Spreadsheet::ParseExcel;
  my $source_book = $source_excel-Parse($sourcename)
  or die "Could not open source Excel file $sourcename: $!";
  my $storage_book;
  foreach my $source_sheet_number (0 .. $source_book-{SheetCount}-1)
  {
  my $source_sheet = $source_book-{Worksheet}[$source_sheet_number];
  print "--------- SHEET:", $source_sheet-{Name}, "\n";
  # sanity checking on the source file: rows and columns should be sensible
  next unless defined $source_sheet-{MaxRow};
  next unless $source_sheet-{MinRow}
  (王朝網路 wangchao.net.cn)

沒有留言:

張貼留言