您的位置:首页 > 其它

Perl实现将将.txt中内容写入到excel中

2012-07-17 15:06 302 查看
在处理存于txt中的大数据时,查看非常不方便,可以考虑写入到excel中。以下是一个简单的用perl实现的写入excel的程序。
#写文件到excel
#输入:待写入到excel表中的.txt文件
#输出:excel文件,注意要用绝对路径

#####################################################
use strict;
use Win32;
use Cwd;
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');
use Encode;

my $infile1="a.txt";
my $outexcel1="E:/PerlCode/cyliu/a.xlsx";
open IN1,"$infile1"
or die "could not open the input file!";

my $Book = $Excel->Workbooks->Open("$outexcel1") or die "Could not open the $outexcel1 !!!\n";
my $Sheet = $Book->Worksheets(1);
my $row=0;
my %hangye;
my %num;
my %word;
my %word1;
while(my $aline=<IN1>)
{
$row++;
print $row."\n";
$aline=~s/\n$//;
my @array=split(/\t/,$aline);

$Sheet->Cells($row,1)->{'Value'}=$array[0];
$Sheet->Cells($row,2)->{'Value'}=$array[1];

$Sheet->Cells($row,3)->{'Value'}=$array[2];
$Sheet->Cells($row,4)->{'Value'}=$array[3];#$big.":".$name{$big};
$Sheet->Cells($row,5)->{'Value'}=$array[4];
$Sheet->Cells($row,6)->{'Value'}=$array[5];
}
$Book->Close;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: