EXCEL Edit Sheet in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source
 

The sample demonstrates how to edit a worksheet in an excel workbook.

EditSheet.gif

       private void btnRun_Click(object sender, System.EventArgs e)
		{
			
			Workbook workbook = new Workbook();
			
			//Load workbook from disk.
			workbook.LoadFromFile(@"..\..\..\..\..\..\Data\EditSheetSample.xls");
			//Initialize worksheet
			Worksheet sheet = workbook.Worksheets[0];

			//Writes string
			sheet.Range["B1"].Text = "Hello,World!";
			//Writes number
			sheet.Range["B2"].NumberValue = 1234.5678;
			//Writes date
			sheet.Range["B3"].DateTimeValue = System.DateTime.Now;
			//Writes formula
			sheet.Range["B4"].Formula = "=1111*11111";

			workbook.SaveToFile("Sample.xls");
			ExcelDocViewer(workbook.FileName);
		}

		private void ExcelDocViewer( string fileName )
		{
			try
			{
				System.Diagnostics.Process.Start(fileName);
			}
			catch{}
		}

          Private Sub btnRun_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRun.Click

			Dim workbook As Workbook = New Workbook()

			'Load workbook from disk.
			workbook.LoadFromFile("..\..\..\..\..\..\Data\EditSheetSample.xls")
			'Initialize worksheet
			Dim sheet As Worksheet = workbook.Worksheets(0)

			'Writes string
			sheet.Range("B1").Text = "Hello,World!"
			'Writes number
			sheet.Range("B2").NumberValue = 1234.5678
			'Writes date
			sheet.Range("B3").DateTimeValue = System.DateTime.Now
			'Writes formula
			sheet.Range("B4").Formula = "=1111*11111"

			workbook.SaveToFile("Sample.xls")
			ExcelDocViewer(workbook.FileName)
		End Sub

		Private Sub ExcelDocViewer(ByVal fileName As String)
			Try
				System.Diagnostics.Process.Start(fileName)
			Catch
			End Try
		End Sub