(二)C#使用DsoFramer操作Excel实现查找、替换的功能
2014-11-22 11:08
856 查看
该查找、替换功能区分了大小写,如需不区分大小写,再读懂代码后可自行添加
1.查找全部待查询的关键字
2.全部替换
1.查找全部待查询的关键字
/// <summary> /// 获得全部查询结果 /// </summary> /// <param name="document">当前打开的文档</param> /// <param name="keyWords">查询关键字</param> /// <returns></returns> private System.Data.DataTable GetSearchResult(object document, string keyWords) { try { //// 新建一个虚拟表,用于存放查找到的记录 System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.Add("工作簿"); dt.Columns.Add("工作表"); dt.Columns.Add("单元格"); dt.Columns.Add("值"); if (!string.IsNullOrEmpty(keyWords)) { //// 获取工作簿 Workbook workbook = (Microsoft.Office.Interop.Excel.Workbook)document; int iSheetCount = workbook.Worksheets.Count; int iRowCount = 0, iColCount = 0, iBeginRow = 0, iBeginCol = 0, iResultCount = 0; //// 遍历工作表 for (int i = 1; i <= iSheetCount; i++) { Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[i]; string sName = worksheet.Name; //// 表页下使用区域的行数、列数 iRowCount = worksheet.UsedRange.Cells.Rows.Count; iColCount = worksheet.UsedRange.Cells.Columns.Count; //// 表页下使用区域的起始行列号 iBeginRow = worksheet.UsedRange.Cells.Row; iBeginCol = worksheet.UsedRange.Cells.Column; //// 遍历每张工作表下的行、列 for (int Row = iBeginRow; Row < iRowCount + iBeginRow; Row++) { for (int Col = iBeginCol; Col < iColCount + iBeginCol; Col++) { string s=worksheet.Name; string sCurrentText = ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[Row, Col]).Text.ToString(); if (sCurrentText.IndexOf(keyWords) >= 0) { //// 获取查询到的单元格 Range range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[Row, Col]; //// 获得单元格编号 string sAddress = range.get_Address(Row, Col, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing); DataRow dr = dt.NewRow(); dr["工作簿"] = workbook.Name != null ? workbook.Name : string.Empty; dr["工作表"] = worksheet.Name != null ? worksheet.Name : string.Empty; dr["单元格"] = sAddress; dr["值"] = keyWords; dt.Rows.Add(dr); iResultCount++; } } } } } return dt; } catch { return null; } }
2.全部替换
/// <summary> /// 全部替换 /// </summary> /// <param name="document">打开的文档</param> /// <param name="keyWords">查找关键字</param> /// <param name="replaceWords">替换成的关键字</param> /// <returns></returns> private int ReplaceAll(object document, string keyWords, string replaceWords) { try { int iResultCount = 0; if (!string.IsNullOrEmpty(keyWords)) { Workbook workbook = (Microsoft.Office.Interop.Excel.Workbook)document; int iSheetCount = workbook.Worksheets.Count; int iRowCount = 0, iColCount = 0, iBeginRow = 0, iBeginCol = 0; //// 遍历工作表 for (int i = 1; i <= iSheetCount; i++) { Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[i]; iRowCount = worksheet.UsedRange.Cells.Rows.Count; iColCount = worksheet.UsedRange.Cells.Columns.Count; iBeginRow = worksheet.UsedRange.Cells.Row; iBeginCol = worksheet.UsedRange.Cells.Column; //// 遍历每张工作表下的行、列 for (int Row = iBeginRow; Row < iRowCount + iBeginRow; Row++) { for (int Col = iBeginCol; Col < iColCount + iBeginCol; Col++) { string sCurrentText = ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[Row, Col]).Text.ToString(); //// 判断遍历到的单元格是否包含要替换的 文本 if (sCurrentText.IndexOf(keyWords) >= 0) { Range range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[Row, Col]; //// 替换 string sNewText = sCurrentText.Replace(keyWords, replaceWords); range.Value2 = sNewText; iResultCount++; } } } } } return iResultCount; } catch { return 0; } }
相关文章推荐
- C# 操作EXCEL 类 可以实现导入导出功能 折腾了好长时间 现在两种方法显示出来
- C#前期绑定和后期绑定操作Excel-------实现简单打印功能
- C#前期绑定和后期绑定操作Excel-------实现简单打印功能
- 文本编辑器 “查找与替换”操作的“全部替换”功能的实现代码 Visual Basic 2005 编写
- c#对word操作的查找替换功能
- 使用JAVA的开源API-JExcelAPI来操作Excel,实现基本的功能
- (一)C#使用DsoFramer操作Excel,获取Window、Workbook、WorkSheet、Range
- 使用VSIUAL C#.NET操作Excel -使用公式
- 使用C#自动生成Word2003文档(通过操作COM组件实现)-转载
- .NET1.1下,使用C#自动生成Word2003文档(通过操作COM组件实现)
- C#实现SMTP服务器,使用TCP命令实现,功能比较完善
- 使用C#和Excel进行报表开发(五)-操作单元格边框和颜色
- C#实现SMTP服务器,使用TCP命令实现,功能比较完善
- 技巧和诀窍:使用ASP.NET 2.0 输出缓存替换的功能实现“甜圈缓存(Donut Caching)”
- .NET1.1下,使用C#自动生成Word2003文档(通过操作COM组件实现)
- VB应用程序中实现查找和替换功能
- C#实现在Excel中搜索文本(遍历操作)
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- 使用C#和Excel进行报表开发(五)-操作单元格边框和颜色
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)