【问题标题】:read and write excel file in perl在perl中读取和写入excel文件
【发布时间】:2013-12-16 09:57:33
【问题描述】:

我是最近的 Perl 用户。我需要一些关于在 perl 中读写文件的帮助。

让我解释一下场景:例如,我有一个 input.xls 文件,其中包含字符串、数字、下拉列表。 工作表中的一些单元格已锁定。

我想从 input.xls 文件中读取数据并将其写入一个名为 output.xls 的新文件中。

我在这里面临的问题是我无法保留正在读取的文件的格式。

即生成的输出文件不显示下拉菜单,并且在 input.xls 文件中锁定的单元格不会出现在 output.xls 文件中。

其次,即使输入文件的格式在输出文件中也会受到干扰。例如,如果单元格在输入文件中合并,则格式在输出文件中显示不一样。请指导。

这是我的代码供您参考:

#!/usr/bin/perl -w

use strict;
use Spreadsheet::ParseExcel;
use Spreadsheet::WriteExcel;
use Spreadsheet::ParseExcel::SaveParser;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
print qq[Content-type:text/html \n\n];
my $cs='Book2.xls';
 # Open the template with SaveParser
my $parser   = new Spreadsheet::ParseExcel::SaveParser;
#my $formatter=Spreadsheet::ParseExcel::FmtJapan->new();
my $template = $parser->Parse('e.xls');

my $sheet    = 0;
my $row      = 0;
my $col      = 2;

# Get the format from the cell
my $format   = $template->{Worksheet}[$sheet]
                        ->{Cells}[$row][$col]
                        ->{FormatNo};



# Write data to some cells
$template->AddCell(0, $row,   $col,   1,     $format);
$template->AddCell(0, $row+1, $col, "This is a hello world eg", $format);
#$format->{Lock};
# Add a new worksheet
# $template->AddWorksheet('New Data');

# The SaveParser SaveAs() method returns a reference to a
# Spreadsheet::WriteExcel object. If you wish you can then
# use this to access any of the methods that aren't
# available from the SaveParser object. If you don't need
# to do this just use SaveAs().
#
my $workbook;

{
    # SaveAs generates a lot of harmless warnings about unset
    # Worksheet properties. You can ignore them if you wish.
    local $^W = 0;

    # Rewrite the file or save as a new file
    $workbook = $template->SaveAs('new.xls');

}

# Use Spreadsheet::WriteExcel methods

my $worksheet  = $workbook->sheets(0);
# $worksheet->protect();
#$worksheet->write('A1:B1','=1+2');
#my $locked  = $workbook->add_format();
# $locked->set_locked(1); # A non-op

#my $unlocked = $workbook->add_format();
#$locked->set_locked(0);

# Enable worksheet protection
#$worksheet->protect();

# This cell cannot be edited.
#$worksheet->write('A1:B1', '=1+2', $locked);

$worksheet->write($row+2, $col, "World2");

$workbook->close();
print qq[
<head>
<script> 

</script>
</head>
<body>

<p>The download should start shortly. If it doesn't, click
<a id="downloadLink" href="http://128.9.45.168/~mint/MINT_Portal/macro             /963/cgi/$cs"     download="$cs" target="_blank">here</a>.</p>
</body>
</html>

];

【问题讨论】:

    标签: excel perl


    【解决方案1】:

    保持单元格的格式非常困难(对于某些任意电子表格)。 Spreadsheet::ParseExcel 并没有真正理解那么多格式(大多忽略它)。

    您可能会发现对于特定的电子表格,这一切都适用,但随后您会发现格式更复杂但效果不佳的电子表格。

    您需要的是能够无损与 Excel 文档对象模型 (DOM) 交互的东西。 (即加载现有的电子表格,克隆一些行,更改数据值,删除一些其他行,保存到不同的文件)。

    我发现唯一能做到这一点的代码是Apache POI

    不幸的是,这是一个 Java API。我最终使用Inline::Java 编写了一堆嵌入到我的perl 脚本中的管道代码(Java 中)。 (也有 .NET API 可以做到这一点,但我在 Linux 上,我永远无法让 MONO+Wine 堆栈来运行大多数 Office 自动化的东西。)

    这种方法有效,但实现它的代码非常复杂。我不鼓励尝试:-)

    有什么方法可以避免在 perl 中这样做吗? (或者只是这部分?)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 2018-02-11
      • 2011-09-03
      相关资源
      最近更新 更多