- 论坛徽章:
- 0
|
10可用积分
我要将Excel文件中的数据导出为CSV(TAB分隔)文本文件,以下是代码,直接运行能正确输出结果,奇怪的是用PDK打包后,输出的中文全是乱码,请高手帮忙解决。
PERL的中文乱码问题实在是太普遍了,我就碰到很多次,每次都需要用不同的,很奇怪的办法才能解决,PERL就不能搞容易些吗?- #!/usr/bin/perl
- use strict;
- use warnings;
- use Spreadsheet::ParseExcel;
- use Spreadsheet::ParseExcel::FmtUnicode;
- my $file = "test.xls";
- open FH, ">out.txt";
- foreach (read_excel($file)) {
- print FH "$_\n" ;
- }
- close FH;
- sub read_excel {
- my $file = shift;
- my $oExcel = new Spreadsheet::ParseExcel;
- my $code = "GB2312";
- my $oFmtJ = Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map =>$code);
- my $oBook = $oExcel->Parse( $file, $oFmtJ );
- my $sheet = $oBook->{Worksheet}[0];
- my ( $minRow, $maxRow ) = $sheet->row_range();
- my ( $minCol, $maxCol ) = $sheet->col_range();
- my @datas;
- for my $row (0..$maxRow) {
- my @row_data;
- foreach my $col (0 .. $maxCol){
- my $cell = $sheet->get_cell($row,$col);
- next unless $cell;
- push(@row_data,$cell->value);
- }
- push(@datas,join("\t",@row_data));
- }
- return @datas;
- }
复制代码 |
最佳答案
查看完整内容
http://bbs.chinaunix.net/viewthread.php?tid=1626493&from=favorites#看下flw的excel终极方案是不是可以终极,反正上次我的问题是终极了~
|