C#将word转换为HTML格式

假如C盘有个简历,AA.doc,我把它转化为html
InBlock.gifStringBuilder sb= new StringBuilder();
InBlock.gif                        Microsoft.Office.Interop.Word.ApplicationClass appclass = new Microsoft.Office.Interop.Word.ApplicationClass(); //实例化一个Word
InBlock.gif                        Type wordtype = appclass.GetType();
InBlock.gif                        Microsoft.Office.Interop.Word.Documents docs = appclass.Documents; //获取Document
InBlock.gif                        Type docstype = docs.GetType();
InBlock.gif                         object filename = @"C:\AA.doc"; //Word文件的路径
InBlock.gif                        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 }); //打开文件
InBlock.gif                        Type doctype = doc.GetType();
InBlock.gif                         object savefilename = @"C:\bb.html"; //生成HTML的路径和名子
InBlock.gif                        doctype.InvokeMember( "SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { savefilename, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML }); //另存为Html格式
InBlock.gif                        wordtype.InvokeMember( "Quit", System.Reflection.BindingFlags.InvokeMethod, null, appclass, null); //退出
InBlock.gif
InBlock.gif                        Thread.Sleep(3000); //为了使退出完全,这里阻塞3秒
InBlock.gif                        StreamReader objreader = new StreamReader(savefilename.ToString(), System.Text.Encoding.GetEncoding( "GB2312"));     //以下内容是为了在Html中加入对本身Word文件的下载                
InBlock.gif                        FileStream fs = new FileStream(savefilename.ToString().Split('.').GetValue(0).ToString() + "$.html", FileMode.Create);
InBlock.gif
InBlock.gif                        StreamWriter streamHtmlHelp = new System.IO.StreamWriter(fs, System.Text.Encoding.GetEncoding( "GB2312"));
InBlock.gif                         //streamHtmlHelp.WriteLine("<a href='E:\\AA.html'>源文件下载</a><br>");
InBlock.gif                         string str = "";
InBlock.gif                         do
InBlock.gif                        {
InBlock.gif                                str = objreader.ReadLine();
InBlock.gif                                sb.Append(str+ "\n");
InBlock.gif                                sb.Replace( "<Html>","");
InBlock.gif                                sb.Replace( "<body>", "");
InBlock.gif                                streamHtmlHelp.WriteLine(str);
InBlock.gif                        }
InBlock.gif                         while (str != "</html>");
InBlock.gif                        streamHtmlHelp.Close();
InBlock.gif                        objreader.Close();
InBlock.gif                        msg.InnerHtml = sb.ToString() ;
InBlock.gif                        
InBlock.gif                        File.Delete(savefilename.ToString());
InBlock.gif                        File.Move(savefilename.ToString().Split('.').GetValue(0).ToString() + "$.html", savefilename.ToString());
结果如下
生成了两个文件,一个是html文件,一个是html文件所要用的图片,也就是word中的图片
我们打开html文件
生成了这么多代码
这是生成的html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值