初学C#,使用ASP.NET写了个上传Word直接生成Html文件的Demo,感谢网络请测成功。

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
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;

        //打开一个Word文档
        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();

        //获取转换文件的文件名,与Doc文件同名
        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;

        docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
        null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
        //一定要先关闭,否则退出Word时不能释放资源
        docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
        //退出Word
        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" || fileExtension == "docx")
                {
                    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)
    {
        if (File1.PostedFile.ContentLength > 0)
        {
            try
            {
                Doc2Html(File1);
            }
            catch (Exception exc)
            {
                 Response.Write("Error:<br />" + exc.ToString() + ".");
            }
            finally
            {
                Response.Write("恭喜,转换成功!");
                Repeater1.DataBind();
            }
        }
    }
}

posted @ 2011-06-22 21:14  kissu  阅读(1565)  评论(4编辑  收藏  举报