I want to create a histogram on a already existent spreadsheet xlWorkSheetDia
which displays data in the spreadsheet xlWorkSheetData
.
The main problem I have is creating the histogram chart because Excel.XlChartType
does not suggest me the xlHistogram
Enumerator. I tried looking up the enumerator and creating it by myself following the suggestions in this question: C# - why Histogram does not work in Excel 2016?
I looked up the enumerator number with the object explorer, it also is 118 (decimal). But I get an out of range exception when applying the chart type.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
try
{
string path = @"C:\TestData.csv";
string destPath = @"C:\TestData.txt";
//Create an instance for word app
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheetDia, xlWorkSheetData;
xlApp.Visible = true; //Set status for word application is to be visible or not.
System.IO.File.Copy(sourceFileName: path, destFileName: destPath, overwrite: true); // use txt file instead of csv, for correct delimiter parsing
//Open WorkBook to have csv Data
xlApp.Workbooks.OpenText(destPath, DataType: Excel.XlTextParsingType.xlDelimited, Semicolon: true);
//Creating necessary objects
xlWorkBook = xlApp.ActiveWorkbook;
xlWorkSheetData = xlWorkBook.Sheets[1];
xlWorkSheetData.Name = "Data";
xlWorkSheetDia = xlWorkBook.Sheets.Add();
xlWorkSheetDia.Name = "Diagrams";
//Add chart
var charts = xlWorkSheetDia.ChartObjects() as Excel.ChartObjects;
var chartObject = charts.Add(60, 10, 300, 300) as Excel.ChartObject;
var chart = chartObject.Chart;
//Move Chart
chartObject.Chart.Location(Where: Excel.XlChartLocation.xlLocationAsObject, Name:xlWorkSheetDia.Name); //already gets created on the right place(first spreadsheet)
//Set chart range
chart.SetSourceData(xlWorkSheetData.get_Range("A1", "A5"));//Data on second spreadsheet
//Set chart properties.
Excel.XlChartType myHistogram = (Excel.XlChartType) 118;
chart.ChartType = myHistogram; //<-The exception happens here
//cleanup
xlWorkBook.Close(false);
xlApp.Quit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}
And some test data:
A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;
2,138;2,066;2,022;2,076;0,297;0;0;0,77;1,898;1,864;1,798;1,859;1,715;20,032;8,599;0,039;0;0;0,336;Iteration: 5 of 500
0,298;0,295;0,298;0,297;0,297;0;0;0,302;0,296;0,296;0,297;0,297;0,299;0,3;0,296;0,037;0;0;0,409;Iteration: 6 of 500
0,297;0,299;0,296;0,297;0,311;0;0;0,295;0,295;0,295;0,295;0,295;0,295;0,306;0,306;0,039;0;0;0,372;Iteration: 7 of 500
0,299;0,298;0,295;0,297;0,295;0;0;0,294;0,307;0,295;0,295;0,296;0,297;0,296;0,295;0,04;0;0;0,368;Iteration: 8 of 500
When I tried to record a macro for creating a histogram, I get following commands:
Sub Makro1()
'
' Makro1 Makro
'
'
Range("A1:A5").Select
ActiveSheet.Shapes.Range(Array("Chart 1")).Select
End Sub
Which is, in my opinion, totally different compared to the way I try it in my code example. I don't know how I should write this in C# because I don't have the same methods.
Any suggestions or hints how I can solve my problem?