zoukankan      html  css  js  c++  java
  • C#操作word

    //需要添加引用 Microsoft.Office.Interop.Word
    public class WordHelp { Microsoft.Office.Interop.Word.Application oWordApp; Microsoft.Office.Interop.Word.Document oWordDoc; object oMissing = System.Reflection.Missing.Value; /// <summary> /// 加载文档 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public bool LoadWordDoc(string fileName) { try { oWordApp = new Microsoft.Office.Interop.Word.Application(); if (oWordApp == null) { CWConfig.ReadResource resRead = new CWConfig.ReadResource(); MessageBoxCls.Show(resRead.GetResouce("Contract.ComputeNotSetWord"), "", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } } catch (Exception) { CWConfig.ReadResource resRead = new CWConfig.ReadResource(); MessageBoxCls.Show(resRead.GetResouce("Contract.ComputeNotSetWord"), "", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } try { object readOnly = false; object isVisible = false; oWordDoc = oWordApp.Documents.Open(fileName, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oWordDoc.Activate(); } catch (Exception ex) { //ex.ToString() 2005-07-25 Tony Modify MessageBoxCls.Show(ex.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Information); oWordDoc = null; } return true; } /// <summary> /// 设置页眉 /// </summary> /// <param name="title"></param> public void SetHeader(string title) { oWordApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader; oWordApp.Selection.WholeStory(); oWordApp.Selection.TypeText(title); oWordApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; } /// <summary> /// 替换所有同类型文本 /// </summary> /// <param name="strOldText"></param> /// <param name="strNewText"></param> /// <returns></returns> public bool SearchReplace(string strOldText, string strNewText) { object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; //首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。 oWordApp.Selection.Find.ClearFormatting(); oWordApp.Selection.Find.Text = strOldText; oWordApp.Selection.Find.Replacement.ClearFormatting(); oWordApp.Selection.Find.Replacement.Text = strNewText; if (oWordApp.Selection.Find.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing)) { return true; } return false; } public void ReplaceTable(int tabIndex, DataTable dt, params string[] colNames) { if (oWordDoc.Tables.Count < tabIndex || dt == null || dt.Rows.Count < 1) return; Microsoft.Office.Interop.Word.Table newTable = oWordDoc.Tables[tabIndex]; int rowCount = dt.Rows.Count; while (rowCount > 0) { object beforeRow = newTable.Rows[2]; newTable.Rows.Add(ref beforeRow); rowCount--; } for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++) { DataRow dr = dt.Rows[rowIndex]; Microsoft.Office.Interop.Word.Row row = newTable.Rows[rowIndex + 2]; for (int index = 0; index < colNames.Length; index++) { string s = colNames[index]; if (dt.Columns.Contains(s)) row.Cells[index + 1].Range.Text = dr[s].ToString(); } } } /// <summary> /// 保存 /// </summary> /// <param name="filename"></param> /// <param name="path"></param> /// <returns></returns> public void Save(object filename) { oWordDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oWordApp.Quit(ref oMissing, ref oMissing, ref oMissing); oWordDoc = null; oWordApp = null; GC.Collect(); } }
  • 相关阅读:
    线性支持向量机分类
    字符识别--模型集成
    字符识别--模型的训练与验证
    反射案例当中pro.load()报错问题的解决
    字节码对象功能
    BS案例服务器之系统找不到指定路径
    内部类接口实现线程
    多个异常,一次捕获,多次处理
    Objects.requireNonNull
    intellij idea编译java出现kotlin:connecting to daemon
  • 原文地址:https://www.cnblogs.com/you000/p/2812429.html
Copyright © 2011-2022 走看看