none
Corrupted file RRS feed

  • Question

  • Hello everyone,

    I'm writeing utility which loads a .xlsx file, make some changes and save it back. I use Microsoft.Office.DocumentFormat.OpenXml.Packaging for manipulating with xlsx file. Problem is that resulting file is corrupted. When I open the resulting file in excel, excel opens it and is able to repair it and after that the file looks like it should looks like (all data and formating is ok). There are 2 strenge things about the corrupted file. First, resulting file from my utility is very big, it's 10times bigger then if I repair it and save it by excel. Second, if I unzip resulting file and zip it again, everything is ok (size is ok and no need for repairing from excel). Same if I run some zip-repair utility. So it seems that problem is with accessing to the zip archive. Fragments of code, how my utility works (C#):

    Opening file:

    SpreadsheetDocument.Open(DocumentFile, true);

    How I load and save data from/to document parts:

    public static void WriteXmlToPart(XmlDocument xdata, OpenXmlPart part)
            {
                using (XmlTextWriter wrt = new XmlTextWriter(part.GetStream(FileMode.Create), Encoding.UTF8))
                {
                     xdata.WriteTo(wrt);
                }
            }

            public static XmlDocument LoadXmlFromPart(OpenXmlPart part)
            {
                using (Stream stm = part.GetStream())
                {
                    XmlDocument xd = new XmlDocument();
                    xd.Load(stm);
                    return xd;
                }
            }

    Any suggestions what I'm doing wrong? Any ideas what should I try?

    Thanks

    Ondra
    Thursday, February 7, 2008 2:45 PM