Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

c# - asp.net open word document

I try to open a word document with c#.
When I open the document, the page is blocked after.

Here is the code :

HttpContext.Current.Response.Write(temp);
//HttpContext.Current.Response.End();

//HttpContext.Current.Response.Flush();

//HttpContext.Current.Response.Write(sw.ToString());
//HttpContext.Current.Response.clear();
//HttpContext.Current.Response.End();
//HttpContext.Current.Response.SuppressContent = true;
//HttpContext.Current.Response.Close();
//Response.Redirect(Page.Request.Url.AbsolutePath.Substring(0, Page.Request.Url.AbsolutePath.LastIndexOf("/")) + "/PIEditor.aspx?PostID=" + Request.Params["PostID"], true);`
//HttpContext.Current.Response.End();

As you see, I tried different options but without result, the window for opening or saving the document is displayed but I can't click on any buttons the page after. It looks like it is deactivated or stopped.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can try GemBox.Document component to export Word document from ASP.NET application, if that is what you are trying to do.

Here is a sample C# code that should go in ASPX page code behind:

// Create a new empty document.
DocumentModel document = new DocumentModel();

// Add document content.
document.Sections.Add(new Section(document, new Paragraph(document, "Hello World!")));

// Microsoft Packaging API cannot write directly to Response.OutputStream.
// Therefore we use temporary MemoryStream.
using (MemoryStream documentStream = new MemoryStream())
{
    document.Save(documentStream, SaveOptions.DocxDefault);

    // Stream file to browser.
    Response.Clear();
    Response.ContentType = "application/vnd.openxmlformats";
    Response.AddHeader("Content-Disposition", "attachment; filename=Document.docx");

    documentStream.WriteTo(Response.OutputStream);

    Response.End();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...