如何在perl中加载关于Excel的模块?
发布网友
发布时间:2022-04-27 04:05
我来回答
共3个回答
热心网友
时间:2022-06-25 22:25
下面是一个简单的例子,先要安装几个模块,按照提示安装吧。
#!/usr/bin/perl -w
## Program : xls.pl
## Version : 1.0
## Date : 2010.7.8
##------------------------------------------------------------------------------
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::FmtUnicode;
use Spreadsheet::WriteExcel;
#---------------------------写EXCEL-------------------------------------------------------
my $output= File::Spec->catfile('Copy.xls');
my $Map=new Unicode::Map("GB2312");
my $workbook=Spreadsheet::WriteExcel->new("$output");
my $out_worksheet=$workbook->add_worksheet("table");
$format = $workbook->add_format(align=>'center',bg_color=>'cyan',border=> 1);
#---------------------------读取EXCEL-------------------------------------------------------
$filename="input.xls";
my $oExcel = new Spreadsheet::ParseExcel;
my $code = "gb2312";
my $oFmtJ = Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map =>$code);
my $oBook = $oExcel->Parse( $filename, $oFmtJ );
my $sheet=$oBook->{Worksheet}[0];
#$sheet_name=$sheet->{Name};
my ( $minRow, $maxRow ) = $sheet->row_range();
my ( $minCol, $maxCol ) = $sheet->col_range();
my @rowdata;
my $r_num=0;
for $r(0..$maxRow){
@row_data=getSingleRow($r,$maxCol,$sheet);
#print "@row_data"."\n";
my $i=0;
foreach(@row_data){
print $_;
$out_worksheet->write_unicode($r,$i++,$Map->to_unicode($_),$format);
}
}
sub getSingleRow{
my($row,$maxCol,$sheet)=@_;
my @row_all;
foreach my $col ( 0 .. $maxCol ){
my $cell = $sheet->get_cell( $row, $col );
next unless $cell;
$data=$cell->value;
push(@row_all,$data);
}
return @row_all;
}
热心网友
时间:2022-06-25 22:26
能用 Win32::OLE 模块来检索来自 Windows Excel 文件的数据.
找到的一个例子:
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;
热心网友
时间:2022-06-25 22:26
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');
my $filename = <STDIN>;
chop $filename;
my $Book = $Excel->Workbooks->Open("$filename");
my $Sheet = $Book->Worksheets(2);
open(A,">test.txt");
my $val3 = 0;
my $val4 = 0;
my $val5 = 0;
my $val7 = 0;
my $val8 = 0;
my $val9 = 0;
my $val10 = 0;
my $val11 = 0;
my $print_flag = 1;
my $print_flag1 = 0;
my $spa = "[\\t ]+";
my $ent = "[\\n]";
my @data1 = "";
my @data2 = "";
my $dat1 = 0;
my $dat2 = 0;
foreach my $row (4..200)
{
foreach my $col (3..5,8..14)
{
...
};
};
perl怎样读入一个xlsx格式的表格文件,再满足其他要求。
一般会用到以下模块,OpenOffice: Spreadsheet::ReadSXC Microsoft Excel:Spreadsheet::ParseExcel 或 Spreadsheet::XLSX 如果你嫌麻烦,就用 Spreadsheet::Read 这个模块 具体用法参考http://search.cpan.org/~hmbrand/Spreadsheet-Read-0.46/Read.pm 参考资料:http://search.cpan.org/~hmbrand/Spreadshe...
如何用perl操作excel,求代码
use strict;use Spreadsheet::Read;use Spreadsheet::Write; # 这两个 module 你可能要自己用 ppm / cpan 下载 my $data = {};my @files = ( "1.xls" , "2.xls" ) ;读取 my $index = 1;foreach my $f ( @files ) { my $xls = ReadData ( $f ) ;my $sheet = $xls ->...
perl如何打开大数据的excel表?
拿到文件指针$workbook,分段读取文件,不要一下子全部加载到内存里.
perl如何读取excel中的日期时间
对于excel的读取,建议用Win32::OLE模块,相当强大和简便 代码如下:use Win32::OLE;use Win32::OLE::Const 'Microsoft Excel';use Cwd;use File::Find;use File::Copy;my $curdir=getcwd();get already active Excel application or open new my $Excel = Win32::OLE->GetActiveObject('Excel...
Perl问题关于my $Excel = Win32::OLE->GetActiveObject('Exc...
my $Excel = Win32::OLE->new('Excel.Application', sub{(shift)->Quit;});是用来新建一个Win32::OLE对象 而my $Excel= Win32::OLE->GetActiveObject('Excel.Application'); 是使用已经打开的Excel进行工作。如果目前你没有打开Excel,必须使用new新建一个,如果打开了就可以使用GetActiveObject了...
我要在linux下使用perl来处理excel文件
你可以用Spreadsheet::ParseExcel先解析excel,再用Spreadsheet::WriteExcel写入。或者直接使用Spreadsheet::ParseExcel::SaveParser。
Perl win32::OLE 操作excel的教程说明
Set ex = CreateObject(Excel.Application)ex = Win32::OLE->new('Excel.Application', \&OleQuit) or die "oops\n";ex.Visible = 1 ex->{Visible} = 1;ex.Workbooks.Add()ex->Workbooks->Add;Dim ovR8 ovR8 = "3 is a good number"ovR8 = Variant(VT_R8, "3 is a good number...
...模块写中文字符到excel中,显示的是乱码,如何解决?来高手指点下_百度...
decode('gb2312',$text); # 进行转码 建议用utf-8 decode('utf-8',$text);然后就不会乱码了
用perl写数据到excel中时,为什么表头有了,就是下面的内容一直为空...
创建一个新的EXCEL文件 my $workbook = Spreadsheet::WriteExcel->new('PERL生成.xls');添加一个工作表 worksheet = $workbook->add_worksheet();新建一个样式 format = $workbook->add_format(); # Add a format format->set_bold();#设置字体为粗体 format->set_color('red');#设置单元格...
perl excel ole 如何处理CP932编码的excel
(如无意外) Excel 就是 UTF-8, 不用管里面的内容是甚麼 是 Perl 不是 PERL, Perl 是语言的名称, perl 是解释器 主要问题是如果你光是用 Win32::OLE, 那预设的编码就是你系统的编码, 例如 GB2312, 要解决你的问题, 可以试试在开头改成:use Win32::OLE 'CP_UTF8';$Win32::OLE::CP ...