perl實現收集文本並導入excel

 perl實現收集文本並導入excel

文本文件格式如:

"Begin--------------------------------------------"
"姓名:xxx"
        Physical Address. . . . . . . . . : 00-00-7D-CB-1C-39        IP Address. . . . . . . . . . . . : 172.0.1.168"計算機name:GZZW-fldajdf"
"End--------------------------------------------"
"Begin--------------------------------------------"
"姓名:yyy"
   Physical Address. . . . . . . . . : 00-00-7D-C0-B3-92   IP Address. . . . . . . . . . . . : 172.0.3.194"計算機name:GZxx-dfa"
"End--------------------------------------------"

 

 

要將其中“姓名”、“ip"、”mac—addr“、”computer name“篩選並導入到excel中。

 

以下是perl腳本實現:

 

 

#! perl -w
use strict;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Excel';

$Win32::OLE::warn = 3;

my $excel = Win32::OLE->GetActiveObject('excel.Application') ||
Win32::OLE->new('Excel.Application','Quit');
my $book = $excel->Workbooks->Add;
my $sheet = $book->worksheets(1);

my $file = shift;
die "Usage:$0 getaddress file./n/n" if (!$file);

open(FILE,"<$file") or die "open file false:$!";

#my $strtest= "llworldtestyulintestlintest";
#my @str0=~m/(test)/g;
#print $str0[0];

my $strtemp = "";
my $str1;
my $num =  0;
my $lie = 1;
my $iptem;
my $mactem;
while(<FILE>){
             #$strtemp = <FILE>;
  #print "$strtemp";

 
          $str1.=$_;
            if(/^.*End-/){
             $num++; 
     $str1=~s//n//g;
     $str1=~s/-------------------------------------------//*/g;
     
     
     while($str1=~s/(?<=/:)/S{1,30}(?=/")//){
                         print "$& ";
                         $sheet->Cells($num,$lie)->{Value}=$&;
                         $lie++;
                        }
                        $lie=1;
     while($str1=~s//d+/./d+/./d+/./d+//){
                        
                        $iptem.=$&;
                        $iptem.=" ";
                        $sheet->Cells($num,3)->{Value}=$iptem;
                       }$iptem="";
     while($str1=~s//w/w-/w/w-/w/w-/w/w-/w/w-/w/w//){
                             $mactem.=$&;
                             $mactem.=" ";
                             $sheet->Cells($num,4)->{Value}=$mactem;
                             }$mactem="";
                             print "/n";
          
     #####print "$str1 /n";
     #print "$`/n";
             $str1="";
     #print "$&/n";
                           }
                 }

$book->SaveAs('E:/linux/perl/protest/test1.xls');
$book->Close;
#print "num=$num /n";

http://svn.perl.org/modules/

發佈了71 篇原創文章 · 獲贊 1 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章