MiniExcel icon indicating copy to clipboard operation
MiniExcel copied to clipboard

xlsx create and read bug, xlsx文件的创建与读取bug

Open AlexeiNaabal opened this issue 2 years ago • 2 comments

it seems the xlsx created by MiniExcel is not compatible with other openxml reader, for my test it is NPOI.

I am not sure if it is a MiniExcel or NPOI bug, but xlsx created by NPOI XSSFWorkbook is able to read by MiniExcel.

The exception thrown by NPOI when reading xlsx created by MiniExcel has message below:

NPOI.OpenXml4Net.Exceptions.PartAlreadyExistsException: "A part with the name '/xl/sharedStrings.xml' already exists : Packages shall not contain 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)\n   at NPOI.OpenXml4Net.OPC.OPCPackage.CreatePart(PackagePartName partName, String contentType)\n   at NPOI.POIXMLDocumentPart.CreateRelationship(POIXMLRelation descriptor, POIXMLFactory factory, Int32 idx, Boolean noRelation)\n   at NPOI.POIXMLDocumentPart.CreateRelationship(POIXMLRelation descriptor, POIXMLFactory factory)\n   at NPOI.XSSF.UserModel.XSSFWorkbook.OnDocumentRead()\n   at NPOI.POIXMLDocument.Load(POIXMLFactory factory)\n   at NPOI.XSSF.UserModel.XSSFWorkbook..ctor(Stream is1)\n   at Program.<Main>$(String[] args) in /Users/**Projects/ExcelTest/Program.cs:29

A simple block of code to reproduce

List<Dictionary<string, object>> excelContent = new List<Dictionary<string, object>>();

excelContent.Add(new Dictionary<string, object>()
{
    {"Column1", "content1" },
    {"Column2", "content2" },
    {"Column3", "content3" }
});


var excelConfig = new OpenXmlConfiguration()
{
    TableStyles = TableStyles.None
};

string filePath = @"./Excel.xlsx";
MiniExcel.SaveAs(filePath, excelContent, sheetName: "Bruh", configuration: excelConfig, excelType: ExcelType.XLSX, overwriteFile: true);
using(FileStream fs = new FileStream(filePath, FileMode.Open))
{
    IWorkbook workbook = new XSSFWorkbook(fs);
/*
    ISheet sheet = workbook.GetSheetAt(0);
    for (int row = 0; row < sheet.LastRowNum; ++row)
    {
        IRow currentRow = sheet.GetRow(row);
        for (int col = 0; col < currentRow.LastCellNum; ++col)
        {
            ICell cell = currentRow.GetCell(col);
            Console.Write($"{cell.StringCellValue} ");
        }
        Console.WriteLine();
    }
*/
}

似乎MiniExcel创建的xlsx文件与其他openxml读取工具存在不兼容的情况?在我所测试的情况下,这个工具指的是NPOI 我不确定这是MiniExcel还是NPOI的bug,但是NPOI的XSSFWorkbook所创建的xlsx文件能够被MiniExcel读取。

Could someone help me identify this issue?

请问有人能够帮我定位这个问题的原因吗?

AlexeiNaabal avatar Dec 24 '23 10:12 AlexeiNaabal

@AlexeiNaabal next version will support NPOI reading

shps951023 avatar Jan 11 '24 13:01 shps951023

Thanks for fixing this issue.

Do you have a release date at the moment?

AlexeiNaabal avatar Jan 12 '24 01:01 AlexeiNaabal

workbook.xml.rels

MiniExcel

<?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="R4403b806f3d24d22a79b467da74b0a9e" />
    <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
        Target="/xl/styles.xml" Id="R3db9602ace774fdb" />
</Relationships>
<?xml version="1.0" encoding="utf-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">    {{sheets}}    <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="/xl/styles.xml" Id="R3db9602ace774fdb" /></Relationships>

O365

<?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>
 MiniExcelLibs.Tests.MiniExcelIssueTests.TestIssueI4ZYUU
   Duration: 11 ms

  Message: 
    DocumentFormat.OpenXml.Packaging.OpenXmlPackageException : The document cannot be opened because there is an invalid part with an unexpected content type. 
    [Part Uri=/xl/sharedStrings.xml], 
    [Content Type=application/vnd.openxmlformats-package.relationships+xml], 
    [Expected Content Type=application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml].

  Stack Trace: 
    OpenXmlPart.Load(OpenXmlPackage openXmlPackage, OpenXmlPart parent, Uri uriTarget, String id, Dictionary`2 loadedParts)
    OpenXmlPartContainer.LoadReferencedPartsAndRelationships(OpenXmlPackage openXmlPackage, OpenXmlPart sourcePart, RelationshipCollection relationshipCollection, Dictionary`2 loadedParts)
    OpenXmlPart.Load(OpenXmlPackage openXmlPackage, OpenXmlPart parent, Uri uriTarget, String id, Dictionary`2 loadedParts)
    OpenXmlPartContainer.LoadReferencedPartsAndRelationships(OpenXmlPackage openXmlPackage, OpenXmlPart sourcePart, RelationshipCollection relationshipCollection, Dictionary`2 loadedParts)
    OpenXmlPackage.Load()
    OpenXmlPackage.OpenCore(String path, Boolean readWriteMode)
    SpreadsheetDocument.Open(String path, Boolean isEditable, OpenSettings openSettings)
    SpreadsheetDocument.Open(String path, Boolean isEditable)
    XLWorkbook.LoadSheets(String fileName)
    XLWorkbook.Load(String file)
    XLWorkbook.ctor(String file, XLEventTracking eventTracking)
    XLWorkbook.ctor(String file)
    MiniExcelIssueTests.TestIssueI4ZYUU() line 353

image

shps951023 avatar May 12 '24 11:05 shps951023