Work with cell ranges in Excel document using C# and. NET

In today's world, automation of office tasks is becoming an integral part of effective work. This is especially true for working with Excel spreadsheets, which are widely used for storing, analyzing, and visualizing data. Using C# and .NET for automating the processes of interacting with Excel files opens up wide opportunities for increasing productivity, accuracy and speed of information processing. In this context, the SautinSoft Excel .Net library acts as a powerful tool that allows you to manage Excel files without having to run Excel itself, which is especially important for server applications and automated processes.

Working with ranges of cells is one of the most popular tasks when automating Excel operations. Especially relevant:

  • In reporting and analytics (filling in, formatting, then exporting/sending reports).
  • Integration of data from various sources (importing data from a database or CSV into Excel format).
  • Automation of business processes and procedures (automatic generation of uploads, updating of data).
  • In data testing and verification systems (error detection, data matching).
  • In large projects where processing of large amounts of data is required.

The number of uses directly depends on the scale of the business, the specifics of the tasks and automation requirements. On average, automating work with cell ranges helps reduce operational costs, speed up data processing, and improve the accuracy of business processes.

Complete code

using SautinSoft.Excel;
using SkiaSharp;
using System.IO;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free key here:   
            // https://sautinsoft.com/start-for-free/

            Sample();
        }

        /// <summary>
        /// Change style properties in the cell range.
        /// </summary>
		/// <remarks>
        /// Details: 
        /// </remarks>
        static void Sample()
        {
            string inpFile = @"..\..\..\Example.xlsx";
            string outFile = @"..\..\..\Result.xlsx";

            ExcelDocument excelDocument = ExcelDocument.Load(inpFile);
            foreach (ExcelCell cell in excelDocument.Worksheets[0].Cells.GetSubrange("E6", "F13"))
            {
                cell.Style.Fill.SetSolid(SKColors.Yellow);
            }
            excelDocument.Save(outFile);

            // Important for Linux: Install MS Fonts
            // sudo apt install ttf-mscorefonts-installer -y

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
    }
}

Download

Imports SautinSoft.Excel
Imports SkiaSharp
Imports System.IO

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Get your free key here:   
			' https://sautinsoft.com/start-for-free/

			Sample()
		End Sub

		''' <summary>
		''' Change style properties in the cell range.
		''' </summary>
		''' <remarks>
		''' Details: 
		''' </remarks>
		Private Shared Sub Sample()
			Dim inpFile As String = "..\..\..\Example.xlsx"
			Dim outFile As String = "..\..\..\Result.xlsx"

			Dim excelDocument As ExcelDocument = ExcelDocument.Load(inpFile)
			For Each cell As ExcelCell In excelDocument.Worksheets(0).Cells.GetSubrange("E6", "F13")
				cell.Style.Fill.SetSolid(SKColors.Yellow)
			Next cell
			excelDocument.Save(outFile)

			' Important for Linux: Install MS Fonts
			' sudo apt install ttf-mscorefonts-installer -y

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:


Captcha

Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.