是否有可能在asp.net页面中显示(或编辑不那么重要)MS Word文件。 [英] Is it possible to show (or edit not so important) MS Word file in asp.net page.

查看:97
本文介绍了是否有可能在asp.net页面中显示(或编辑不那么重要)MS Word文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些关于如何在.aspx页面中使用C#

显示.doc或.docx文件的好教程,任何第三方api,软件或其他。我需要这个用于教育目的所以解决方案必须是免费的。我通过Aspose,Dev Express和Text Control这些都是付费的。并没有在Google Drive Api(Google Docs)上找到一个很好的完整教程,因为我不知何故新手..

解决方案

你需要至少谷歌一次你的问题,那里超过100万解决这些问题的解决方案已经解决并准备好为您服务。



一个简单的Google给出了这个,CodeProject上的一篇文章解释了在C#中将DOC文件转换为其他格式。



http://www.codeproject.com/Articles/5273/How-to-convert-DOC-into-other-formats-using-C



由于您正在使用ASP.NET,因此ASP.NET的更通用的文章或代码示例将非常适合您。这是ASP.NET中另一篇同样的文章。



http://www.codeproject.com/Articles/517456/Upload-and-convert- PowerPoint-Excel-Visio-Word-to



祝你好运!


请参阅我对解决方案1的评论。



另请参阅本文中引用的过去的答案如何从MS Visual Studio 2010中的添加引用添加microsoft excel 15.0对象库 [ ^ ]。



-SA


< b> C#代码



<前lang =c#> 受保护 void show_merge_click( 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文档
应用程序应用程序类= new Microsoft.Office.Interop.Word.Application();
applicationclass.Documents.Open( @ D:\\ Word \\ Merge_File_transfer.doc );

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 );

// 将Html文件读取为字节数组并在浏览器中显示
byte [] bytes;
使用(FileStream fs = new FileStream(htmlFilePath.ToString(),FileMode.Open, FileAccess.ReadWrite))
{
BinaryReader reader = new BinaryReader(fs);
bytes = reader.ReadBytes(( int )fs.Length);
abc = System.Text.Encoding.Default.GetString(bytes);

fs.Close();
}

ifrm.InnerHtml = abc;

}



Aspx代码



< pre lang =HTML> < asp:panel id = pnlContents runat = server xmlns:asp = #unknown >
< asp:literal id = FrameLiteral runat = server / >
< ; div id < span class =code-keyword> = ifrm runat = server width = 700px height = 473 marginwidth = 0 marginheight = 0 frameborder = no 滚动 = style = border-width:2px;边框颜色:#333;背景:#FFF; border-style:inset >
< / div >
< / asp:panel >


I Need some good tutorial on how to show .doc or .docx file in .aspx page using C#
any third party apis, software or else. I need this for educational purpose so solution must be free. I go through Aspose, Dev Express and Text Control these are all paid. And not found a good complete tutorial on Google Drive Api (Google Docs) as I'm somehow newbie..

解决方案

You need to Google your problems atleast once, there are more than 1million solutions to such problems already solved and ready to work for you.

A simple Google gave this, an article at CodeProject that explains the conversion of a DOC file to other formats in C#.

http://www.codeproject.com/Articles/5273/How-to-convert-DOC-into-other-formats-using-C

Since you're working with ASP.NET, a more generic article or code sample for ASP.NET instead of C# would be great for you. Here is another article for the same thing, in ASP.NET.

http://www.codeproject.com/Articles/517456/Upload-and-convert-PowerPoint-Excel-Visio-Word-to

Good luck!


Please see my comments to Solution 1.

See also my past answers referenced in this one How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

—SA


C# Code

protected void show_merge_click(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";
 
        //Open the word document in background
        Application applicationclass = new Microsoft.Office.Interop.Word.Application();
        applicationclass.Documents.Open(@"D:\\Word\\Merge_File_transfer.doc");
        
        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);

        //Read the Html File as Byte Array and Display it on browser
        byte[] bytes;
        using (FileStream fs = new FileStream(htmlFilePath.ToString(), FileMode.Open, FileAccess.ReadWrite))
        {
            BinaryReader reader = new BinaryReader(fs);
            bytes = reader.ReadBytes((int)fs.Length);
            abc = System.Text.Encoding.Default.GetString(bytes);

            fs.Close();
        }

      ifrm.InnerHtml = abc;

    }


Aspx code

<asp:panel id="pnlContents" runat="server" xmlns:asp="#unknown">
<asp:literal id="FrameLiteral" runat="server" />
<div id="ifrm" runat="server" width="700px" height="473" marginwidth="0" marginheight="0" frameborder="no" scrolling="yes" style="border-width:2px; border-color:#333; background:#FFF; border-style:inset ">
</div>
</asp:panel>


这篇关于是否有可能在asp.net页面中显示(或编辑不那么重要)MS Word文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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