如何在 Perl 或批处理中合并两个 Excel (xls) 文件?

发布于 2024-11-26 04:04:03 字数 156 浏览 1 评论 0原文

我有两个文件,例如 a.xlsb.xls。第一张包含 2 张,第二张包含 3 张。有人可以告诉我是否可以使用 Perl 或批处理脚本合并它们吗?

我想要在一个 XLS 文件中输出包含 5 页的内容。

I have two files lets say a.xls and b.xls. The first one contains 2 sheets and the second one 3 of them. Can someone let me know if I can merge them using Perl or batch scripting?

I want an output with 5 sheets in one single XLS file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

标点 2024-12-03 04:04:03

对于 Perl 解决方案,请使用 Spreadsheet::ParseExcel 读取文件并 Spreadsheet::WriteExcel 保存您的 输出。这两个模块都有详细的文档记录,并附带大量示例代码(例如 WriteExcel)

For a Perl solution use Spreadsheet::ParseExcel to read the files and Spreadsheet::WriteExcel to save your output. Both modules are well documented and come with lots of example code(e.g. WriteExcel)

夏末的微笑 2024-12-03 04:04:03

如果您在 Windows 上运行,已安装 Excel 并且可以使用 Win32::OLE (例如,脚本不会被 Web 服务器调用等),以下应该可以工作:

#!/usr/bin/perl

use warnings;
use strict;

use FindBin qw($Bin);
use File::Spec::Functions qw( catfile );

use Win32::OLE qw(in);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3;

my $excel = get_excel();
$excel->{Visible} = 1; # for illustration only

my @src = map $excel->Workbooks->Open($_),
          map catfile($Bin, $_),
          qw(one.xls two.xls)
          ;

my $target = $excel->Workbooks->Add(xlWBATWorksheet);
my $before = $target->Worksheets->Item(1);

for my $book ( @src ) {
    my $sheets = $book->Worksheets;
    my $it = Win32::OLE::Enum->new($sheets);

    while (defined(my $sheet = $it->Next)) {
        $sheet->Copy($before);
    }
}

$before->Delete;

$_->Close for @src;
$target->SaveAs(catfile($Bin, 'test.xls'));
$target->Close;

sub get_excel {
    my $excel = Win32::OLE->GetActiveObject('Excel.Application');
    unless(defined $excel) {
        $excel = Win32::OLE->new('Excel.Application', sub { $_[0]->Quit })
            or die "Oops, cannot start Excel: ",
                   Win32::OLE->LastError, "\n";
    }
    return $excel;
}

If you are running on Windows, have Excel installed and can use Win32::OLE (e.g. the script will not be invoked by a web server etc), the following should work:

#!/usr/bin/perl

use warnings;
use strict;

use FindBin qw($Bin);
use File::Spec::Functions qw( catfile );

use Win32::OLE qw(in);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3;

my $excel = get_excel();
$excel->{Visible} = 1; # for illustration only

my @src = map $excel->Workbooks->Open($_),
          map catfile($Bin, $_),
          qw(one.xls two.xls)
          ;

my $target = $excel->Workbooks->Add(xlWBATWorksheet);
my $before = $target->Worksheets->Item(1);

for my $book ( @src ) {
    my $sheets = $book->Worksheets;
    my $it = Win32::OLE::Enum->new($sheets);

    while (defined(my $sheet = $it->Next)) {
        $sheet->Copy($before);
    }
}

$before->Delete;

$_->Close for @src;
$target->SaveAs(catfile($Bin, 'test.xls'));
$target->Close;

sub get_excel {
    my $excel = Win32::OLE->GetActiveObject('Excel.Application');
    unless(defined $excel) {
        $excel = Win32::OLE->new('Excel.Application', sub { $_[0]->Quit })
            or die "Oops, cannot start Excel: ",
                   Win32::OLE->LastError, "\n";
    }
    return $excel;
}
北风几吹夏 2024-12-03 04:04:03

您可以使用 Alacon - Alasql 数据库的命令行实用程序来完成此操作。

它与 Node.js 配合使用,因此您需要安装 Node.js,然后安装 Alasql 包:

要从 Excel 文件中获取数据,您可以使用以下命令:

> node alacon "SELECT * INTO XLSX("main.xls",{headers:true}) 
               FROM XLSX('data1.xlsx', {headers:true}) 
               UNION ALL SELECT * FROM XLSX('data2.xlsx', {headers:true})
               UNION ALL SELECT * FROM XLSX('data3.xlsx', {headers:true})
               UNION ALL SELECT * FROM XLSX('data4.xlsx', {headers:true})
               UNION ALL SELECT * FROM XLSX('data5.xlsx', {headers:true})"

这是很长的一行。在此示例中,所有文件都在“Sheet1”工作表中具有数据。

You can do it with Alacon - command-line utility for Alasql database.

It works with Node.js, so you need to install Node.js and then Alasql package:

To take data from Excel file you can use the following command:

> node alacon "SELECT * INTO XLSX("main.xls",{headers:true}) 
               FROM XLSX('data1.xlsx', {headers:true}) 
               UNION ALL SELECT * FROM XLSX('data2.xlsx', {headers:true})
               UNION ALL SELECT * FROM XLSX('data3.xlsx', {headers:true})
               UNION ALL SELECT * FROM XLSX('data4.xlsx', {headers:true})
               UNION ALL SELECT * FROM XLSX('data5.xlsx', {headers:true})"

This is one very long line. In this example all files have data in "Sheet1" sheets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文