求个问题,关于word转化成html的

问题描述

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的回复:

试试这个呢

调用的都是一样的东西

时间: 2024-07-29 22:05:13

求个问题,关于word转化成html的的相关文章

在sharepoint 2010项目中,怎么实现word转化成html,然后显示在iframe中?

问题描述 在网上搜到word转化成html的方法很多,在一般的webapplication里可以实现,但是在sharepoint项目中,总是提示缺乏对象实例.求提示,谢谢!stringitem="下载.docx";WordToHtmlFiles(Server.MapPath("~/_layouts/Documents/"+item));//WordToHtmlFilespublicvoidWordToHtmlFiles(stringWordFilePath){Mic

npoi-跪求NPOI.dll读取word文件(.net)

问题描述 跪求NPOI.dll读取word文件(.net) 从官网下载NPOI2.0版本没有XWPFDocument方法. 解决方案 进微软官网查找软件下载更新,重启系统配置 解决方案二: NPOI2.0 只可以操作WORD2007和2010 不支持2003格式.

求完整的网页word编辑器教学视频(制作视频也行)

问题描述 求完整的网页word编辑器教学视频(制作视频也行) 网页word编辑器没接触过,从哪里学起比较好啊,各位好友有没有例子什么的可以学 习学习的,求教学视频连接,各位求助,尽量详细,想从零学起

齿轮-有没有会展开式二级减速器的课程设计,求一个详细的word文档,有的朋友请帮我一下,求各位兄弟姐妹们帮助。

问题描述 有没有会展开式二级减速器的课程设计,求一个详细的word文档,有的朋友请帮我一下,求各位兄弟姐妹们帮助. 谢谢大家了,在线等.谢谢大家了,在线等.谢谢大家了,在线等.谢谢大家了,在线等.谢谢大家了,在线等.谢谢大家了,在线等.

怎么样使用java生成一个word文档,求个例子,word的格式可以自己定义和排版。

问题描述 类似LoadRunner导出测试报告的那种功能,希望大家可以提供些技术,小弟在此谢过大家了. 解决方案 解决方案二:利用流,输出流,先创建文件,然后利用输出流输出到word文档中.以下程序仅供参考://建立一个文件,向其中写入多行数据,然后读出来打印到显示器上importjava.io.*;importjava.util.*;classTestBufferedOutputStream{publicstaticvoidmain(Stringargs[])throwsIOException

跪求rdlc报表导出word报错问题

问题描述 rdlc报表导出excel和pdf是正常的,但是选择了导出word之后就报错这是点击word之后导航栏里的链接:http://localhost:27673/Reserved.ReportViewerWebControl.axd?Culture=2052&CultureOverrides=True&UICulture=2052&UICultureOverrides=True&ReportStack=1&ControlID=2d2607be3f7e443b8

求助,求推荐一个可以复制word文档的编辑器

问题描述 如题,收费和不收费都可以,谢谢了 解决方案 解决方案二:ckeditor解决方案三:国产的有:百度编辑器:KindEditor:解决方案四:ckEditor+ckfinder二者结合起来用着方便

html-RTF2HTML 如何转换 求解答

问题描述 RTF2HTML 如何转换 求解答 RTF2HTML 如何转换 求解答 不调用Word.Application 解决方案 rtf其实就是带控制符的文本,可以自己解析 参考:http://www.codeproject.com/Tips/1011178/RTF-to-HTML-VB-NET

word文件格式

问题描述 在文件目录下有很多word文档,请问我怎么在每次打开word的时候把word里面的文字,图片或表格统一格式呢?也就是说word内的字体一致,段落,行间距,图片和表格样式也要一致!或者是把word转化成html文件的同时把格式给固定了? 解决方案 解决方案二:不会呀,顶一个.