如何在网页中预览Word文件 [英] How Do I Preview Word File In A Webpage

查看:2099
本文介绍了如何在网页中预览Word文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS13和sql server.i想要预览word文件,当它在数据库中上传并从detabase.i下载时已经使用了一些代码但是有一些错误。

下面给出的代码和错误---我使用的技术参考来自 http://www.aspsnippets.com/Articles/Display-Word-document-on-web-page-in-ASP.Net.aspx [ ^ ]



SETP 1:添加了对Microsoft Word 11.0对象库的引用

第2步:

 使用 Microsoft.Office; 
使用 Microsoft.Office.Interop.Word;
使用 System.IO;



第3步:

< pre lang =cs> protected void 上传( object sender,EventArgs e)
{
object missingType = Type.Missing;
object readOnly = true ;
object isVisible = false ;
object documentFormat = 8 ;
string randomName = DateTime.Now.Ticks.ToString();
object htmlFilePath = Server.MapPath( 〜/ Temp /)+ randomName + 。htm;
string directoryPath = Server.MapPath( 〜/ Temp /)+ randomName + _ files;

// 上传word文档并保存到Temp文件夹
FileUpload1.SaveAs(Server.MapPath( 〜/ Temp /)+ Path.GetFileName(FileUpload1 .PostedFile.FileName));
object fileName = FileUpload1.PostedFile.FileName;

// 在后台打开word文档
ApplicationClass applicationclass = new ApplicationClass();
applicationclass.Documents.Open( ref fileName,
ref readOnly,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref isVisible,
ref missingType, ref missingType , ref missingType,
ref missingType, ref missingType);
applicationclass.Visible = false ;
Document document = applicationclass.ActiveDocument;

// 将word文档另存为HTML文件
document .SaveAs( ref htmlFilePath, ref documentFormat, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType);

// 关闭word文档
document.Close( ref missingType, ref missingType, ref missingType );

// 删除上传的Word文件
File.Delete (Server.MapPath( 〜/ Temp /)+ Path.GetFileName(FileUpload1.PostedFile.FileName ));

// 将Html文件读取为字节数组并在浏览器中显示
byte [] bytes;
使用(FileStream fs = new FileStream(htmlFilePath.ToString(),FileMode.Open, FileAccess.Read))
{
BinaryReader reader = new BinaryReader(fs);
bytes = reader.ReadBytes(( int )fs.Length);
fs.Close();
}
Response.BinaryWrite(bytes);
Response.Flush();

// 删除Html文件
File.Delete( htmlFilePath.ToString());
foreach 字符串文件 in Directory.GetFiles(directoryPath))
{
File.Delete(file);
}
Directory.Delete(directoryPath);
Response.End();
}











ERROR SHOWING ::

1.类型'Microsoft.Office.Interop.Word.ApplicationClass'没有定义构造函数

2.'Microsoft.Office.Interop。 Word.ApplicationClass'不包含'Documents'的定义,也没有扩展方法'Documents'接受类型为'Microsoft.Office.Interop.Word.ApplicationClass'的第一个参数(你是否缺少using指令或程序集)引用?)

3.'Microsoft.Office.Interop.Word.ApplicationClass'不包含'Documents'的定义,也没有扩展方法'Documents'接受类型'Microsoft.Office的第一个参数可以找到.Interop.Word.ApplicationClass'(您是否缺少using指令或程序集引用?)

5.Interop类型'Microsoft.Office.Interop.Word.ApplicationClass'无法嵌入。请改用适用的界面。

6.Interop类型'Microsoft.Office.Interop.Word.ApplicationClass'无法嵌入。请改用适用的界面。



请告诉我哪里错了????

解决方案

请阅读Microsoft解释为什么不建议在服务器端设置中使用Office互操作:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2 [ ^ ],

http://support.microsoft.com/kb/257757/en-us [ ^ ]。



相反,你可以使用Microsoft Open XML SDK。请参阅本文中引用的过去的答案:如何从MS Visual中的添加参考添加microsoft excel 15.0对象库Studio 2010 [ ^ ] 。



-SA


http://www.aspsnippets.com/Articles/Display-Word-document-on-web-page-in -ASP.Net.aspx [ ^ ]


i am using VS13 and sql server.i want to preview a word file when it upload in the database and download from the detabase.i have already use some code but there are some error.
code and error given below---i have use Technical Refence from http://www.aspsnippets.com/Articles/Display-Word-document-on-web-page-in-ASP.Net.aspx[^]

SETP 1: Added reference of Microsoft Word 11.0 Object Library
STEP 2:

using Microsoft.Office;
using Microsoft.Office.Interop.Word;
using System.IO;


STEP 3:

protected void Upload(object sender, EventArgs e)
{
    object missingType = Type.Missing;
    object readOnly = true;
    object isVisible = false;
    object documentFormat = 8;
    string randomName = DateTime.Now.Ticks.ToString();
    object htmlFilePath = Server.MapPath("~/Temp/") + randomName + ".htm";
    string directoryPath = Server.MapPath("~/Temp/") + randomName + "_files";
 
    //Upload the word document and save to Temp folder
    FileUpload1.SaveAs(Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName));
    object fileName = FileUpload1.PostedFile.FileName;
 
    //Open the word document in background
    ApplicationClass applicationclass = new ApplicationClass();
    applicationclass.Documents.Open(ref fileName,
                                    ref readOnly,
                                    ref missingType, ref missingType, ref missingType,
                                    ref missingType, ref missingType, ref missingType,
                                    ref missingType, ref missingType, ref isVisible,
                                    ref missingType, ref missingType, ref missingType,
                                    ref missingType, ref missingType);
    applicationclass.Visible = false;
    Document document = applicationclass.ActiveDocument;
 
    //Save the word document as HTML file
    document.SaveAs(ref htmlFilePath, ref documentFormat, ref missingType,
                    ref missingType, ref missingType, ref missingType,
                    ref missingType, ref missingType, ref missingType,
                    ref missingType, ref missingType, ref missingType,
                    ref missingType, ref missingType, ref missingType,
                    ref missingType);
       
    //Close the word document
    document.Close(ref missingType, ref missingType, ref missingType);
 
    //Delete the Uploaded Word File
    File.Delete(Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName));
 
    //Read the Html File as Byte Array and Display it on browser
    byte[] bytes;
    using (FileStream fs = new FileStream(htmlFilePath.ToString(), FileMode.Open, FileAccess.Read))
    {
        BinaryReader reader = new BinaryReader(fs);
        bytes = reader.ReadBytes((int)fs.Length);
        fs.Close();
    }
    Response.BinaryWrite(bytes);
    Response.Flush();
 
    //Delete the Html File
    File.Delete(htmlFilePath.ToString());
    foreach (string file in Directory.GetFiles(directoryPath))
    {
        File.Delete(file);
    }
    Directory.Delete(directoryPath);
    Response.End();
}






ERROR SHOWING::
1.The type 'Microsoft.Office.Interop.Word.ApplicationClass' has no constructors defined
2.'Microsoft.Office.Interop.Word.ApplicationClass' does not contain a definition for 'Documents' and no extension method 'Documents' accepting a first argument of type 'Microsoft.Office.Interop.Word.ApplicationClass' could be found (are you missing a using directive or an assembly reference?)
3.'Microsoft.Office.Interop.Word.ApplicationClass' does not contain a definition for 'Documents' and no extension method 'Documents' accepting a first argument of type 'Microsoft.Office.Interop.Word.ApplicationClass' could be found (are you missing a using directive or an assembly reference?)
5.Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the applicable interface instead.
6.Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the applicable interface instead.

Please tell me whats wrong????

解决方案

Please read the Microsoft explanation why Office interop is not recommended in server-side settings:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2[^],
http://support.microsoft.com/kb/257757/en-us[^].

Instead, you can use Microsoft Open XML SDK. Please see my past answered referenced in this one: How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

—SA


http://www.aspsnippets.com/Articles/Display-Word-document-on-web-page-in-ASP.Net.aspx[^]


这篇关于如何在网页中预览Word文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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