के साथ शब्द दस्तावेज़ बनाएं मैं सरल शब्द दस्तावेज़ उत्पन्न करने के लिए नमूना हैंडलर बना रहा हूं।
इस दस्तावेज़ इच्छा पाठ शामिल हैलो वर्ल्डओपन एक्सएमएल
इस कोड का उपयोग मैं (सी # .NET 3.5),
मेरे द्वारा बनाए गए वर्ड दस्तावेज़ मिल गया है, लेकिन यह करने में कोई पाठ का आकार 0 नहीं है, तो।
मैं इसे कैसे ठीक कर सकता हूं?
(मैं CopyStream विधि का उपयोग करें क्योंकि CopyTo .NET 4.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);
}
}
}
मैं आपके दस्तावेज़ को डीबग करने के लिए ओपन एक्सएमएल उत्पादकता उपकरण का उपयोग करने की सलाह देता हूं। दस्तावेज़ को पहले Word में बनाने पर विचार करें और फिर टूल को बनाने के लिए टूल का उपयोग करने के लिए टूल का उपयोग करें। – juharr