也谈使用ASP.NET上传Word文件至服务器,并转成HTML。

      一直以来想在网站里实现直接将Word文件上传并自动转换成Html文件(适当过滤比较废话的代码),在网上搜了,有不少结果,但真正能用的去没有几个,也不知道是哪位能人的代码,被到处Copy了。

 

网上找来的基本上都是这个样子:

//创建word实例

02Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
03Type wordType = word.GetType();
04//文档s
05Microsoft.Office.Interop.Word.Documents docs = word.Documents;
06Type docsType = docs.GetType();
07//打开文档
08Microsoft.Office.Interop.Word.Document doc = (Document)docsType.InvokeMember("Open",System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] {fileName, true, true});
09Type doctype = doc.GetType();
10//另存为
11doctype.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc,new object[] {savefilename, WdSaveFormat.wdFormatHTML}); //另存为Html格式
12//关闭
13wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
经本人测试基本没什么用。后来几经摸索,终于将基本功能实现了,请大家看看还有什么问题不??
1、到 http://www.dllzj.com/下载Microsoft.Office.Interop.Word.dll,有各种版本的,我用的是11.0(Word2003)的;
2、添加引用,VS会自动拷贝一份到bin文件夹;
3、实现html代码;
<html xmlns=" http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ASP.NET Word 文件上传于转换 Demo</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <input id="File1" type="file" runat="server" />
        <br />
        <br />
        <asp:Button ID="btnUpload" runat="server" Text="上传转换" οnclick="btnUpload_Click" />
     </div>
    </form>
</body>
</html>
4、C#的代码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    /// 上传文件并转存为htm
    public string Doc2Html(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
    {
        Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
        Type wordType = word.GetType();
        Microsoft.Office.Interop.Word.Documents docs = word.Documents;
        // 打开文件
        Type docsType = docs.GetType();
        //应当先把文件上传至服务器然后再解析文件为html
        string filePath = uploadDoc(wordFilePath);
        //判断是否上传文件成功
        if (filePath == "0")
            return "0";
        //判断是否为word文件
        if (filePath == "1")
            return "1";
        object fileName = filePath;
        Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
        System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });
        // 转换格式,另存为html
        Type docType = doc.GetType();
        string strfileName = fileName.ToString();
        //获取转换文件的文件名
        int fullNameIndex = strfileName.LastIndexOf("//");
        string filename = strfileName.Substring(fullNameIndex);
        string pattern = @"(.doc)";
        filename = System.Text.RegularExpressions.Regex.Replace(filename,pattern,String.Empty);
 
        //被转换的html文档保存的位置
        string ConfigPath = HttpContext.Current.Server.MapPath("Html/" + filename + ".html");
        object saveFileName = ConfigPath;
        /*其它格式:
        * wdFormatHTML
        * wdFormatDocument
        * wdFormatDOSText
        * wdFormatDOSTextLineBreaks
        * wdFormatEncodedText
        * wdFormatRTF
        * wdFormatTemplate
        * wdFormatText
        * wdFormatTextLineBreaks
        * wdFormatUnicodeText
        */
        docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
        null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
        docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
        wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        //转到新生成的页面
        return ("Html/" + filename + ".html");
    }
    public string uploadDoc(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)
    {
        if (uploadFiles.PostedFile != null)
        {
            string fileName = uploadFiles.PostedFile.FileName;
            string fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
            string newfileName = "";
            try
            {
                //验证是否为word格式
                if (fileExtension == ".doc")
                {
                    DateTime now = DateTime.Now;
                    newfileName = now.Year.ToString() + now.Month.ToString() + now.Day.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString();
                    //上传路径 指当前上传页面的同一级的目录下面的Doc路径
                    uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("Doc/" + newfileName + fileExtension));
                }
                else
                {
                    return "1";
                }
            }
            catch
            {
                return "0";
            }
            return System.Web.HttpContext.Current.Server.MapPath("Doc/" + newfileName + fileExtension);
        }
        else
        {
            return "0";
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            Doc2Html(File1);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            Response.Write("恭喜,转换成功!");
        }
    }
}
可以参考的一些文章:
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值