excel文档经常用来操作数量繁多的数据和资料。当我们需要对excel数据做修改的时候,查找和替换功能非常重要。通过查找和替换功能,我们可以快速地找到想要的数据并一次性对一个或多个单元格的数据进行更改。该文将详细介绍如何使用c#来实现excel的查找,替换和高亮显示数据功能。
首先请看将用来更改数据的示例excel工作表:
代码片段:
c#
//加载excel文档
workbook workbook = new workbook();
workbook.loadfromfile("交货单.xlsx");
//获取工作表
worksheet sheet = workbook.worksheets[0];
//查找字符串“紧张”
cellrange[] ranges = sheet.findallstring("紧张", false, false);
foreach (cellrange range in ranges)
{
//使用 “充足”替换
range.text = "充足";
//设置高亮显示颜色
range.style.color = color.yellow;
}
//查找数值“1504”
foreach (cellrange range in sheet.findallnumber(1504, true))
{
//替换数值和设置高亮显示颜色
range.text = "1506";
range.style.color = color.gold;
}
//保存工作薄
workbook.savetofile("result.xlsx", excelversion.version2010);
vb.net
'加载excel文档
dim workbook as new workbook()
workbook.loadfromfile("交货单.xlsx")
'获取工作表
dim sheet as worksheet = workbook.worksheets(0)
'查找字符串“紧张”
dim ranges as cellrange() = sheet.findallstring("紧张", false, false)
for each range as cellrange in ranges
'使用 “充足”替换
range.text = "充足"
'设置高亮显示颜色
range.style.color = color.yellow
next
'查找数值“1504”
for each range as cellrange in sheet.findallnumber(1504, true)
'替换数值和设置高亮显示颜色
range.text = "1506"
range.style.color = color.gold
next
'保存工作薄
workbook.savetofile("result.xlsx", excelversion.version2010)
替换数据并高亮显示后的效果图: