首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Perl删除Excel工作表中的整列并在新的excel文件中写入更新的数据?

如何使用Perl删除Excel工作表中的整列并在新的excel文件中写入更新的数据?
EN

Stack Overflow用户
提问于 2016-06-04 02:44:33
回答 1查看 685关注 0票数 6

我对Perl很陌生。我有excel文件,上面写着"sample.xls“,如下所示。Sample.xls

大约有1000行这样的数据。我想解析这个文件并将它写到另一个文件中,用下面的输出格式表示"output.xls“。

output.xls我用perl编写了一个脚本,但是它并没有给出我想要的确切输出方式。而且,看起来这个脚本不是很有效率。谁能指导我如何改进我的脚本,以及我的输出,如"output.xls“中所示?

下面是剧本:

代码语言:javascript
运行
复制
#!/usr/bin/perl –w

use strict;
use warnings;
use Spreadsheet::ParseExcel;
use Spreadsheet::WriteExcel;
use Spreadsheet::WriteExcel::Chart;


# Read the input and output filenames.
my $inputfile  = "path/sample.xls";
my $outputfile = "path/output.xls";

if ( !$inputfile || !$outputfile ) {
    die( "Couldn't find file\n" );
}

my $parser      = Spreadsheet::ParseExcel->new();
my $inwb = $parser->parse( $inputfile );

if ( !defined $inwb ) {
    die "Parsing error: ", $parser->error(), ".\n";
}

my $outwb = Spreadsheet::WriteExcel->new( $outputfile );


my $inws  = $inwb->worksheet( "Sheet1" );
my $outws = $outwb->add_worksheet("Sheet1");
my $out_row       = 0;

my ( $row_min, $row_max ) = $inws->row_range();
my ( $col_min, $col_max ) = $inws->col_range();

my $format = $outwb->add_format(
center_across => 1,
bold => 1,
size => 10,
border => 4,
color => 'black',
border_color => 'black',
align => 'vcenter',
);


$outws->write(0,0, "Item Name", $format);
$outws->write(0,1, "Spec", $format);
$outws->write(0,2, "First name", $format);
$outws->write(0,3, "Middle Name", $format);
$outws->write(0,4, "Last Name", $format);
$outws->write(0,5, "Customer Number", $format);
$outws->write(0,6, "Age", $format);
$outws->write(0,7, "Units", $format);

my $col_count = 1;
#$row_min = 1;
for my $inws ( $inwb->worksheets() ) {
    my ( $row_min, $row_max ) = $inws->row_range();
    my ( $col_min, $col_max ) = $inws->col_range();

    for my $in_row ( 2 .. $row_max ) {

        for my $col (  0 .. 0 ) {


            my $cell = $inws->get_cell( $in_row, $col);

            my @fields = split /_/, $cell->value();
                next unless $cell;


            $outws->write($in_row,$col, $cell->value());
            $outws->write($in_row,$col+1, $fields[1]);
        }
    }   

    for my $in_row ( 2 .. $row_max ) {

        for my $col (  1 .. 1 ) {

            my $cell = $inws->get_cell( $in_row, $col);

            my @fields = split /_/, $cell->value();
                next unless $cell;


            #$outws->write($in_row,$col+1, $cell->value());
            $outws->write($in_row,$col+1, $fields[0]);
            $outws->write($in_row,$col+2, $fields[1]);
            $outws->write($in_row,$col+3, $fields[2]);
            $outws->write($in_row,$col+4, $fields[3]);
        }
    }   

    for my $in_row ( 2 .. $row_max ) {

        for my $col (  2 .. 2 ) {

            my $cell = $inws->get_cell( $in_row, $col);

            my @fields = split /_/, $cell->value();
                next unless $cell;


            $outws->write($in_row,6, $cell->value());
        }
    }   

    for my $in_row ( 2 .. $row_max ) {

        for my $col (  3 .. 9 ) {

            my $cell = $inws->get_cell( $in_row, $col);

            next unless $cell;


        }
    }   

    for my $in_row ( 2 .. $row_max ) {
        for my $col ( 10 .. 10 ) {

            my $cell = $inws->get_cell( $in_row, $col );

            next unless $cell;


            $outws->write($in_row,7, $cell->value());

        }
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-04 11:15:08

要对输出进行排序,首先需要收集所有的信息,然后才能将其写出来。现在,您正在行和列之间跳来跳去。

这里是一些我会做的改变,以使它排序,并使它更有效率(阅读)。

  • 在循环之外创建一个数据结构$data来存储所有信息。
  • 如果只有一个工作表,则不需要遍历工作表。只用一张纸就行了。
  • 在线上绕圈。
  • 在这个循环中,使用您必须解析单个字段的代码来解析它们。没有2..2循环。只是一堆陈述。 我的@item_fields = split /_/,$inws->get_cell( $in_row,0)连用x{};my @name_fields = split /_/,$inws->get_cell( $in_row,$col )x_ q{};
  • 将它们存储在每个项目的$data中。 push @{ $data }=[ $item_fields,. ];
  • 循环结束了。打开输出文件。
  • 用一个$data循环sort并写入输出文件。 提前执行我的$row (排序{ $a-> cmp $b-> } @{ $data }){.}
  • 好了。

我建议您阅读sort,并查看参考文献全折射,以了解更多关于引用(数据结构)的信息。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37625821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档