自己写的一个Perl的package(原创)

来源:互联网 发布:firewall 关闭端口 编辑:程序博客网 时间:2024/06/10 12:11

package Func;

use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
use DirHandle;

#Strings Trim(Strings $str);
sub Trim{
 my $str = $_[0];

 if (!(defined $str)) {
 die "The str is empty,please check the parameter!!!/n";
 }

 for($str) {
 s/^/s+//;
 s//s+$//;
 }
 return $str;
}

#Strings CheckPath(Strings $path);
sub CheckAndTranslatePath {
 my $path = $_[0];

 if ($path eq "/n" ||$path eq "/t/n" || $path eq " /n") {
  $path = undef;
  return $path;
 }

 if (defined $path) {
  chomp $path;
  $path = &Trim($path);

  $path =~ s///////g;
  if (!($path =~ /(//)$/)) {
    $path = $path."/";
  }
  return $path;
 }
 else {
  die "The path inputed is empty, please check input!!!/n";
 }
}

#ArrayList DirFilesList(Strings $dir_path);
sub DirFilesList {
 my $dir_path = $_[0];
 my @fileslist = undef;

 if (defined $dir_path) {
  my $dh = new DirHandle($dir_path);
  @fileslist = $dh -> read();
  @fileslist = sort @fileslist;
  shift @fileslist;
  shift @fileslist;
 }
 return @fileslist;
}

#Strings ReadTxtFile(Strings $filename, Strings $filepath = undef);
sub ReadTxtFile {
 my @lines;
 my $line = undef;
 my $file_name = undef;
 my $file_path = undef;

 if (defined $_[1]) {
  ($file_name, $file_path) = @_;

  if (!(defined $file_name)) {
   die "The txt file name is empty, please check it!!!/n";
  }

  chomp $file_path;
  chomp $file_name;
  $file_path = &Trim($file_path);
  $file_name = &Trim($file_name);

  if ($file_name =~ /(.txt)$/) {
    if (open(FD, "$file_path"."/"."$file_name")) {
      while($line = ) {
         chomp $line;
         $line = &Trim($line);
         push @lines, $line;
      }
      return @lines;
   }
   else {
      die "Can not open current txt file, please check your access!!!/n";
     }
   }
   else {
     die "The file is not txt file, please check file is wtether txt file!!!/n";
   }
  }
  else {
   $file_name = $_[0];
   if (!(defined $file_name)) {
     die "The txt file name is empty, please check it!!!/n";
   }

  chomp $file_name;
    if ($file_name =~ /(.txt)$/) {
   if (open(FD, "$file_name")) {
   while($line = ) {
       chomp $line;
       $line = &Trim($line);
       push @lines, $line;
    }
    return @lines;
   }
   else {
     die "Can not open current txt file, please check your access!!!/n";
   }
  }
   else {
      die "The file is not txt file, please check file is wtether txt file!!!/n";
   }
  }
}

#undef WriteExcel(Strings $filename, Numbers $row, Numbers $col, Numbers $row_from, Numbers $col_from, Strings @text);
sub WriteExcelFile {
  my $n = 0;
  my $row_to = 1;
  my $col_to = 1;

  my ($excel_name, $row, $col, $row_from, $col_from, @text) = @_;

  if ($row_from == 0) {
    $row_from = 1;
  }
  if ($col_from == 0) {
    $col_from = 1;
 }

  if ( defined $excel_name) {
    chomp $excel_name;
    $excel_name = &Trim($excel_name);
  }
  else {
    die "The excel file name is empty,please check!!!/n";
 }

  my $excel_path = $excel_name;

 # get already active Excel application or open new
 my $excel = Win32::OLE -> GetActiveObject('Excel.Application')
   || Win32::OLE -> new('Excel.Application', 'Quit');

 # open Excel file and check it.
  my $book = $excel->Workbooks->Open($excel_path);
 if(!$book) {
   die "Error open excel file,please check the excel file!!/n";
 }

 #select th sheet 1;
 my $sheet = $book -> Worksheets(1);

 #write the message into cells.
 if ($row == @text) {
   $row_to = $row_from + $row - 1;
   foreach my $r ($row_from..$row_to) {
      $n ++;
      $sheet -> Cells($r, $col_from) -> {'Value'} = $text[$n - 1] ;
   }
 }
 elsif ($col == @text) {
    $col_to = $col_from + $col - 1;
    foreach my $c ($col_from..$col_to) {
       $n ++;
       $sheet -> Cells($row_from, $c) -> {'Value'} = $text[$n - 1];
    }
  }
 elsif (($col*$row) == @text) {
    $row_to = $row_from + $row - 1;
    $col_to = $col_from + $col - 1;
    foreach my $r ($row_from..$row_to) {
       foreach my $c ($col_from..$col_to) {
          $n ++;
          $sheet -> Cells($r, $c) -> {'Value'} = $text[$n - 1];
      }
   }
 }
 else {
   die "The writed text is more than the number of column and row!!!/n";
 }

 #auto save and close the excel file;
 $book -> Save;
 $book -> Close;

 #free the $Book and $Excel;
 undef $book;
 undef $excel;
}


1;

原创粉丝点击