nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.73k stars 1.43k forks source link

Open xlsx file generated by MiniExcel failed #1237

Closed li-zheng-hao closed 11 months ago

li-zheng-hao commented 11 months ago

NPOI Version

2.6.2

File Type

Upload the Excel File

sample.xlsx

Reproduce Steps

  1. download sample project zip file ConsoleApp1.zip
  2. run project and it will throw exception
Unhandled exception. NPOI.OpenXml4Net.Exceptions.PartAlreadyExistsException: A part with the name '/xl/sharedStrings.xml' already exists : Packages shall not conta
in equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]
   at NPOI.OpenXml4Net.OPC.OPCPackage.CreatePart(PackagePartName partName, String contentType, Boolean loadRelationships)
   at NPOI.OpenXml4Net.OPC.OPCPackage.CreatePart(PackagePartName partName, String contentType)
   at NPOI.POIXMLDocumentPart.CreateRelationship(POIXMLRelation descriptor, POIXMLFactory factory, Int32 idx, Boolean noRelation)
   at NPOI.XSSF.UserModel.XSSFWorkbook.OnDocumentRead()
   at NPOI.POIXMLDocument.Load(POIXMLFactory factory)
   at NPOI.XSSF.UserModel.XSSFWorkbook..ctor(OPCPackage pkg)
   at NPOI.XSSF.UserModel.XSSFWorkbook..ctor(String path)
   at Program.<Main>$(String[] args) in E:\Code\NpoiBugSample\ConsoleApp1\Program.cs:line 7

Issue Description

this xlsx file is generated by MiniExcel lib, and it can open by MS Office ,but it cannot open by npoi

image

tonyqus commented 11 months ago

I believe this is a bug of MiniExcel

This is the workbook.xml.rels in the original Excel file.

<?xml version="1.0" encoding="utf-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="/xl/worksheets/sheet1.xml" Id="R9565601aa86e4230840e173f558e9418"/>
    <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="/xl/styles.xml" Id="R3db9602ace774fdb"/>
</Relationships>

And this is the fixed version of workbook.xml.rels by Microsoft Excel after resaving this file

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
    <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
    <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>
    <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>
</Relationships>

It looks theme/theme1.xml and SharedStrings.xml are missing in this list. But they should appear here. And NPOI has the logic that if there is no SharedStrings.xml in workbook.xml.rels, it will create one. You can find the code here.

And I do notice that sharedStrings.xml is empty. I believe it's a kind of performance optimization in MiniExcel while generating the Excel file.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="0" uniqueCount="0"/>

You should talk to the MiniExcel team.