开发者

how to open an word document on click of link button

开发者 https://www.devze.com 2022-12-16 13:38 出处:网络
i have an link button in my gridview once user click that link button i need to op开发者_如何学编程en an a word document(the pathwhere the document is stored inserver would be like this c:/abc/doc/abc

i have an link button in my gridview once user click that link button i need to op开发者_如何学编程en an a word document(the path where the document is stored in server would be like this c:/abc/doc/abc1.doc) so now i should all ow the user to download that document for viewing it.

how to get it achived thank you


You should look at useing the TransmitFile method instead of the WriteFile method. For what you are doing, it is more efficient.

protected void btnPurchaseOrderOpen_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=one.pdf");
    Response.TransmitFile(@"c:\test\one.pdf");
    Response.End();
}


asp.net, that means you want to open document in web browswer.

here is simple snippet

    string fPath = @"c:/abc/doc/abc1.doc";
    FileInfo myDoc = new FileInfo(fPath);

    Response.Clear();
    Response.ContentType = "Application/msword";
    Response.AddHeader("content-disposition", "attachment;filename=" + myDoc.Name);
    Response.AddHeader("Content-Length", myDoc.Length.ToString());
    Response.ContentType = "application/octet-stream";

    Response.WriteFile(myDoc.FullName);

    Response.End();


Hai,

Add a reference to word object library to your project. Then try this

 Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
    app.Visible = true;
    object visible = true;
    object fileName = @"C:\temp\sample.doc";
    object optional = System.Type.Missing;
    Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ref fileName, ref optional, ref 
        optional, ref optional, ref optional, ref optional, ref optional, ref 
        optional, ref optional, ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional);

If you want web browser thing just have a look at saar's answer

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号