自己写的一个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;
- 自己写的一个Perl的package(原创)
- 自己写的一个数据库操作类(C#)[原创]
- [原创]写自己的字
- 学习perl写的一个
- perl写的一个端口扫描
- 【总结】perl载入自己写的模块
- perl 调用自己写的pm
- 自己联系的一个小perl
- 用perl写的一个网络爬虫
- perl的package和module
- 又一个Perl的程序(原创)
- [原创]写一个仅仅压缩文件的例子
- 写一个自己的引擎
- 写一个自己的引擎
- 自己写的一个分页
- 自己写的一个ArrayList
- 自己写的一个linkedList
- 自己写的一个例子
- Linux环境下java中文绘图配置
- 了解程序员-软件工程思想
- [CodeProject C#]Read/Write Config files
- 热门开发者中文知识库(2004年8月)
- 论曹操的用人政策
- 自己写的一个Perl的package(原创)
- Http.java
- 了解程序经理
- linux发送邮件的shell脚本
- Windows 2000 Server 常见技术问题
- mail邮件函数
- 解决eclipse无法整合lomboz的一个小技巧
- ITSM:某省通信公司支撑系统服务管理案例
- Internet Explorer 问题的一般思路