NET页面中嵌入只读Word文档

NET页面中嵌入只读Word文档

本文介绍了在ASP .NET页面中嵌入只读Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我需要在ASP .NET webform页面中显示Word文档。到目前为止,我所做的是:



Hello,
I have a requirement where I need to display a Word Document in a ASP .NET webform page. So far what I have done is this:

object fileName = "RandomDocument.docx";
object readOnly = true;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application oWordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document oWordDoc = new Document();
try
{
    oWordApp.Visible = false;
    oWordDoc = oWordApp.Documents.Open(ref fileName,
                      ref missing, ref readOnly,
                      ref missing, ref missing, ref missing,
                      ref missing, ref missing, ref missing,
                      ref missing, ref missing, ref isVisible,
                      ref missing, ref missing, ref missing);

    oWordDoc.Activate();
}
catch (Exception ex)
{
}
finally
{
    oWordApp.Visible = true;
}
object startPosition = 0;
object endPosition = (object)oWordDoc.Characters.Count;
Range range = oWordDoc.Range(ref startPosition, ref endPosition);
string text = range.Text;





正如您所看到的,我在Word窗口中打开此文档并从中获取文本完全没有问题还有。



但是我需要在ASP .NET页面上显示这个文档。现在我可以从代码块的最后一部分获取文本,它不包含在Word文档中完成的格式化。



我搜索过很多但找不到合适的答案。有关是否可以在页面上显示文档的只读视图的任何指针?在此先感谢。



As you can see I have no problem at all opening this document in a Word window and getting the text from it as well.

But I need this document to display on a ASP .NET page. Now I'm able to get the text from the last part in the code block it does not comprise of the exact formatting done in the Word document.

I have searched a lot but could not find a proper answer. Any pointers as to whether it is possible to display a read only view of the document on the page? Thanks in advance.

推荐答案

<iframe frameborder ="1" height ="200px" width ="600px" id ="iframe1" runat ="server" scrolling ="auto"  >
</iframe>





in .cs file add

iframe1.Attributes [ src] = filesNameWithPath;



第二个解决方案



in .cs文件添加





in .cs file add
iframe1.Attributes["src"] = filesNameWithPath;

second solution

in .cs file add

Response.Clear();
        Response.ContentType = "application/msword";
        Response.AddHeader("Content-Disposition", "attachment; filename=\"" + sFilename + "\"");
        Response.Flush();
byte[]databyte = File.ReadAllBytes(strFilepath);

        MemoryStream ms = new MemoryStream();
        ms.Write(databyte, 0, databyte.Length);
        ms.Position = 0;
        ms.Capacity = (int)ms.Length;
        byte[] arrbyte = ms.GetBuffer();
        ms.Close();
        Response.BinaryWrite(arrbyte);
        Response.End();



这篇关于在ASP .NET页面中嵌入只读Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:40