在ASP .NET页面中嵌入只读Word文档 [英] Embedding a Read Only Word Document in a ASP .NET Page

查看:102
本文介绍了在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.

推荐答案

有两个解决方案,但我们不能在只读中打开单词。你可以设置word文件密码保护。

first soution

there is two solution but we can't open word in read only. you can set word file password protected.
first soution
<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();


string filename = @C:/.../ xx.docx;

// string filename2 = @xx.docx;

object file = filename;

object nullobj = System.Reflection.Missing.Value;



Microsoft.Office.Interop.Word.ApplicationClass wordApp = new ApplicationClass();

//对象文件=路径;

// object nullobj = System.Reflection.Missing.Value;



Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(

ref file,ref nullobj,ref nullobj,

ref nullobj,ref nullobj,ref nullobj,

ref nullobj,ref nullobj,ref nullobj,

ref nullobj,ref nullobj,ref nullobj);

doc.ActiveWindow.Selection.WholeStory();

doc.ActiveWindow.Selection.Copy ();

System.Windows.Forms.IDataObjec t data = Clipboard.GetDataObject();

txtFileContent.Text = doc.Content.Text; // data.GetData(DataFormats.Text).ToString();

doc.Close(ref nullobj,ref nullobj,ref nullobj);

wordApp.Quit(ref nullobj,ref nullobj,ref nullobj);
string filename = @"C:/.../xx.docx";
//string filename2 = @"xx.docx";
object file = filename;
object nullobj = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.ApplicationClass wordApp = new ApplicationClass();
//object file = path;
//object nullobj = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(
ref file, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
System.Windows.Forms.IDataObject data = Clipboard.GetDataObject();
txtFileContent.Text = doc.Content.Text;//data.GetData(DataFormats.Text).ToString();
doc.Close(ref nullobj, ref nullobj, ref nullobj);
wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);


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

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