Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Excel file: Specified part does not exist in the package. #247

Closed
AlirezaAhmadiRad opened this issue Sep 24, 2017 · 6 comments
Closed

Excel file: Specified part does not exist in the package. #247

AlirezaAhmadiRad opened this issue Sep 24, 2017 · 6 comments
Assignees
Labels

Comments

@AlirezaAhmadiRad
Copy link

AlirezaAhmadiRad commented Sep 24, 2017

Hello
I have an Excel file directly created by Microsoft Excel. When I try to open it with SpreadsheetDocument.Open(), I get exception of Specified part does not exist in the package.

I'm using NuGet 2.7.2

Exception details:
Source: WindowsBase
Target site: System.IO.Packaging.Package.GetPart(System.Uri)

Could you please help?

DB84080A-8AB9-49AB-A607-0367D6A7F73ABook1.xlsx

@tarunchopra
Copy link

@AlirezaAhmadiRad : I used following code to open your file and it worked fine.I tested this on Windows OS.

OpenSettings openSettings = new OpenSettings();
openSettings.MarkupCompatibilityProcessSettings = new MarkupCompatibilityProcessSettings(MarkupCompatibilityProcessMode.ProcessAllParts, FileFormatVersions.Office2013);
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(@"DB84080A-8AB9-49AB-A607-0367D6A7F73ABook1.xlsx", true, openSettings))
{
OpenXmlValidator v = new OpenXmlValidator(FileFormatVersions.Office2013);
var errs = v.Validate(doc);
var cnt = errs.Count();
Assert.True(cnt == 1 || cnt == 0);
}
if (TestUtil.DeleteTempFiles)
fiCopy.Delete();
}

Thanks

@AlirezaAhmadiRad
Copy link
Author

Thanks
I usd your code but no success. I must read the file from stream.

            OpenSettings openSettings = new OpenSettings();
            openSettings.MarkupCompatibilityProcessSettings = new MarkupCompatibilityProcessSettings(MarkupCompatibilityProcessMode.ProcessAllParts, FileFormatVersions.Office2013);          
            using (SpreadsheetDocument doc = SpreadsheetDocument.Open(stream2,false, openSettings))
            {
                OpenXmlValidator v = new OpenXmlValidator(FileFormatVersions.Office2013);
                var errs = v.Validate(doc);
                var cnt = errs.Count();
                //Assert.True(cnt == 1 || cnt == 0);
            }

I also checked with MarkupCompatibilityProcessMode.ProcessLoadedPartsOnly but the same problem persisted.

@tarunchopra
Copy link

@AlirezaAhmadiRad : Can you replace that 'stream2' with absolute path of the file to verify if that is causing this issue? Also, are you running this on Windows OS or MAC ?

@AlirezaAhmadiRad
Copy link
Author

Hello
Sorry for delay. Opening directly from file works well. Opening from stream fails.

Thnaks

@tarunchopra
Copy link

tarunchopra commented Oct 3, 2017

You have not assigned anything to memorystream which is most likely the reason for this error. Refer following working code. Use 'ms.Write(ba, 0, ba.Length)' before opening the SpreadsheetDocument; to resolve the issue.

        var docName = @"DB84080A-8AB9-49AB-A607-0367D6A7F73ABook1.xlsx";
        var ba = File.ReadAllBytes(docName);
        using (MemoryStream ms = new MemoryStream())
        {
            ms.Write(ba, 0, ba.Length);
            using (SpreadsheetDocument doc = SpreadsheetDocument.Open(ms, true))
            {
                OpenXmlValidator v = new OpenXmlValidator(FileFormatVersions.Office2013);
                var errs = v.Validate(doc);
                var cnt = errs.Count();
                Assert.True(cnt == 1 || cnt == 0);
            }
        }

@tarunchopra tarunchopra self-assigned this Oct 3, 2017
@AlirezaAhmadiRad
Copy link
Author

Thanks, you directed me to correct route. byte array is corrupted somewhere in the middle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants