私が作成したワードドキュメントをブラウザにストリーミングできません。私は絶えずMicrosoft Wordから文書が壊れているというメッセージを受け取ります。メモリにストリーミングするOpenXML SDKを使用しているASP.NETドキュメントでASP.NETが破損しています。
コンソールアプリケーション経由でコードを実行し、ASP.NETを画像から取り除くと、ドキュメントは問題なく正しく生成されます。私はすべてがファイルを書き込むことを中心に考えています。しかし、何もかなり機能していない - 私はlinksの多くでlookedを持って
using (MemoryStream mem = new MemoryStream())
{
// Create Document
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(mem,
WordprocessingDocumentType.Document, true))
{
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
new Document(new Body()).Save(mainPart);
Body body = mainPart.Document.Body;
body.Append(new Paragraph(
new Run(
new Text("Hello World!"))));
mainPart.Document.Save();
// Stream it down to the browser
// THIS IS PROBABLY THE CRUX OF THE MATTER <---
Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");
Response.ContentType = "application/vnd.ms-word.document";
mem.WriteTo(Response.OutputStream);
Response.End();
}
}
:
は、ここに私のコードです。私は多くの人がMemoryStream.WriteToを使用し、BinaryWriteを使用しています。この時点では、正しい方法が何であるか分かりません。また、長いコンテンツタイプ、つまりapplication/vnd.openxmlformats-officedocument.wordprocessingml.documentを試しましたが、運はありません。 )(使用して内
..: -
いくつかのスクリーンショットは、あなたが回復しようとする場合でも、あなたはこの質問につまずく人のために、同じ「部品が欠落しているか、または無効です」
ソリューションを取得しますWordProcessingDocumentのディレクティブを呼び出す必要があります。
wordDocument.Save();また、正しく外使用のブロックでこれを使用し、MemoryStreamををストリーミングする
:
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");
mem.Position = 0;
mem.CopyTo(Response.OutputStream);
Response.Flush();
Response.End();
どのようにwordDocument.Save()を追加しますか? ?私は試してみました。それに近い唯一のコードは.Close()で、それは私にとってはうまくいきません。 – RicL
あなたの質問と回答のためにkd7に感謝します。 –