OpenXML docs, how do you know what is when there is no extension

What I have done now and what works:

private string DetermineOpenXML(string file)
    {

        try
        {
            SpreadsheetDocument doc = SpreadsheetDocument.Open(file, false);
            doc.Close();
            return ".xslx";
        }
        catch
        {
            try
            {
                WordprocessingDocument doc = WordprocessingDocument.Open(file, false);
                doc.Close();
                return ".docx";
            }
            catch
            {
                try
                {
                    PresentationDocument doc = PresentationDocument.Open(file, false);
                    doc.Close();
                    return ".pptx";
                }
                catch
                {
                    return string.Empty;
                }
            }
        }
    }

      

I think there must be a better way to see what this file is, other than trial and error. The point is that I am working on a small program that figures out which files the file extension should have. The reason I am doing this is because I have files from a database where they are sometimes saved without extension and at other times with the wrong extension.

What I did with these files is that I found out that all OpenXML documents have the same file signature: "50 4B 03 04 14 00 06 00" which is close to the signature of the zip file and I can also open OpenXML files using a zip program and view its contents. And maybe this is the solution I should go for, I was just hoping it would be faster / easier to use the OpenXML SDK and that it has a property or something that can test it for me.

Edit: I added an answer, I would still like to know if there is a better solution, even though it works for my current purpose. The account does not require extensions to be templates.

+3


source to share


2 answers


I ended up using System.IO.Packaging.

private string anotherOpenXmlAttempt(string file)
    {
        string ext = string.Empty;
        Package package = Package.Open(file);
        if (package.PartExists(new Uri("/word/document.xml", UriKind.Relative)))
        {
            ext = ".docx";
        }
        else if (package.PartExists(new Uri("/xl/workbook.xml", UriKind.Relative)))
        {
            ext = ".xslx";
        }else if (package.PartExists(new Uri("/ppt/presentation.xml", UriKind.Relative)))
        {
            ext = ".pptx";
        }

        package.Close();
        return ext;
    }

      



havn't done any extensive testing but worked on my current files.

I'll leave the question open in case anyone has a good solution.

+3


source


From my experience OpenXMLSDK2 it is more useful for manipulating internal xml document documents. If you just want an extension type, then why not just use:

string extension = System.IO.Path.GetExtension (filename);

It's worth noting that trying to catch is an expensive approach for simply defining the outer details, since it will need all the exception details, the stack trace, etc. for the catch block.



also Excel extension type -.xslx not.xslt, that is, "extensible style language conversions"

Hope it helps!

0


source







All Articles