zoukankan      html  css  js  c++  java
  • WORD/EXCEL内容替换

    最近,在做一个小项目时,需要用到WORD/EXCEL(目前只针对OFFICE2003)文档中内容的替换,在网上搜寻了一番,找到了一些解决方法,自己再整理了一下,记录出来与大家共享。

    1、在项目的引用中添加WORD和EXCEL的COM引用:Microsoft.Word 11.0 Object Library和Microsoft.Excel 11.0 Object Library;

    2、在文件的USING部分加入以下命名空间:

    using Microsoft.Office.Core;
    using Microsoft.Office.Interop.Word;
    using Microsoft.Office.Interop.Excel;
    using System.IO;
    using System.Reflection;

    3、定义WORD和EXCEL内容替换函数:

            private void WordReplace(string filePath, string strOld, string strNew)
            {
                Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.ApplicationClass();
                object nullobj = System.Reflection.Missing.Value;
                object file = filePath;
                Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(
                ref file, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj, ref nullobj);

                doc.Content.Text = doc.Content.Text.Replace(strOld, strNew);
                doc.Content.AutoFormat();
                Microsoft.Office.Interop.Word.Range range = null;
                doc.Close(ref nullobj, ref nullobj, ref nullobj);

                app.Quit(ref nullobj, ref nullobj, ref nullobj);
            }

            private void ExcelReplace(string filePath, string strOld, string strNew)
            {
                Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                excel.Visible = false;

                string modelFile = filePath;  //文件名
                Workbook wb = excel.Workbooks._Open(modelFile, Missing.Value, Missing.Value, Missing.Value, Missing.Value
                 , Missing.Value, Missing.Value, Missing.Value, Missing.Value
                 , Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                Worksheet xSheet = (Worksheet)wb.Sheets[1];
                int icount = wb.Sheets.Count;
                for (int i = 1; i <= icount; i++)
                {
                    try
                    {
                        xSheet = (Worksheet)wb.Sheets[i];
                        object what = strOld;  //查找字符串
                        object retxt = strNew; //替换字符串
                        xSheet.Cells.Replace(what, retxt, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                    }
                    catch
                    {
                    }
                }
                wb.Save();
                NAR(xSheet);
                wb.Close(false, Missing.Value, Missing.Value);
                NAR(wb);
                excel.Quit();
                NAR(excel);
                System.GC.Collect();
            }

            private void NAR(object o)
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(o);//强制释放一个对象
                }
                catch
                {
                }
                finally
                {
                    o = null;
                }
            }

    4、代码中直接调用这两个函数即可。

  • 相关阅读:
    SSH整合中,使用父action重构子类action类.(在父类中获取子类中的泛型对象)
    算法大神之路----排序(插入排序法)
    Redis的基本使用(基于maven和spring)
    算法大神之路----排序(选择排序法)
    exe4j 安装
    png转ico网站
    artTemplate/template.js模板将时间格式化为正常的日期
    artTemplate js模板引擎动态给html赋值
    IIS7的集成模式下如何让自定义的HttpModule不处理静态文件(.html .css .js .jpeg等)请求
    Windows Server2012 R2 安装.NET Framework 3.5失败解决方法
  • 原文地址:https://www.cnblogs.com/tianfu/p/1445540.html
Copyright © 2011-2022 走看看