C#將Word或Excel文檔轉換為Html文件
2022-06-18 17:57:45 來源:易采站長站 作者:
這個是CodeProject上的一篇文章:Microsoft Interop API to convert the .doc, .docx, .dot, .dotx and .xls,.xlsx, .rtf to HTML。該文介紹了一種通過Microsoft office Interop library轉換word或excel文檔為html的方法,這里轉錄一下,以供更多需要的人參考。
要使用Microsoft office Interop library庫,首先得在電腦上安裝Office,然后添加如下三個com組件的引用:
Microsoft Office Excel library.
Microsoft Office Word library
Microsoft Office object library
作者編寫了兩個類DocToHtml和XlsToHtml用以轉換Word和Excel文檔。
public static IConverter Converter(string fullFilePath, string fileToSave) { switch (Path.GetExtension(fullFilePath).ToLower()) { case ".doc": case ".docx": case ".dot": case ".dotx": case ".rtf": return new DocToHtml { FileToSave = fileToSave, FullFilePath = fullFilePath }; case ".xls": case ".xlsx": return new XlsToHtml { FileToSave = fileToSave, FullFilePath = fullFilePath }; default: throw new NotSupportedException(); } }
使用方法如下:
static void Main(string[] args) { var converter = ConverterLocator.Converter(@"r:\1.xlsx", @"r:\1.html"); var html = converter.Convert(); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持易采站長站。
如有侵權,請聯系QQ:279390809 電話:15144810328
最新圖文推薦
相關文章
-
使用Visual Studio2019創建C#項目(窗體應用程序、控制臺應用程序、
一、VS的開發環境 首先你得安裝了vs2019,然后確認下下面三個組件是否存在,如果沒有要下載一下。vs2019的安裝可參考visual studio2019的安裝以及使用。 二、創建C#窗體應用程序 打開vs 可2020-03-08