Essential XlsIO如何实现Excel单元格内容的查找和替换

作者:控件中国网   出处:控件中国网   2016-08-03 09:35:04   阅读:3

Essential XlsIO是一款专门用于对Excel文件进行存取转换的.NET控件,开发人员可以使用该控件在应用程序中集成Excel操作功能,这篇文章主要介绍怎么使用控件提供的API对工作表中的单元格内容进行查找和替换,控件提供的ExcelFindType枚举可以用于指定查找的范围,如查找评论、错误、公式、公式值、数字、文本以及计算后的值,ExcelFindOptions则是用于设置只是匹配查找值或完全匹配单元格值。如果不做任何设置可以采用 FindAll方法,该方法是查找所有符合条件的文本,具体如何实现查找和替换可以参考下面的部分代码:
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
IWorksheet sheet = workbook.Worksheets[0];
// Searches for the given string within the text of worksheet.
IRange[] result = sheet.FindAll("FindValue", ExcelFindType.Text);
// Searches for the given string in formulas.
IRange[] result = sheet.FindAll("FindValue", ExcelFindType.Formula);
// Searches for the given string in calculated value, number and text.
IRange[] result = sheet.FindAll("FindValue", ExcelFindType.Values);
// Searches for the given string in comments.
IRange[] result = sheet.FindAll("FindValue", ExcelFindType.Comments);
// Searches for the given string within the text of worksheet and case matched.
IRange[] result = sheet.FindAll("FindValue", ExcelFindType.Text, ExcelFindOptions.MatchCase);
// Searches for the given string within the text of worksheet and the entire cell content matching to search text.
IRange[] result = sheet.FindAll("FindValue", ExcelFindType.Text, ExcelFindOptions.MatchEntireCellContent);
workbook.Version = ExcelVersion.Excel2013;
workbook.SaveAs("Find.xlsx");
workbook.Close();
excelEngine.Dispose();
Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat