如何使用Perl向现有Excel电子表格添加新行?

时间:2010-01-14 10:43:38

标签: perl excel spreadsheet

我使用Perl创建了我的Excel工作表 a.xls ,其中我有以下字段:

date         name    eid     
13/jan/2010   asa    3175

当我下次编译时,如果日期将超过上一个日期,那么它必须像智慧一样更新:

date         name   eid
13/jan/2010  asa    3175
14/jan/2010  stone  3180

如果日期将是上一行日期,因为上一行日期为14/jan/2010且当前日期也为14/jan/2010,则它不应插入任何应该仅更新上一条记录的行。

3 个答案:

答案 0 :(得分:5)

参见Spreadsheet::ParseExcel::SaveParser documentation中的示例。据我了解,AddCell方法可以替换现有的单元格或添加新的。

答案 1 :(得分:0)

使用{2007}文件,使用Excel::Writer::XSLX,您无法执行此操作。自2012年6月起发布0.47:

  

此模块目前尚不能用于写入现有Excel   XLSX文件。

您只能创建新文件。

答案 2 :(得分:0)

    use Win32::OLE::Const 'Microsoft Excel';

    $Excel = Win32::OLE->new('Excel.Application', 'Quit');
    $Excel->{DisplayAlerts}=0;

    my $workbookpath = "D:\\temp.xlsx";
    my $workbook = $Excel->Workbooks->Open($workbookpath);
    # lets think if you created multiple sheets in an workbook, you can refer each workbook by name(Ex: sheet1 here)
    my $sheet = $workbook->WorkSheets("sheet1");
    #Insert row in a particular row value
    $sheet->Cells(8,1)->EntireRow->Insert;
    #save and close workbook, its a best practise when you are doing any excel operation
    $workbook->Save;
    $workbook->Close();