How to Replace Selected Data in Excel on WPF Applications

Spire.Xls enables developers to quickly find specific data, highlight the data as well as replace them with new data in excel files. We've already introduced how to find and highlight excel data, so this article is aimed to demonstrate how to replace selected data in excel on WPF applications using Spire.Xls for WPF.

Detail steps and code snippets:

Step 1: Create a WPF Application, add two buttons, three text boxes, two text blocks into the Main Window and align them like below.

How to Replace Selected Data in Excel on WPF Applications

Step 2: Double click the Browse button, add following codes to initialize a new OpenFileDialog object and set its properties to select excel file, and save its file name to the first text box.

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = @"E:\";
openFileDialog1.Title = "Select Excel Files";

openFileDialog1.DefaultExt = "xlsx";
openFileDialog1.Filter = "Excel files (*.xls;*.xlsx)|*.xls;*.xlsx|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;

openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;

if (openFileDialog1.ShowDialog().Value)
{
    textBox1.Text = openFileDialog1.FileName;
}

Step 3: Double click the Replace All button, add following codes to load the excel file, replace all of the text entered in the find box with new text entered in the replace box, then save the changes and launch the file.

//Load the sample excel file
Workbook workbook = new Workbook();
workbook.LoadFromFile(textBox1.Text);

//Get the first worksheet of the excel file
Worksheet sheet = workbook.Worksheets[0];

//Call Worksheet.FindAllString(string stringValue, bool formula, bool formulaValue) method to find all of the specific text from the first worksheet and save the results to a CellRange array
CellRange[] ranges = sheet.FindAllString(this.FindBox.Text, false, false);

//Loop through the array, replace the selected text with new text
foreach (CellRange range in ranges)
{
    range.Text = this.ReplaceBox.Text;
}

//Save the changes and launch the file
workbook.SaveToFile("Replaced.xlsx");
System.Diagnostics.Process.Start("Replaced.xlsx");

Result:

Run the project, you will get the following dialog box, Click Browse to choose excel file, then input the text that you want to find and the text used to replace, next click the Replace All button.

How to Replace Selected Data in Excel on WPF Applications

Effective screenshot of the result excel file:

How to Replace Selected Data in Excel on WPF Applications

Full codes:

using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
using Spire.Xls;

namespace Replace_Selected_Excel_Data_in_WPF
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void BrowseBtn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = @"E:\";
            openFileDialog1.Title = "Select Excel Files";

            openFileDialog1.DefaultExt = "xlsx";
            openFileDialog1.Filter = "Excel files (*.xls;*.xlsx)|*.xls;*.xlsx|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;

            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.ReadOnlyChecked = true;
            openFileDialog1.ShowReadOnly = true;

            if (openFileDialog1.ShowDialog().Value)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }

        private void ReplaceBtn_Click(object sender, RoutedEventArgs e)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(textBox1.Text);

            Worksheet sheet = workbook.Worksheets[0];

            CellRange[] ranges = sheet.FindAllString(this.FindBox.Text, false, false);
            foreach (CellRange range in ranges)
            {
                range.Text = this.ReplaceBox.Text;
            }

            workbook.SaveToFile("Replaced.xlsx");
            System.Diagnostics.Process.Start("Replaced.xlsx");
        }
    }
}