任何人都可以帮助解析xlsx格式文件中的特定行和列,并返回另一个xlsx文件

时间:2015-03-19 08:42:06

标签: perl

我正在尝试从1st文件解析7thxlsx列,并且我想将解析后的列重写为另一个新的xlsx格式文件作为输出。

use strict;
use warnings;
use Spreadsheet::WriteExcel;
use Spreadsheet::Read;
use constant FILE_NAME => 'file1.xls';
my $workbook = ReadData ("Next seq_Run039_ Data Transfer     sheet_22.1.2015.xlsx");

$workbook = Spreadsheet::WriteExcel->new(FILE_NAME)
|| die "could not create $!";
$workbook->compatibility_mode();

my $worksheet = $workbook->add_worksheet();
#my $parser = $worksheet->"Next seq_Run039_ Data Transfer sheet_22.1.2015.xlsx";
$worksheet->write_col(0, 0,[[qw(Header)],[ qw(File_Version), 1 ],[], [qw(Data)], [qw(SampleID SampleName Index Index2)]]);
$worksheet->write_col( 5, 0,  [1 .. 100] );


$workbook->close() or die "Error closing file: $!";

我的问题是如何解析数据并将其打印到另一个文件中?

1 个答案:

答案 0 :(得分:0)

我不确定您对这些列的处理方式以及您希望放置它的位置,但此代码可以为您提供线索。

use strict;
use warnings;
use Spreadsheet::Read;
use Excel::Writer::XLSX;
use List::Util qw(max);

use constant FILE_IN   => 'Next seq_Run039_ Data Transfer     sheet_22.1.2015.xlsx';
use constant FILE_OUT  => 'file.xlsx';
use constant SHEET     => 1;
use constant COLS      => ( 1, 7 );
use constant SKIP_ROWS => 3;
use constant COL_SPACE => 0;

my $book = ReadData(FILE_IN);
my @cols = @{ $book->[SHEET]{cell} }[COLS];

# undef $book; # you can release rest of $book

# there is filler as 0. row so +1
# note splice is destructive to $book->[SHEET]{cell} }[COLS] content but not to FILE_IN
splice @$_, 0, SKIP_ROWS + 1 for @cols;
my $max_row = max map scalar @$_, @cols;

# replace any space in @cols content by single '_'
map s/\s+/_/g, @$_ for @cols;

# Create new Spreadsheet

my $workbook  = Excel::Writer::XLSX->new(FILE_OUT);
my $worksheet = $workbook->add_worksheet();

# add header
my @header = (
    [qw(Header)], [ qw(File_Version), 1 ],
    [], [qw(Data)], [qw(SampleID SampleName Index Index2)]
);
$worksheet->write_col( 0, 0, \@header );

# insert columns from 3rd column
$worksheet->write( scalar @header,
    0, [ [ 1 .. $max_row ], ( [] ) x COL_SPACE, @cols ] );

$workbook->close();