• 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(); } }
  • 相关阅读:
    用继承和组合的知识构造一辆汽车,功能需求见注释
    应用组合的方式实现继承关系
    PL/SQL 07 触发器 trigger
    PL/SQL 05 存储过程 procedure
    PL/SQL 04 游标 cursor
    PL/SQL 03 流程控制
    PL/SQL 02 声明变量 declare
    PL/SQL 01 代码编写规则
    Oracle基础 12 对象 objects 同义词/序列/试图/索引
    Oracle基础 11 约束 constraints
  • 原文地址:https://www.cnblogs.com/you000/p/2812429.html
Copyright © 2020-2023  润新知