간단한 샘플 Word 문서를 생성하는 샘플 핸들러를 만듭니다.
이 문서의 의지가 텍스트를 포함 안녕하세요 세계Open XML을 사용하여 워드 문서를 만듭니다.
이 내가 만든 Word 문서를 가지고 내가 사용하는 코드 (C# .NET 3.5),
이지만에는 텍스트 크기가 0도 없다.
어떻게 해결할 수 있습니까?
는
public class HandlerCreateDocx : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
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();
// Create the document structure and add some text.
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Hello world!"));
mainPart.Document.Save();
// Stream it down to the browser
context.Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");
context.Response.ContentType = "application/vnd.ms-word.document";
CopyStream(mem, context.Response.OutputStream);
context.Response.End();
}
}
}
// Only useful before .NET 4
public void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[16 * 1024]; // Fairly arbitrary size
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
Open XML 생산성 도구를 사용하여 문서를 디버깅하는 것이 좋습니다. 또한 먼저 Word에서 문서를 만든 다음이 도구를 사용하여 문서를 만들 코드를 제공하십시오. – juharr