我有这个脚本将xls转换为xml,我的excel文件必须是三列,xmls格式应该是这样的<testcase name="Test1"> but it prints <testcase name>test-1</testcase name>
每个脚本都是这样的:
my $file = 'myfile.xls';
my @columns = ('testcase name', 'summary', 'preconditions');
my $xls = XML::Excel->new({column_headings => \@columns});
print $file;
$xls->parse_doc($file);
$xls->declare_xml({version => '1.0',
encoding => 'UTF-8', standalone => 'yes'});
$xls->print_xml('test.xml',
{
file_tag => 'testcases',
parent_tag => "testcase"
}
);
提前感谢..
答案 0 :(得分:1)
我不认为XML :: Excel可以做你需要的,它是一个简单的模块,也很老(2001)。这是使用Spreadsheet::ParseExcel(XML :: Excel使用)和XML::Writer的方法。如果您需要写入文件,可以更改“输出”选项 - 请参阅docs。
use strict;
use warnings;
use Spreadsheet::ParseExcel;
use XML::Writer;
my @columns = ('name', 'summary', 'preconditions');
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('myfile.xls') or die $parser->error();
my $worksheet = $workbook->worksheet(0);
my ( $row_min, $row_max ) = $worksheet->row_range();
my @data;
for my $row ( $row_min .. $row_max ) {
my %hash;
for my $col (0 .. $#columns) {
my $cell = $worksheet->get_cell( $row, $col );
$hash{$columns[$col]} = $cell->value();
}
push(@data,\%hash)
}
my $writer = XML::Writer->new(OUTPUT => 'self', DATA_MODE => 1);
$writer->xmlDecl();
$writer->startTag('testcases');
for my $row (@data) {
$writer->startTag('testcase', name => $row->{'name'});
$writer->dataElement('summary', $row->{'summary'});
$writer->dataElement('preconditions', $row->{'preconditions'});
$writer->endTag('testcase');
}
$writer->endTag('testcases');
$writer->end;
print $writer->to_string;
示例输出:
<?xml version="1.0"?>
<testcases>
<testcase name="test1">
<summary>summary1</summary>
<preconditions>preconditions1</preconditions>
</testcase>
<testcase name="test2">
<summary>summary2</summary>
<preconditions>preconditions2</preconditions>
</testcase>
</testcases>