EXCEL Edit Chart Sheet in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source
 

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

EditChart.gif

		private void btnRun_Click(object sender, System.EventArgs e)
		{
			Workbook workbook = new Workbook();
  			
			//Initialize worksheet
			workbook.LoadFromFile("../../../../../../Data/EditChartSample.xls",true);
			Worksheet sheet = workbook.Worksheets[0];

			//Writes chart data
			CreateChartData(sheet);

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

		private void CreateChartData(Worksheet sheet)
		{
			//Jun
			sheet.Range["F6"].NumberValue = 6000;
			sheet.Range["F7"].NumberValue = 8000;
			sheet.Range["F8"].NumberValue = 9000;
			sheet.Range["F9"].NumberValue = 8500;

			//Aug
			sheet.Range["G6"].NumberValue = 4000;
			sheet.Range["G7"].NumberValue = 7000;
			sheet.Range["G8"].NumberValue = 2000;
			sheet.Range["G9"].NumberValue = 5000;

			sheet.Range["F6:F9"].Style.NumberFormat = "\"$\"#,##0";
			sheet.Range["G6:G9"].Style.NumberFormat = "\"$\"#,##0";
		}

		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()

			'Initialize worksheet
			workbook.LoadFromFile("../../../../../../Data/EditChartSample.xls",True)
			Dim sheet As Worksheet = workbook.Worksheets(0)

			'Writes chart data
			CreateChartData(sheet)

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

		Private Sub CreateChartData(ByVal sheet As Worksheet)
			'Jun
			sheet.Range("F6").NumberValue = 6000
			sheet.Range("F7").NumberValue = 8000
			sheet.Range("F8").NumberValue = 9000
			sheet.Range("F9").NumberValue = 8500

			'Aug
			sheet.Range("G6").NumberValue = 4000
			sheet.Range("G7").NumberValue = 7000
			sheet.Range("G8").NumberValue = 2000
			sheet.Range("G9").NumberValue = 5000

			sheet.Range("F6:F9").Style.NumberFormat = """$""#,##0"
			sheet.Range("G6:G9").Style.NumberFormat = """$""#,##0"
		End Sub

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