免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7304 | 回复: 3
打印 上一主题 下一主题

从Excel导出中文数据乱码的奇怪问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-23 15:34 |显示全部楼层 |倒序浏览
10可用积分
我要将Excel文件中的数据导出为CSV(TAB分隔)文本文件,以下是代码,直接运行能正确输出结果,奇怪的是用PDK打包后,输出的中文全是乱码,请高手帮忙解决。

PERL的中文乱码问题实在是太普遍了,我就碰到很多次,每次都需要用不同的,很奇怪的办法才能解决,PERL就不能搞容易些吗?
  1. #!/usr/bin/perl

  2. use strict;
  3. use warnings;
  4. use Spreadsheet::ParseExcel;
  5. use Spreadsheet::ParseExcel::FmtUnicode;

  6. my $file = "test.xls";
  7. open FH, ">out.txt";
  8. foreach (read_excel($file)) {
  9.         print FH "$_\n" ;
  10. }
  11. close FH;

  12. sub read_excel {
  13.         my $file = shift;
  14.         my $oExcel = new Spreadsheet::ParseExcel;
  15.         my $code = "GB2312";
  16.         my $oFmtJ = Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map =>$code);
  17.         my $oBook = $oExcel->Parse( $file, $oFmtJ );
  18.         my $sheet = $oBook->{Worksheet}[0];
  19.         my ( $minRow, $maxRow ) = $sheet->row_range();
  20.         my ( $minCol, $maxCol ) = $sheet->col_range();
  21.         my @datas;
  22.         for my $row (0..$maxRow) {
  23.                  my @row_data;
  24.                  foreach my $col (0 .. $maxCol){
  25.                           my $cell = $sheet->get_cell($row,$col);
  26.                           next unless $cell;
  27.                           push(@row_data,$cell->value);
  28.                  }
  29.                  push(@datas,join("\t",@row_data));
  30.         }
  31.          return @datas;
  32. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-08-23 22:49 |显示全部楼层
回复 2# yiten


    你打包后输出的中文正常吗?我试了一下,还是不行,请指教!

论坛徽章:
0
3 [报告]
发表于 2011-08-24 09:39 |显示全部楼层
回复 4# Kitaisky


    谢谢,但打包后运行依然是乱码!

论坛徽章:
0
4 [报告]
发表于 2011-08-24 10:50 |显示全部楼层
回复 6# x9x9


    非常感谢,我的问题也被终极了!同时也感谢楼上两位。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP