回覆列表
  • 1 # 使用者990540253822

    頁面程式碼:

    [html] view plaincopy

    Choose File

    C#程式碼:

    [csharp] view plaincopy

    using System;

    using System.Data;

    using System.Configuration;

    using System.Collections;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using System.IO;

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    ///

    /// 將word轉換為Html

    ///

    ///

    ///

    protected void btnConvert_Click(object sender, EventArgs e)

    {

    try

    {

    //上傳

    //uploadWord(File1);

    //轉換

    wordToHtml(File1);

    }

    catch (Exception ex)

    {

    throw ex;

    }

    finally

    {

    Response.Write("恭喜,轉換成功!");

    }

    }

    //上傳檔案並轉換為html wordToHtml(wordFilePath)

    ///

    ///上傳檔案並轉存為html

    ///

    ///word文件在客戶機的位置

    ///上傳的html檔案的地址

    public string wordToHtml(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 = uploadWord(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 filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +

    System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();

    // 判斷指定目錄下是否存在資料夾,如果不存在,則建立

    if (!Directory.Exists(Server.MapPath("~\\html")))

    {

    // 建立up資料夾

    Directory.CreateDirectory(Server.MapPath("~\\html"));

    }

    //被轉換的html文件儲存的位置

    string ConfigPath = HttpContext.Current.Server.MapPath("html/" + filename + ".html");

    object saveFileName = ConfigPath;

    /*下面是Microsoft Word 9 Object Library的寫法,如果是10,可能寫成:

    * docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,

    * null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});

    * 其它格式:

    * 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, new object[] { null, null, null });

    // 退出 Word

    wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);

    //轉到新生成的頁面

    return ("/" + filename + ".html");

    }

    public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)

    {

    if (uploadFiles.PostedFile != null)

    {

    string fileName = uploadFiles.PostedFile.FileName;

    int extendNameIndex = fileName.LastIndexOf(".");

    string extendName = fileName.Substring(extendNameIndex);

    string newName = "";

    try

    {

    //驗證是否為word格式

    if (extendName == ".doc" || extendName == ".docx")

    {

    DateTime now = DateTime.Now;

    newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString();

    // 判斷指定目錄下是否存在資料夾,如果不存在,則建立

    if (!Directory.Exists(Server.MapPath("~\\wordTmp")))

    {

    // 建立up資料夾

    Directory.CreateDirectory(Server.MapPath("~\\wordTmp"));

    }

    //上傳路徑 指當前上傳頁面的同一級的目錄下面的wordTmp路徑

    uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));

    }

    else

    {

    return "1";

    }

    }

    catch

    {

    return "0";

    }

    //return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName;

    return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);

    }

    else

    {

    return "0";

    }

    }

  • 中秋節和大豐收的關聯?
  • 大師兄坦言:保持狀態,我和Uzi一檔了,粉絲:你都進S9了,他呢,RNG能進S9嗎?