OpenXML error "file is corrupt and cannot be opened." for a Word Document in C#
I have built a word document with Word 2007 and saved it. Then i used OpenXML productivity tool and used code reflection to grab the C# code.
The next step I did was build a function that returns a tableRow object. I would use my list I had built with LINQ to populate the text fields of the row. I copied the row generation code from 1 of the built in rows of the table, and filled in the fields in a loop, using
foreach(ClassForLinqResult lr in LinqResult)
{
Table1.Append(CreateRow(lr.param1,lr.param2..etc));
}
The problem remains, when the server responds back with the file, I get an error from word trying to open it:
"The file is corrupt and cannot be opened".
Was there some part of the code I missed that needs to be added somewhere else for the rows I add? As it turns out when I use word to try to recover the file, my rows are showing correctly.
The corrupted file has a package folder with a file in it like this:
package\services\metadata\core-properties\edf684ae4e35438dacc06cb66c0afad2.psmdcp
after it corrects the file it removes that and adds a core.xml file to the "docProps" folder.
some other files are changed
customXML\item.xml changes to customXML\item1.xml after recovery
customXML\itemProps.xml changes to customXML\itemProps1.xml after recovery
customXML\item.xml.rels changes to customXML\item1.xml.rels after recovery
word\footer.xml changes to word\footer1.xml after recovery
word\theme\theme.xml changes to word\theme\theme1.xml
Anyway, if anyone has some tools that point out the problem for me I'd appreciate it. I have tried
OpenXmlPackageValidationSettings validationSettings = new OpenXmlPackageValidationSettings();
validationSettings.EventHandler += new EventHandler<OpenXmlPackageValidationEventArgs>(PackageValidationEventHandler);
CreateParts(package,LicenseToPrint);
package.Validate(validationSettings);
This never hits any code indicating there was problem with the validation. I have not been able to figure out the problem with the word document. Can anyone help with an idea how to figure out the problem? Or perhaps I missed a step that is causing my problem?
Additional Information:
This code is how I am rendering the docx fi开发者_JAVA技巧le in asp.net
GeneratedClass c = new GeneratedClass();
using (MemoryStream generatedStream = new MemoryStream())
{
c.CreatePackage(generatedStream,LicenseToPrint);
ValidateDocument(generatedStream);
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; FileName=\"License.docx\"");
Response.ContentType = "application/msword";
//Response.ContentType = "application/vnd.ms-word.document";
generatedStream.WriteTo(Context.Response.OutputStream);
Context.Response.Flush();
}
I am not sure what was the cause, but I repeated my steps again and now have a file with no more errors with MS word. Copying from the Open XML tool into my code screwed up somewhere. If this happens to anyone doing similar steps, my suggestion is to repeat your steps, it may have been a slip up somewhere doing the copying of the code. The problem is if you have a lot of code written already and there's too many steps to trace back. A versioning system should help in such a case. Thank you all
I don't have the answer. But I can help rule out some of the available information.
customXML\item.xml changes to customXML\item1.xml after recovery
customXML\itemProps.xml changes to customXML\itemProps1.xml after recovery
customXML\item.xml.rels changes to customXML\item1.xml.rels after recovery
word\footer.xml changes to word\footer1.xml after recovery
word\theme\theme.xml changes to word\theme\theme1.xml
Don't worry about the renaming. Microsoft Word likes to add a suffix-number to any parts that can have multiples. So when word recovered the document, it recreated the document and used its preferred naming system for those parts.
It's possible the file have not finish writing/saving when you tried to open the file as in my case. I waited a few seconds before opening the file and it worked.
Also, make sure to dispose of streams used as recommended in Open-XML-SDK issue#1188
精彩评论