JanKallman/EPPlus

Exception on loading xlsx having multiple `_xlnm.Print_Area`

kenjiuno opened this issue · 0 comments

Sometimes LibreOffice Calc (version 6.2.0.3 Windows I have tested) seems to produce xlsx file that EPPlus cannot read.

multiple_print_areas.xlsx

Program.cs

using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp28
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

            var fileInfo = new FileInfo(@"C:\A\multiple_print_areas.xlsx");
            var package = new ExcelPackage(fileInfo);
            var workbook = package.Workbook;
            var worksheets = workbook.Worksheets;
            var worksheet = worksheets.First();
        }
    }
}

$exception+""

System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at OfficeOpenXml.ExcelNamedRangeCollection.AddName(String Name, ExcelNamedRange item)
   at OfficeOpenXml.ExcelNamedRangeCollection.Add(String Name, ExcelRangeBase Range)
   at OfficeOpenXml.ExcelWorkbook.GetDefinedNames()
   at OfficeOpenXml.ExcelPackage.get_Workbook()
   at ConsoleApp28.Program.Main(String[] args) in C:\Proj\ConsoleApp28\Program.cs:line 21

xl/workbook.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
    <fileVersion appName="Calc"/>
    <workbookPr backupFile="false" showObjects="all" date1904="false"/>
    <workbookProtection/>
    <bookViews>
        <workbookView showHorizontalScroll="true" showVerticalScroll="true" showSheetTabs="true" xWindow="0" yWindow="0" windowWidth="16384" windowHeight="8192" tabRatio="500" firstSheet="0" activeTab="0"/>
    </bookViews>
    <sheets>
        <sheet name="Sheet1" sheetId="1" state="visible" r:id="rId2"/>
    </sheets>
    <definedNames>
        <definedName function="false" hidden="false" localSheetId="0" name="_xlnm.Print_Area" vbProcedure="false">Sheet1!$A$1:$E$5</definedName>
        <definedName function="false" hidden="false" localSheetId="0" name="_xlnm.Print_Area" vbProcedure="false">Sheet1!$A$1:$E$5,Sheet1!$A$1:$E$5</definedName>
    </definedNames>
    <calcPr iterateCount="100" refMode="A1" iterate="false" iterateDelta="0.0001"/>
    <extLst>
        <ext xmlns:loext="http://schemas.libreoffice.org/" uri="{7626C862-2A13-11E5-B345-FEFF819CDC9F}">
            <loext:extCalcPr stringRefSyntax="ExcelA1"/>
        </ext>
    </extLst>
</workbook>

The duplicated _xlnm.Print_Area is generated when I used:

Format > Print Ranges > Add Print Range

in LibreOffice Calc.