简体   繁体   English

用perl读写excel文件

[英]read and write excel file in perl

I am a recent Perl user.我是最近的 Perl 用户。 I need some help regarding the reading and writing of files in perl.我需要一些有关在 perl 中读取和写入文件的帮助。

Let me explain the scenario: For example, I have a input.xls file which contains string,numeric,drop down list.让我解释一下这个场景:例如,我有一个 input.xls 文件,其中包含字符串、数字和下拉列表。 Some cells in the sheets are locked.工作表中的某些单元格被锁定。

I want to read the data from input.xls file and want to write it in a new file file called output.xls.我想从 input.xls 文件中读取数据,并将其写入一个名为 output.xls 的新文件中。

The problem I am facing here is I am unable to retain the formatting of the file from which I am reading.我在这里面临的问题是我无法保留我正在阅读的文件的格式。

Ie the output file which is generated does not display the drop down as well as the the cells which are locked in input.xls file does not appear in the output.xls file.即生成的输出文件不显示下拉列表,并且在 input.xls 文件中锁定的单元格没有出现在 output.xls 文件中。

Secondly even the formatting of the input file is disturbed in the output file.其次,即使是输入文件的格式也会在输出文件中受到干扰。 For eg if cells are merged in input file then the formatting does not appear the same in output file.例如,如果单元格在输入文件中合并,则格式在输出文件中不会显示相同。 Kindly guide.请指导。

Here is my code for your reference:这是我的代码供您参考:

#!/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>

];

It will be extremely hard to keep the formatting of the cells (for some arbitrary spreadsheet).保持单元格的格式(对于某些任意电子表格)将非常困难。 Spreadsheet::ParseExcel doesn't really understand that much of formatting (mostly ignores it). Spreadsheet::ParseExcel 并没有真正理解那么多的格式(大多忽略它)。

You may find that you get it all working for a particular spreadsheet, but then you'll find a spreadsheet with more intricate formatting that doesn't work as well.您可能会发现,对于特定的电子表格,您可以使用所有这些功能,但随后您会发现格式更复杂的电子表格也无法正常工作。

What you need is something that losslessly interacts with the Excel document object model (DOM).您需要的是与 Excel 文档对象模型 (DOM)无损交互的东西。 (ie load an existing spreadsheet, clone some rows, change the data values, drop some other rows, save out to a different file). (即加载现有的电子表格,克隆一些行,更改数据值,删除一些其他行,保存到不同的文件)。

The only code I've found that does this reliably is Apache POI .我发现可靠地执行此操作的唯一代码是Apache POI

Unfortunately, that's a Java API.不幸的是,这是一个 Java API。 I ended up writing a bunch of plumbing code (in Java) embedded in my perl script using Inline::Java .我最终使用Inline::Java编写了一堆嵌入在我的 perl 脚本中的管道代码(用 Java 编写)。 (There are .NET APIs that can do this too, but I was on Linux, and I could never get the MONO+Wine stack to run most of the Office automation stuff.) (也有 .NET API 可以做到这一点,但我使用的是 Linux,我永远无法使用 MONO+Wine 堆栈来运行大部分 Office 自动化内容。)

This approach works, but the code to pull it off is very complicated.这种方法有效,但实现它的代码非常复杂。 I would discourage trying :-)我不鼓励尝试:-)

Is there some way you could avoid doing this in perl?有什么方法可以避免在 perl 中这样做吗? (Or just this part?) (或者只是这部分?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM