Skip to content

Opening Ms Word Document in asp.net

suresh

Asked Jan 26, 2012, 9:24 AM EST1 answer
Share
LinkedInX
Hi,
My web page contains two button i.e
Collapse | Copy Code
<asp:button id="btnOpenword" runat="server" text="OpenFile">
<asp:button id="btnNext" runat="server" text="Next" visible="false">

when i click on the OpenFile Button i need to open the word document and also below the Next button should visible to the user.
so i write some code but it's working to open the word document, but it's not visible the Next button to the user.
Collapse | Copy Code
btnNext.Visible = true;
FileInfo file = new FileInfo("filename.doc");
Response.ClearContent();
Response.AddHeader("Content-Disposition", "inline;filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/msword";
Response.TransmitFile(file.FullName);
Response.End();

Plese give me solution it's urgent.
or is there any way to do..
Thanks ,
Suresh

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi,

    I assume you want to export to Word in ASP.NET.

    Here is a sample C# code how to do it with this C# / VB.NET Word component:

    JavaScript
    // Use the component in free mode.
     ComponentInfo.SetLicense("FREE-LIMITED-KEY");
    
     // 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();
     }
    0