在asp.net的浏览器中打开word文档文件 [英] open word document file in browser in asp.net

查看:96
本文介绍了在asp.net的浏览器中打开word文档文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在浏览器中显示word文档文件。



我也在这里写一个代码。

但是它没有在浏览器中打开,而是首先打开弹出窗口进行下载或打开文件。



但是我想读取文件在浏览器中没有打开下载的文件

I want to display word document file in the browser .

I also write a code here.
but it is not open in browser but first open pop up window for download or open the file.

But i want to read file in the browser not a downloaded file is open

推荐答案

只需通过iframe嵌入Google Doc Viewer并指定要显示的DOC文件。这是你应该添加的代码:



Just embed Google Doc Viewer through an iframe and specify the DOC file you want to display. This is the code you should add:

<iframe src="http://docs.google.com/gview?url=http://www.YOUR_DOMAIN.com/YOUR_WORD.DOC&embedded=true" style="width:600px; height:500px;" frameborder="0"></iframe>


啊,对不起..我误解了。

在这种情况下,我认为你需要合作将doc文件转换为html,然后直接在浏览器上加载html内容。下面是将doc转换为html然后在浏览器中加载html的示例代码:

ah, sorry.. I misunderstood.
In that case I think you need to convert the doc file to html and then load html content directly on browser. Here is sample code to convert doc to html and then load the html in browser:
private void OpenMSWordFileByBrowser()
    {
        string htmlFilePath = "E:\\test.html";
        Convert("E:\\test.docx",htmlFilePath, WdSaveFormat.wdFormatHTML);
        //
        Response.ClearContent();
        Response.ClearHeaders();
        Response.WriteFile(htmlFilePath);
        Response.Flush();
        Response.Close();
    }
    private static void Convert(string docFilePath,string htmlFilePath, WdSaveFormat format)
    {

        DirectoryInfo dirInfo = new DirectoryInfo(docFilePath);
        FileInfo wordFile = new FileInfo(docFilePath);
        //
        object oMissing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
        try
        {
            word.Visible = false;
            word.ScreenUpdating = false;
            //
            Object filename = (Object)wordFile.FullName;
            Document doc = word.Documents.Open(ref filename, ref oMissing,
                                               ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                               ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                               ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            try
            {
                doc.Activate();
                object outputFileName = htmlFilePath;
                object fileFormat = format;
                doc.SaveAs(ref outputFileName,
                           ref fileFormat, ref oMissing, ref oMissing,
                           ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                           ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                           ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            }
            finally
            {
                object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
                ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
                doc = null;
            }
        }
        finally
        {
            ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
            word = null;
        }
    }





如果您的doc文件中存在doc文件,则可以使用相对路径而不是物理文件位置应用程序文件夹并使用server.mappath。



Instead of physical file location you can use relative path if the doc file exists in your application folder and use server.mappath.


您好,这是打开word文档文件的代码:

Hi, Here is the code to open a word document file:
Context.Response.Clear();
        FileInfo file = new FileInfo("E:\\test.docx");
        Context.Response.ContentType = "Application/msword";
        Context.Response.AppendHeader("Content-Disposition", "inline; filename=" + file.Name);
        Context.Response.AppendHeader("Content-Length", file.Length.ToString());
        Context.Response.WriteFile(file.FullName);
        Context.Response.End();


这篇关于在asp.net的浏览器中打开word文档文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆