WORD/EXCEL内容替换
2009-04-28 16:53
357 查看
最近,在做一个小项目时,需要用到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、代码中直接调用这两个函数即可。
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、代码中直接调用这两个函数即可。
相关文章推荐
- 利用Excel查找替换Word文本框里的内容
- word解析为html文本 并替换内容图片(doc、docx
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例 .
- struts2 用stream导出excel/word(导出内容由XML关联自定义的XSL文件构成的)
- 用java获得word,excel,pdf等文档的内容
- java读取word,excel和pdf文档内容
- 网页内容导出word/excel的js代码
- poi+servlet+网络图片,将Servlet内容下载到客户端为Excel或Word
- struts2 用stream导出excel/word(导出内容由XML关联自定义的XSL文件构成的)
- 利用.net替换Word的内容(从数据库中取数据来替换word里面的书签)(转)
- 使用Activex插件复制剪贴板中的word或excel内容到web HTML编辑器并自动上传图片或文件
- 导出EXCEL、WORD中带有图片的无敌替换法
- 使用poi动态替换word指定文本内容
- JavaScript Web页面内容导出到Word、Excel
- 使用JavaScript将Web页面内容导出到Word、Excel
- python python-docx 替换word文档部分内容
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法
- c#获取txt,word,excel文档内容方法
- 用java获得word,excel,pdf等文档的内容
- phpword替换模板内容+解决中文乱码