博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
将PDF转化为wrod
阅读量:4698 次
发布时间:2019-06-09

本文共 3701 字,大约阅读时间需要 12 分钟。

public static void CreateWord(string HtmlPath, string WordSavePath)        {            string inputName = HtmlPath;            //string ext = Path.GetExtension(inputName);            string outputName = WordSavePath;            if (File.Exists(inputName))            {                object oMissing = System.Reflection.Missing.Value;                object oTrue = true;                object oFalse = false;                Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();                Microsoft.Office.Interop.Word._Document oWordDoc = new Microsoft.Office.Interop.Word.Document();                try                {                    oWord.Visible = false;                    // 这里是打开文件的扩展名                    object openFormat = Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatWebPages;                    object openName = inputName;                    oWordDoc = oWord.Documents.Open(ref openName, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref openFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);                    object saveFileName = outputName;                    oWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;  // 居中                    oWord.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdWebView;//将web视图修改为默认视图,不然打开word的时候会以web视图去展示,而不是默认视图。(唯独这句代码是自己加的 = =|||)                    //for (int i = 0; i < oWordDoc.Tables.Count; i++)                    //{                    //    oWordDoc.Tables[i].PreferredWidth = 600;                    //}                              // 这里是保存文件的扩展名                    object saveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault;                    //去掉软回车                    oWordDoc.Content.Find.Text = "^l";                    object FindText, ReplaceWith, Replace;//                     object MissingValue = Type.Missing;                    FindText = "^l";//要查找的文本                     ReplaceWith = "^p";//替换文本                     Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;                    /**/                    /*wdReplaceAll - 替换找到的所有项。                     * wdReplaceNone - 不替换找到的任何项。                     * wdReplaceOne - 替换找到的第一项。                     * */                    oWordDoc.Content.Find.ClearFormatting();                    //移除Find的搜索文本和段落格式设置                     if (oWordDoc.Content.Find.Execute(                    ref FindText, ref MissingValue,                    ref MissingValue, ref MissingValue,                    ref MissingValue, ref MissingValue,                    ref MissingValue, ref MissingValue, ref MissingValue,                    ref ReplaceWith, ref Replace,                    ref MissingValue, ref MissingValue,                    ref MissingValue, ref MissingValue))                    {                    }                                    oWordDoc.SaveAs(ref saveFileName, ref saveFormat, ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);                }                catch (Exception)                {                    throw;                }                finally                {                    oWordDoc.Close(ref oTrue, ref oMissing, ref oMissing);                    oWord.Quit(ref oTrue, ref oMissing, ref oMissing);                }                      }        }

 

转载于:https://www.cnblogs.com/Zhengxue/p/8778034.html

你可能感兴趣的文章
核心J2EE模式 - 截取过滤器
查看>>
.net开源CMS
查看>>
JdbcTemplate
查看>>
第一次使用maven记录
查看>>
SharePoint服务器端对象模型 之 使用CAML进展数据查询
查看>>
Building Tablet PC Applications ROB JARRETT
查看>>
Adobe® Reader®.插件开发
查看>>
【POJ 3461】Oulipo
查看>>
Alpha 冲刺 (5/10)
查看>>
使用Siege进行WEB压力测试
查看>>
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
Android_去掉EditText控件周围橙色高亮区域
查看>>
《构建之法》第一、二、十六章阅读笔记
查看>>
arrow:让Python的日期与时间变的更好
查看>>
(转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
查看>>
Java并发编程
查看>>
Git Stash用法
查看>>
sql server 2008学习8 sql server存储和索引结构
查看>>
Jquery radio选中
查看>>