Edit Excel Data in C#, VB.NET

2011-07-29 02:30:14 Written by  support iceblue
Rate this item
(0 votes)

It is necessary to edit existing Excel workbook when the data information has changed. For example, if the one staff’s telephone number or address is changed, the matched data in Excel should be changed as well. Solution in this guide focuses on introducing the solution to Edit Excel and format edited data in C# and VB.NET. The following screenshot presents result after editing.

Edit Excel Data

Spire.XLS for .NET, a professional .NET Excel component, provides an XlsRange class to enables users to set Text, Value, Number, Formula or other properties to edit data in specified cells. In this example, the data in Cell D2 and E2 is edited in the existing Excel workbook. Also, the edited data will be formatted for distinguishing with other data information. When formatting, set Font properties of CellStyle for specified cell range, including FontName and Color. Download and Install Spire.XLS for .NET and follow code below.

[C#]
using Spire.Xls;
using System.Drawing;

namespace EditSheet
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Workbook
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"E:\Work\Documents\ExcelFiles\Staff Contact Info.xlsx");

            //Edit Text
            Worksheet sheet = workbook.Worksheets[0];
            sheet.Range["D2"].Text = "Kelly Cooper";
            sheet.Range["D2"].Style.Font.FontName = "Arial Narrow";
            sheet.Range["D2"].Style.Font.Color = Color.DarkBlue;

            //Edit Cell Value
            sheet.Range["E2"].Value = "00-1-285-7901742";
            sheet.Range["E2"].Style.Font.FontName = "Book Antiqua";
            sheet.Range["E2"].Style.Font.Color = Color.DarkOrange;

            //Save and Launch
            workbook.SaveToFile("EditSheet.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("EditSheet.xlsx");

        }
    }
}
[VB.NET]
Imports Spire.Xls
Imports System.Drawing

Namespace EditSheet
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load Workbook
            Dim workbook As New Workbook()
            workbook.LoadFromFile("E:\Work\Documents\ExcelFiles\Staff Contact Info.xlsx")

            'Edit Text
            Dim sheet As Worksheet = workbook.Worksheets(0)
            sheet.Range("D2").Text = "Kelly Cooper"
            sheet.Range("D2").Style.Font.FontName = "Arial Narrow"
            sheet.Range("D2").Style.Font.Color = Color.DarkBlue

            'Edit Cell Value
            sheet.Range("E2").Value = "00-1-285-7901742"
            sheet.Range("E2").Style.Font.FontName = "Book Antiqua"
            sheet.Range("E2").Style.Font.Color = Color.DarkOrange

            'Save and Launch
            workbook.SaveToFile("EditSheet.xlsx", ExcelVersion.Version2010)
            System.Diagnostics.Process.Start("EditSheet.xlsx")

        End Sub
    End Class
End Namespace

Additional Info

  • tutorial_title:
Last modified on Monday, 28 February 2022 09:42