0

I wrote the simple code, but even it doesn't work. I receveid the file that MS Word can't open.

XWPFDocument doc;

using (Stream fileStream = System.IO.File.OpenRead(pathTemplate))
{
    doc = new XWPFDocument(fileStream);
    fileStream.Close();
}


using (FileStream fileStreamNew = System.IO.File.Create(fullPath))
{
    doc.Write(fileStreamNew);
    fileStreamNew.Close();
}
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
vovkor
  • 1
  • 1
  • 1
  • 2
    So did you compare the files? What's different? – nvoigt Mar 29 '19 at 10:34
  • You should provide more information, in order for others to be able to help. – meJustAndrew Mar 29 '19 at 10:42
  • Not being that familiar with this library, but knowing how big docx files can be, I'd be remarkably surprised if just calling the *constructor* caused an entire in-memory representation of the document to be created, so closing the stream it's working with before you're done actually using it is probably a problem. Can't help much with your real problem at the moment though since the whole piece of code can be replaced with `File.Copy(pathTemplate,fullPath);`. – Damien_The_Unbeliever Mar 29 '19 at 11:02
  • Have you looked at the size of the file? Is it what you expect? Just out of curiosity, try calling `fileStreamNew.Flush()` before closing it to ensure that everything is written to the stream. – Scott Hannen Mar 29 '19 at 11:50
  • @ScottHannen - no need to `Flush`. Both the redundant `Close` (which calls `Dispose`) and the implicit `Dispose` call (from `using`) are specced to flush any final changes. – Damien_The_Unbeliever Mar 29 '19 at 11:52
  • Thanks to all. That code works. It's my mistake. I used wrong typeConten when upload to client : return File(fullPath, "application/vnd.ms-excel", file); – vovkor Mar 30 '19 at 13:53
  • Do you have picture with "wrap text" option on it? – Steven.Xi Jan 18 '21 at 08:53

0 Answers0