Find And Highlight Data In Excel Using C# And SPIRE.XLS

Introduction
 
Hi Everyone..!! Recently I got an opportunity from a China-Based company, E-iceblue, to review one of their products. So I selected Spire.Pdf from E-iceblue.The article was Convert pdf to other format using Spire.Pdf. Now I am going introduce Spire.XLS. The Spire.XLS for .NET is a professional Excel .NET component that can be used to any type of .NET 2.0, 3.5, 4.0 or 4.5 framework application, both ASP.NET web sites and Windows Forms applications. Spire.XLS for .NET offers object model Excel API for speeding up Excel programming in .NET platform – create new Excel documents from template, edit existing Excel documents, and convert Excel files. 
 
Namespace
 
The following code contain “Spire.Xls”. This library contains the functionality of highlight data in excel,Excel Version,Excel Location,etc.
 
  1. using Spire.Xls;    
  2.     using System.Drawing;    
  3.          
  4.     namespace ConsoleApplication1    
  5.     {    
  6.         class Program    
  7.         {    
  8.             static void Main(string[] args)    
  9.             {    
  10.                 //load an excel file from system    
  11.                 Workbook workbook = new Workbook();    
  12.                 workbook.LoadFromFile(@"C:\Users\RAJEESH\Desktop\TestSpireXls.xlsx", ExcelVersion.Version2013);    
  13.          
  14.                 //find and highlight excel data    
  15.                 Worksheet sheet = workbook.Worksheets[0];    
  16.                 foreach (CellRange range in sheet.FindAllString("Palakkad",true,true))    
  17.                 {    
  18.                     range.Style.Color = Color.Green;    
  19.                 }    
  20.                 //save and launch the project    
  21.                 workbook.SaveToFile("NewProduct.xlsx", ExcelVersion.Version2013);    
  22.                 System.Diagnostics.Process.Start("NewProduct.xlsx");    
  23.             }    
  24.         }    
  25.     }   
Creating the instance of PdfDocument
 
  1. Workbook workbook = new Workbook();  
 Load the file location & Selecting the version of Excel
 
 Loading the location of excel file for “TestSpireXls.xlsx“. We need to specify the version of excel file on loading the process.
  1. workbook.LoadFromFile(@"C:\Users\RAJEESH\Desktop\TestSpireXls.xlsx", ExcelVersion.Version2013);   
 The following excel versions are contain in Spire.Xls.
  1. ODS  
  2. Version2007  
  3. Version2010  
  4. Version2013  
  5. Version97to2003  
  6. Xlsb2007  
  7. Xlsb2010  
Find and Highlight Excel Data 
 
The following codeis finding string as “Palakkad” in excel sheet. So we can change this based on our requirement. Other finding options and Color options are specified in Pic 1 & Pic 2 respectively. 
 
  1. Worksheet sheet = workbook.Worksheets[0];    
  2.   foreach (CellRange range in sheet.FindAllString("Palakkad",true,true))    
  3.   {    
  4.       range.Style.Color = Color.Green;    
  5.   }  
 Pic 1-
 
 
 Pic 2
 
 
Save and Launch The Project
Highlighted data saved into the new excel sheet “NewProduct.xlsx“.
 
  1. workbook.SaveToFile("NewProduct.xlsx", ExcelVersion.Version2013);    
  2.    System.Diagnostics.Process.Start("NewProduct.xlsx");  
 Before Highlighting Data In Excel
 
 
 
 OutPut
 
 
 
Pros
  • Very fast conversion
  • Easy to handle code
  • Less code with quality output
 
Summary

We learned how to highlight data in Excel Using C# & Spire.Xls. I hope this article is useful for all beginners.
 


Similar Articles