国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#.NET實現Word或Excel文件轉為HTML文件

2019-11-17 02:35:30
字體:
來源:轉載
供稿:網友

C#.NET實現WordExcel文件轉為HTML文件

Word文件轉html,返回相對路徑

 1 PRivate string GetPathByDocToHTML(string strFile) 2     { 3         if (string.IsNullOrEmpty(strFile)) 4         { 5             return "0";//沒有文件 6         } 7  8         Microsoft.Office.Interop.Word.applicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass(); 9         Type wordType = word.GetType();10         Microsoft.Office.Interop.Word.Documents docs = word.Documents;11 12         // 打開文件  13         Type docsType = docs.GetType();14 15         object fileName = strFile;16 17         Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",18         System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });19 20         // 轉換格式,另存為html  21         Type docType = doc.GetType();22         //給文件重新起名23         string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +24         System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();25 26         string strFileFolder = "../html/";27         DateTime dt = DateTime.Now;28         //以yyyymmdd形式生成子文件夾名29         string strFileSubFolder = dt.Year.ToString();30         strFileSubFolder += (dt.Month < 10) ? ("0" + dt.Month.ToString()) : dt.Month.ToString();31         strFileSubFolder += (dt.Day < 10) ? ("0" + dt.Day.ToString()) : dt.Day.ToString();32         string strFilePath = strFileFolder + strFileSubFolder + "/";33         // 判斷指定目錄下是否存在文件夾,如果不存在,則創建 34         if (!Directory.Exists(Server.MapPath(strFilePath)))35         {36             // 創建up文件夾 37             Directory.CreateDirectory(Server.MapPath(strFilePath));38         }39 40         //被轉換的html文檔保存的位置 41         // HttpContext.Current.Server.MapPath("html" + strFileSubFolder + filename + ".html")42         string ConfigPath = Server.MapPath(strFilePath + filename + ".html");43         object saveFileName = ConfigPath;44 45         /*下面是Microsoft Word 9 Object Library的寫法,如果是10,可能寫成: 46           * docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, 47           * null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFiltereDHTML}); 48           * 其它格式: 49           * wdFormatHTML 50           * wdFormatDocument 51           * wdFormatDOSText 52           * wdFormatDOSTextLineBreaks 53           * wdFormatEncodedText 54           * wdFormatRTF 55           * wdFormatTemplate 56           * wdFormatText 57           * wdFormatTextLineBreaks 58           * wdFormatUnicodeText 59         */60         docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,61         null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });62 63         //docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,64         //  null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML }); 65 66         //關閉文檔  67         docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,68         null, doc, new object[] { null, null, null });69 70         // 退出 Word  71         wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);72         //轉到新生成的頁面  73         //return ("/" + filename + ".html");74 75         //轉化HTML頁面統一編碼格式76         TransHTMLEncoding(ConfigPath);77 78         return (strFilePath + filename + ".html");79     }

Excel文件轉HTML,返回相對路徑

 1 private string GetPathByXlsToHTML(string strFile) 2     { 3         if (string.IsNullOrEmpty(strFile)) 4         { 5             return "0";//沒有文件 6         } 7  8         //實例化Excel   9         Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();10         Microsoft.Office.Interop.Excel.Workbook workbook = null;11         Microsoft.Office.Interop.Excel.Worksheet worksheet = null;12 13         //打開文件,n.FullPath是文件路徑  14         workbook = repExcel.Application.Workbooks.Open(strFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);15         worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];16 17         //給文件重新起名18         string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +19         System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();20 21         string strFileFolder = "../html/";22         DateTime dt = DateTime.Now;23         //以yyyymmdd形式生成子文件夾名24         string strFileSubFolder = dt.Year.ToString();25         strFileSubFolder += (dt.Month < 10) ? ("0" + dt.Month.ToString()) : dt.Month.ToString();26         strFileSubFolder += (dt.Day < 10) ? ("0" + dt.Day.ToString()) : dt.Day.ToString();27         string strFilePath = strFileFolder + strFileSubFolder + "/";28         // 判斷指定目錄下是否存在文件夾,如果不存在,則創建 29         if (!Directory.Exists(Server.MapPath(strFilePath)))30         {31             // 創建up文件夾 32             Directory.CreateDirectory(Server.MapPath(strFilePath));33         }34         string ConfigPath = Server.MapPath(strFilePath + filename + ".html");35         object savefilename = (object)ConfigPath;36 37         object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;38         //進行另存為操作    39         workbook.SaveAs(savefilename, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsaccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);40         object osave = false;41         //逐步關閉所有使用的對象  42         workbook.Close(osave, Type.Missing, Type.Missing);43         repExcel.Quit();44         System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);45         worksheet = null;46         //垃圾回收  47         GC.Collect();48         System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);49         workbook = null;50         GC.Collect();51         System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel.Application.Workbooks);52         GC.Collect();53         System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel);54         repExcel = null;55         GC.Collect();56         //依據時間殺滅進程  57         System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("EXCEL");58         foreach (System.Diagnostics.Process p in process)59         {60             if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5)61             {62                 p.Kill();63             }64         }65 66         return (strFilePath + filename + ".html");67     }

這里可能會遇到一個問題,由于轉化為HTML文件的頁面編碼可能使得瀏覽器無法正確解讀,所以需要轉碼,轉換代碼如下:

 1     private void TransHTMLEncoding(string strFilePath) 2     { 3         try 4         { 5             System.IO.StreamReader sr = new System.IO.StreamReader(strFilePath, Encoding.GetEncoding(0)); 6             string html = sr.ReadToEnd(); 7             sr.Close(); 8             html = System.Text.RegularExpressions.Regex.Replace(html, @"<meta[^>]*>", "<meta http-equiv=Content-Type content='text/html; charset=gb2312'>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); 9             System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false, Encoding.Default);10 11             sw.Write(html);12             sw.Close();13         }14         catch (Exception ex)15         {16             Page.RegisterStartupScr

這樣就可以正常在頁面上正常顯示了


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 花莲市| 日土县| 澄迈县| 腾冲县| 葫芦岛市| 盈江县| 东乌珠穆沁旗| 万盛区| 池州市| 哈巴河县| 衡阳县| 中江县| 武冈市| 象山县| 平陆县| 满城县| 大新县| 东平县| 张北县| 上林县| 勃利县| 阿合奇县| 苏尼特右旗| 宜良县| 南雄市| 固原市| 淮南市| 灵川县| 常宁市| 萍乡市| 牟定县| 乌苏市| 沿河| 竹溪县| 新巴尔虎左旗| 汕头市| 平江县| 黄陵县| 保定市| 乌拉特后旗| 紫金县|