免费注册 查看新帖 |

Chinaunix

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

如何读取多个文件和如何操作EXCEL [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-02-13 10:36 |只看该作者 |倒序浏览
各位大虾,
    小弟有几个问题请教。

    1。我有多个文本文件需要顺次打开,只知道路径,不可预知文件名,该如何做?

    2。需要从文本文件中读入一些数据,需要匹配一些关键字,如何匹配与读取?
       比如文本是:
             .....
             hello  2345
          需要匹配的是hello,需要读取的是2345

      3。我还需要把这些数据写入EXCEL表格,可我找不到相应的资料。

    各位大虾多多指教。

论坛徽章:
0
2 [报告]
发表于 2006-02-13 10:47 |只看该作者
别人只能帮助你解决问题
而不是帮助你完成工作

下面有个例子 希望对你有帮助


  1. #!/usr/bin/env perl
  2. #
  3. # Copyright (c) 2004 Alexander Anderson. All rights reserved.
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the same terms as Perl itself.
  6. #
  7. # $Id: xls2csv.pl,v 1.7 2005/01/07 15:46:49 anderal Exp anderal $

  8. use Getopt::Std;
  9. use Spreadsheet::ParseExcel::Simple;
  10. use Fatal qw(open);

  11. use strict;

  12. our($opt_s, $opt_v);
  13. getopts('vs:');

  14. my %indexes = map { $_ => 1 } split(/,/, $opt_s);

  15. # Doing it one Excel file at a time because Spreadsheet::ParseExcel seems to
  16. # have a memory leak when dealing with lots (>12) large (>1Mb) Excel files.
  17. # (or maybe memory leak used to be in the script?)

  18. my $filename = shift or die "Usage: xls2csv.pl [-s n,...] [-v] file.xls\n";

  19. $filename =~ /(.*)\.xls$/i or die "Invalid filename: $filename";
  20. my $prefix = $1;

  21. print "Reading '$filename'...\n" if $opt_v;
  22. my $xls = Spreadsheet::ParseExcel::Simple->read($filename)
  23.     or die "Can't read $filename: $!";

  24. my $index = 1;
  25. foreach my $sheet ($xls->sheets) {
  26.     next if $opt_s && !$indexes{$index};

  27.     print "Writing '$prefix-Sheet$index.csv'...\n" if $opt_v;
  28.     open(CSV, ">$prefix-Sheet$index.csv");
  29.     while ($sheet->has_data) {
  30.         my @row = $sheet->next_row;
  31.         foreach (@row) {
  32.             if (/[,"]/) {
  33.                 s/"/""/g;
  34.                 s/^/"/;
  35.                 s/$/"/;
  36.             }
  37.         }
  38.         print CSV join(',', @row), "\n";
  39.     }
  40.     close CSV;
  41. } continue {
  42.     $index++;
  43. }

  44. __END__

  45. =head1 NAME

  46. xls2csv.pl - save MS Excel worksheets as CSV files

  47. =head1 SYNOPSIS

  48. B<xls2csv.pl> [B<-s> n,...] [B<-v>] F<spreadsheet.xls>

  49. =head1 DESCRIPTION

  50. This script saves worksheets from an MS Excel file as CSV files. It does not
  51. depend on Excel because it uses L<Spreadsheet::ParseExcel::Simple> module. The
  52. values in each output CSV file are the underlying data values from each cell in
  53. the Excel file. Thus, cell formatting from the Excel file is not preserved.

  54. The CSV files are saved into the same directory as F<spreadsheet.xls> under the
  55. names F<spreadsheet-SheetI<n>.csv>, where I<n> is the index of each worksheet.

  56. =head1 OPTIONS

  57. =over 7

  58. =item B<-s>

  59. Saves only the worksheets at the specified indexes. More than one index can be
  60. specified with a comma-separated list. The first worksheet has index 1.

  61. =item B<-v>

  62. Be verbose. Some messages indicating the progress will be printed to stdout.

  63. =back

  64. =head1 AUTHOR

  65. Alexander Anderson E<lt>a.anderson@utoronto.caE<gt>

  66. =head1 README

  67. Save MS Excel worksheets as CSV files.

  68. =head1 PREREQUISITES

  69. Spreadsheet::ParseExcel::Simple

  70. =head1 SCRIPT CATEGORIES

  71. Win32
  72. Win32/Utilities

  73. =cut


复制代码

[ 本帖最后由 Aaron.Gao 于 2006-2-13 10:48 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2006-02-13 10:48 |只看该作者


[ 本帖最后由 Aaron.Gao 于 2006-2-13 10:49 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2006-02-13 11:00 |只看该作者

多谢这位大虾指教

多谢大虾指教。
真得很不好意思提出这些问题。我是个新手,刚刚开始学习使用perl就被分派了这样一个任务,感觉无处下手。

再次感谢。

能不能给些学习的资料链接?我找到的都是讲编程格式的,真的是惭愧得很。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP