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(); } }
  • 相关阅读:
    php rewrite 简单
    第十六章 复杂的抽像类结构 简单
    php数学函数 简单
    PHP PCLZIP压缩类的学习笔记 简单
    windows实战Git环境配置msysGit+TortoiseGit 简单
    配置php.ini支持图片exif信息 简单
    使用PHP连接POSTGRES数据库 简单
    jQuery Ajax 实例 全解析 简单
    第十六章 多态性(一) 简单
    C#委托与事件
  • 原文地址:https://www.cnblogs.com/you000/p/2812429.html
Copyright © 2011-2022 走看看