问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingMicrosoft.Office.Core;usingWord=Microsoft.Office.Interop.Word;namespaceCode{publicclassOfficeHtmlHelper{///<summary>///Word转成Html///</summary>///<paramname="path">要转换的文档的路径</param>///<paramname="savePath">转换成html的保存路径</param>///<paramname="wordFileName">转换成html的文件名字</param>publicstaticvoidWord2Html(stringpath,stringsavePath,stringwordFileName){Word.ApplicationClassword=newWord.ApplicationClass();TypewordType=word.GetType();Word.Documentsdocs=word.Documents;TypedocsType=docs.GetType();Word.Documentdoc=(Word.Document)docsType.InvokeMember("Open",System.Reflection.BindingFlags.InvokeMethod,null,docs,newObject[]{(object)path,true,true});TypedocType=doc.GetType();stringstrSaveFileName=savePath+wordFileName+".html";objectsaveFileName=(object)strSaveFileName;docType.InvokeMember("SaveAs",System.Reflection.BindingFlags.InvokeMethod,null,doc,newobject[]{saveFileName,Word.WdSaveFormat.wdFormatFilteredHTML});docType.InvokeMember("Close",System.Reflection.BindingFlags.InvokeMethod,null,doc,null);wordType.InvokeMember("Quit",System.Reflection.BindingFlags.InvokeMethod,null,word,null);}///<summary>///Excel转成Html///</summary>///<paramname="path">要转换的文档的路径</param>///<paramname="savePath">转换成html的保存路径</param>///<paramname="wordFileName">转换成html的文件名字</param>publicstaticvoidExcel2Html(stringpath,stringsavePath,stringwordFileName){stringstr=string.Empty;Microsoft.Office.Interop.Excel.ApplicationrepExcel=newMicrosoft.Office.Interop.Excel.Application();Microsoft.Office.Interop.Excel.Workbookworkbook=null;Microsoft.Office.Interop.Excel.Worksheetworksheet=null;workbook=repExcel.Application.Workbooks.Open(path,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);worksheet=(Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];objecthtmlFile=savePath+wordFileName+".html";objectofmt=Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;workbook.SaveAs(htmlFile,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);objectosave=false;workbook.Close(osave,Type.Missing,Type.Missing);repExcel.Quit();}///<summary>///ppt转成Html///</summary>///<paramname="path">要转换的文档的路径</param>///<paramname="savePath">转换成html的保存路径</param>///<paramname="wordFileName">转换成html的文件名字</param>publicstaticvoidPPT2Html(stringpath,stringsavePath,stringwordFileName){Microsoft.Office.Interop.PowerPoint.ApplicationppApp=newMicrosoft.Office.Interop.PowerPoint.Application();stringstrSourceFile=path;stringstrDestinationFile=savePath+wordFileName+".html";Microsoft.Office.Interop.PowerPoint.PresentationprsPres=ppApp.Presentations.Open(strSourceFile,Microsoft.Office.Core.MsoTriState.msoTrue,Microsoft.Office.Core.MsoTriState.msoFalse,Microsoft.Office.Core.MsoTriState.msoFalse);prsPres.SaveAs(strDestinationFile,Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML,MsoTriState.msoTrue);prsPres.Close();ppApp.Quit();}}}
先把类放上来。这个类可以用,但是转化成的内容都集中在左边了,丢失了好多word中的格式,求怎么样才能保存下来格式呢?
解决方案
解决方案二:
多少格式会丢失或变化一些的。我以前做的word转换html后,有的表格格式都乱了,还得再微调一下才行。
解决方案三:
引用1楼yaotomo的回复:
多少格式会丢失或变化一些的。我以前做的word转换html后,有的表格格式都乱了,还得再微调一下才行。
具体要怎么操作呢?对IO操作实在不怎么了解
解决方案四:
尝试另存为mht看看,因为光存html是会丢失样式表的。
解决方案五:
引用2楼as198645的回复:
具体要怎么操作呢?对IO操作实在不怎么了解
我是打开html手工修改的我也不知道怎么通过代码生成完美的html页面。
解决方案六:
引用3楼caozhy的回复:
尝试另存为mht看看,因为光存html是会丢失样式表的。
我是在上传一个word为附件的时候同时调用这个类来处理如果说采用另存为的话,代码要怎么实现呢
解决方案七:
有没人知道呢?继续求助!!
解决方案八:
objectformat=8;//WordDoc.Content.TextWordDoc.SaveAs(ref(object)aimFileName,refformat,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing);
解决方案九:
表格样式会乱的
解决方案十:
引用8楼uppaway的回复:
表格样式会乱的
对,但是我想知道如何避免,类似QQ邮箱的那种,至少能看,我现在的内容全挤在左边,太难看了
解决方案十一:
把Word转化成html的实例privatevoidbtn_SaveAs_Click(objectsender,EventArgse){btn_SaveAs.Enabled=false;//停用转换按钮try{G_wa.ActiveDocument.Save();//保存文档((Word._Application)G_wa.Application).Quit(//退出应用程序refG_missing,refG_missing,refG_missing);}catch(Exceptionex){Console.WriteLine(ex.Message);}//CodeGo.net/SaveFileDialogP_SaveFileDialog=//创建保存文件对话框对象newSaveFileDialog();P_SaveFileDialog.Filter="*.html|*.html";//筛选文件扩展名DialogResultP_DialogResult=//打开保存文件对话框P_SaveFileDialog.ShowDialog();if(P_DialogResult==DialogResult.OK)//判断是否确认保存文件{objectP_str_path=P_SaveFileDialog.FileName;//创建object对象ThreadPool.QueueUserWorkItem(//开始线程澉(pp)=>//使用Lambda表达式{G_wa=//创建应用程序对象newMicrosoft.Office.Interop.Word.Application();G_wa.Visible=false;Word.DocumentP_wd=G_wa.Documents.Open(//打开Word文档refG_FilePath,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing);objectP_Format=Word.WdSaveFormat.wdFormatHTML;//创建保存文档参数P_wd.SaveAs(//保存Word文件refP_str_path,refP_Format,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing);((Word._Application)G_wa.Application).Quit(//退出应用程序refG_missing,refG_missing,refG_missing);this.Invoke(//调用窗体线程(MethodInvoker)(()=>//使用lambda表达式{btn_Open.Enabled=true;//启用打开按钮btn_New.Enabled=true;//启用新建按钮MessageBox.Show(//提示已经创建Word"文件已经创建","提示!");}));});}}
解决方案十二:
试试这个呢
解决方案十三:
引用10楼kangda_2467974115的回复:
把Word转化成html的实例privatevoidbtn_SaveAs_Click(objectsender,EventArgse){btn_SaveAs.Enabled=false;//停用转换按钮try{G_wa.ActiveDocument.Save();//保存文档((Word._Application)G_wa.Application).Quit(//退出应用程序refG_missing,refG_missing,refG_missing);}catch(Exceptionex){Console.WriteLine(ex.Message);}//CodeGo.net/SaveFileDialogP_SaveFileDialog=//创建保存文件对话框对象newSaveFileDialog();P_SaveFileDialog.Filter="*.html|*.html";//筛选文件扩展名DialogResultP_DialogResult=//打开保存文件对话框P_SaveFileDialog.ShowDialog();if(P_DialogResult==DialogResult.OK)//判断是否确认保存文件{objectP_str_path=P_SaveFileDialog.FileName;//创建object对象ThreadPool.QueueUserWorkItem(//开始线程澉(pp)=>//使用Lambda表达式{G_wa=//创建应用程序对象newMicrosoft.Office.Interop.Word.Application();G_wa.Visible=false;Word.DocumentP_wd=G_wa.Documents.Open(//打开Word文档refG_FilePath,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing);objectP_Format=Word.WdSaveFormat.wdFormatHTML;//创建保存文档参数P_wd.SaveAs(//保存Word文件refP_str_path,refP_Format,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing,refG_missing);((Word._Application)G_wa.Application).Quit(//退出应用程序refG_missing,refG_missing,refG_missing);this.Invoke(//调用窗体线程(MethodInvoker)(()=>//使用lambda表达式{btn_Open.Enabled=true;//启用打开按钮btn_New.Enabled=true;//启用新建按钮MessageBox.Show(//提示已经创建Word"文件已经创建","提示!");}));});}}
我转化成功了,只是丢格式而已。
解决方案十四:
引用11楼wangnaisheng的回复:
试试这个呢
调用的都是一样的东西