免费注册 查看新帖 |

Chinaunix

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

用perl批量转换txt为xls [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-10-31 18:24 |只看该作者 |倒序浏览
目前面临以下数据结构(txt格式)
文件名:1.txt
                                       1                       2                   3
item1                             100                   200                300
item2                             300                   400                450
文件名:2.txt
                                      1                       2            
item1                             500                   600               
item2                             700                   900      
item3                             1000                 3000      
第一行是年度,但是没有对应的说明。
字段、数据之间是用空格(或者制表符tab)隔开。
相关文件共有1000多个。
要求:用perl提取相应的年度/项目/数据信息,并将每个文件生成对应的xls文件如下。
1.xls
FileName          Item                     year1                 year2              year3
    1                 item1                     100                   200                300
    1                 item2                     300                   400                450
2.xls
Filename            Item                            year1                  year2            
   2                   item1                             500                   600               
   2                   item2                             700                   900      
   2                    item3                            1000                 3000     

我是Perl的初学者,参考论坛各位的程序建议,编写程序如下:

#!/usr/bin/perl

use Spreadsheet::WriteExcel;
       
#Specify the directory containing the files that you want to read
my $direct="e:\\test";
#Specify the directory containing the files that you want to save
my $dirOut="e:\\test1";
#If Windows "\\", if Mac "/";
my $slash='\\';
#The following two steps open the directory containing the files you plan to read
#and then stores the name of each file in an array called @data.
opendir(DIR1,"$direct")||die "Can't open directory";
my @New1=readdir(DIR1);

foreach $file(@New1)
{
# this step removes the default end of line character (\n)
# so the the entire file can be read in at once.
local $/;
open (SLURP, "$direct$slash"."$file") or die "can't open $file: $!";
#read the contents into file
$file = <SLURP>;
}
#close the filehandle called SLURP
close SLURP or die "cannot close $file: $!";

if ($file){
my @data = map [split], <$file>;
splice $data[0], 5, 2, join '_', @{$data[0]}[5,6];

my $xls    = Spreadsheet::WriteExcel->new('"$dirOut$slash"."$file".xls');
my $ws     = $xls->add_worksheet();
my $format = $xls->add_format();
$format->set_align('right');

my $i = 0;
for my $e (@data) {
    my $j = 0;
    map $ws->write( $i, $j++, $_, $format ), @$e;
    $i++;
                   }
             }
运行结果显示 can't open .: Permission denied at table_test2.pl line 21
请各位高手指教!
多谢!

求职 : 软件工程师
论坛徽章:
3
程序设计版块每日发帖之星
日期:2015-10-07 06:20:00程序设计版块每日发帖之星
日期:2015-12-13 06:20:00程序设计版块每日发帖之星
日期:2016-05-05 06:20:00
2 [报告]
发表于 2013-10-31 19:43 |只看该作者
把 excel 文件关闭后,运行脚本

论坛徽章:
0
3 [报告]
发表于 2013-10-31 20:59 |只看该作者
我运行一个文件,成功。
但是用循环语句,做不了。
请教如何编写循环语句?或者我的程序哪里有问题,望各位大侠不吝指教。

论坛徽章:
1
天蝎座
日期:2014-02-28 16:08:53
4 [报告]
发表于 2013-11-05 11:20 |只看该作者
打印下dir内容,估计是..和.的问题

论坛徽章:
5
丑牛
日期:2014-01-21 08:26:26卯兔
日期:2014-03-11 06:37:43天秤座
日期:2014-03-25 08:52:52寅虎
日期:2014-04-19 11:39:48午马
日期:2014-08-06 03:56:58
5 [报告]
发表于 2013-11-05 14:40 |只看该作者
用循环语句
  1. for $file (@New1){
  2.   Do_Everything_HERE;
  3. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP