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(); } }
  • 相关阅读:
    oracle 11g jdbc jar包在哪个文件目录
    Java生成验证码
    Oracle 安装“权限不够”
    Linux新建文件、文件夹
    基于Java web技术文件上传和下载功能开发实战练习视频(fileupload)
    springmvc的xml文件位置
    oracle 11g的OEM(企业管理器),https://主机ip:1158/em/console网页无法显示
    常见的开源协议分析
    约瑟夫问题猴子选大王
    TOJ1036.Rails STL栈
  • 原文地址:https://www.cnblogs.com/you000/p/2812429.html
Copyright © 2011-2022 走看看