使用EPPLUS查找和替换所有字符串 [英] Find and Replace all string using EPPLUS

查看:533
本文介绍了使用EPPLUS查找和替换所有字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用EPPLUS查找和替换工作表中的所有字符串?

How to find and replace all the string in worksheet using EPPLUS?

在Excel宏上就像这样:

on Excel Macro it is simply as like this:

Cells.Replace What:="k", Replacement:="w", LookAt:=xlPart, SearchOrder _
    :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False


推荐答案

EPPLus 中搜索和替换单元格值的快速方法是在 EEPlus <中使用 Linq / code>。我为您写了一个简单的例子。我的电子表格几乎有10列和1157行,并且花费不到一秒钟的时间来搜索和替换值。

The fast way to search and replace cell values in EPPLus is to use Linq in EEPlus. I wrote a simple example for you. My spread-sheet got almost 10 columns and 1157 rows and it took less than a second to search and replace values.

    var valueToSearch = "Foo";
    var valueToReplace = "Bar";
    var sheetName = "Sheet1";
    var filePath = @"d:\foo-bar.xlsx";

    using (var excel = new ExcelPackage(new System.IO.FileInfo(filePath)))
    {
        var ws = excel.Workbook.Worksheets[sheetName];

        // search in all cells
        // https://github.com/JanKallman/EPPlus/wiki/Addressing-a-worksheet
        var query = from cell in ws.Cells["A:XFD"] 
                    where cell.Value?.ToString().Contains(valueToSearch) == true
                    select cell;

        foreach(var cell in query)
        {
            cell.Value = cell.Value.ToString().Replace(valueToSearch, valueToReplace);
        }

        excel.Save();
    }

这篇关于使用EPPLUS查找和替换所有字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆