• 如何把rtf、doc文件转换为HTML文件


    //retText是路径
    1
    public string ExtractHtml(string rtfText) 2 { 3 try 4 { 5 //Create word object 6 Word.Application applicationObject = new Word.Application(); 7 Type wordType = applicationObject.GetType(); 8 9 //define path for save your temporary file. 10 string userTemp = @"F:CodeDisplayPrintfileDisplayPrintfilefile"; 11 //Open and save your rtf as HTML in your temp path. 12 object missing = Type.Missing; 13 object fileName = rtfText; 14 object False = false; 15 object rtfFileFormt = Word.WdSaveFormat.wdFormatRTF; 16 applicationObject.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone; 17 18 Word.Document documentObject =applicationObject.Documents.Open(ref fileName, ref missing, ref missing, ref missing, 19 ref missing, ref missing, ref missing, ref missing, ref missing, 20 ref missing, ref missing, ref False, ref missing, ref missing, 21 ref missing, ref missing); 22 23 object tempFileName =@"F:CodeDisplayPrintfileDisplayPrintfilefile empHtm.html"; 24 object fileFormt = Word.WdSaveFormat.wdFormatHTML; 25 object makeFalse = false; 26 object makeTrue = true; 27 string absolutePath = tempFileName.ToString(); 28 if (File.Exists(absolutePath)) 29 { 30 try 31 { 32 File.Delete(absolutePath); 33 } 34 catch { } 35 } 36 37 documentObject.SaveAs(ref tempFileName, ref fileFormt, 38 ref makeFalse, ref missing, ref makeFalse, 39 ref missing, ref missing, ref missing, ref makeFalse, ref missing, ref missing, 40 ref missing, ref missing, ref missing, ref missing, ref missing); 41 GC.Collect(); 42 GC.WaitForPendingFinalizers(); 43 documentObject.Close(ref makeFalse, ref missing, ref missing); 44 GC.Collect(); 45 GC.WaitForPendingFinalizers(); 46 //File.Delete(rtfText); 47 48 String htmlCode = ""; 49 50 //Extract html source from the temporary html file. 51 if (File.Exists(absolutePath)) 52 { 53 WebClient client = new WebClient(); 54 htmlCode = client.DownloadString(absolutePath); 55 GC.Collect(); 56 GC.WaitForPendingFinalizers(); 57 try 58 { 59 File.Delete(absolutePath); 60 } 61 catch { } 62 } 63 64 else 65 { 66 htmlCode = ""; 67 } 68 69 return htmlCode; 70 } 71 catch 72 { 73 return ""; 74 } 75 }

    demo: 下载

  • 相关阅读:
    linux常用命令之--用户与用户组管理命令
    linux常用命令之--文本编辑和文本内容查看命令
    linux常用命令之--目录与文件的操作命令
    linux的文件属性介绍、目录及路径表示方法
    scalatest的userguide
    SBT的用法
    性能工具列表
    ruby不能识别中文的一个坑
    基于Calabash-andriod的UI自动化测试(1)-环境和原理
    scala速成记录1
  • 原文地址:https://www.cnblogs.com/caosenianhuan/p/3158580.html
Copyright © 2020-2023  润新知