Move the chart so as not to cover your data. SeriesName = String.Concat( seriesName, "\"" ) SeriesName = String.Concat( seriesName, iRet ) OSeries = (Excel.Series)oChart.SeriesCollection(iRet) OSeries.XValues = oWS.get_Range("A2", "A6") įor( int iRet = 1 iRet <= iNumQtrs iRet++) OSeries = (Excel.Series)oChart.SeriesCollection(1) Missing.Value, Missing.Value, Missing.Value, Missing.Value ) OChart.ChartWizard( oResizeRange, 3DColumn, Missing.Value,Į, Missing.Value, Missing.Value, Missing.Value, OResizeRange = oWS.get_Range("E2:E6", Missing.Value ).get_Resize( Use the ChartWizard to create a new chart from the selected data. OChart = (Excel._Chart)( Missing.Value, Missing.Value, OResizeRange = oWS.get_Range("E8", "E8").get_Resize( Missing.Value, iNumQtrs) Add a Totals formula for the sales data and apply a border. OResizeRange = oWS.get_Range("E1", "E6").get_Resize( Missing.Value, iNumQtrs) Apply borders to the Sales data and headers. OResizeRange = oWS.get_Range("E2", "E6").get_Resize( Missing.Value, iNumQtrs) Fill the columns with a formula and apply a number format. Fill the interior color of the headers. Change the Orientation and WrapText properties for the headers. OResizeRange = oWS.get_Range("E1", "E1").get_Resize( Missing.Value, iNumQtrs) Starting at E1, fill headers for the number of columns selected. MessageBox.Show( sMsg, "Quarterly Sales" ) SMsg = String.Concat( sMsg, " quarter(s)." ) SMsg = String.Concat( sMsg, " quarter(s)?") ĭialogResult iRet = MessageBox.Show( sMsg, "Quarterly Sales?", Determine how many quarters to display data for.įor( iNumQtrs = 4 iNumQtrs >= 2 iNumQtrs-) Private void DisplayQuarterlySales(Excel._Worksheet oWS) MessageBox.Show( errorMessage, "Error" ) Make sure Excel is visible and give the user controlĮrrorMessage = String.Concat( errorMessage, theException.Message ) ĮrrorMessage = String.Concat( errorMessage, " Line: " ) ĮrrorMessage = String.Concat( errorMessage, theException.Source ) Manipulate a variable number of columns for Quarterly Sales Data. Fill D2:D6 with a formula(=RAND()*100000) and apply format. Fill C2:C6 with a relative formula (=A2 & " " & B2).
OSheet.get_Range("A2", "B6").Value2 = saNames Fill A2:B6 with an array of values (First and Last Names). Create an array to multiple values at once. OSheet.get_Range("A1", "D1").VerticalAlignment = OSheet.get_Range("A1", "D1").Font.Bold = true Format A1:D1 as bold, vertical alignment = center. OSheet = (Excel._Worksheet)oWB.ActiveSheet OWB = (Excel._Workbook)(( Missing.Value )) Start Excel and get Application object.
With: private void button1_Click(object sender, System.EventArgs e)
In the code window, replace the following code: private void button1_Click(object sender, System.EventArgs e) On the View menu, select Toolbox to display the toolbox, and then add a button to Form1.ĭouble-click Button1. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
Select Windows Application from the Visual C# Project types.
On the File menu, click New, and then click Project. Create an Automation Client for Microsoft Excel NET and provides sample code to automate Excel.
This article demonstrates how to set the proper project reference to the Excel type library for Visual C#. NET, you can set a project reference to the type library. To access the object model from Visual C#. For example, there is an Application object, a Workbook object, and a Worksheet object, each of which contain the functionality of those pieces of Excel. The object model is a collection of classes and methods that serve as counterparts to the logical components of Excel. With Excel and other Microsoft Office applications, virtually all of the actions that you can perform manually through the user interface can also be performed programmatically by using Automation.Įxcel exposes this programmatic functionality through an object model. Automation to Excel permits you to perform actions such as creating a new workbook, adding data to the workbook, or creating charts. NET to programmatically control other applications. More InformationĪutomation is a process that permits applications that are written in languages such as Visual C#. This article demonstrates how to create an Automation client for Microsoft Excel by using Microsoft Visual C#.