If you open an existing Excel file with ExcelApplication.Open, you can use ExcelWriter's charting objects to modify charts in the spreadsheet.
To create a simple column chart:
Get a Charts collection:
The Charts collection contains all charts in a specified worksheet.
Create an anchor in the worksheet:
An anchor represents the position of a floating object (e.g., a chart) within a worksheet. The chart's top left corner will be placed at the anchor.
Create a blank chart of a specified type, at the anchor you created:
The ChartType class contains all available chart types (e.g., Column) and sub-types (e.g., Clustered).
Return a SeriesCollection object representing the set of data series in the chart:
Set the range of category (x) axis values:
Add a data series to the chart:
The formula passed to CreateSeries represents cells that contain the source values for the new data series.
Example: Creating a Chart
Modifying an Existing Chart
Code sample: Modifying an Existing Chart
When you use ExcelApplication to open a spreadsheet, you can access charts through their names or titles. Chart.getTitle returns a ChartText object representing the title region. ChartText.getText returns the title text. The following function takes a chart's title and returns a Chart object representing the chart. This code can be useful if you want to locate a specific chart in a worksheet but only know its title text:
The following example uses the FindChart code to locate a specific chart in a worksheet. The chart series collection is then cleared and re-set to the data imported from a database: