Hello friends
I'm trying to automate an excel file within a chart,and I want to export the chart to JPG file or something like this,but I got this error :
Exception from HRESULT: 0x800401A8
I searched hours and tried many codes but every time I get the same error.
here is my code:
Excel._Workbook workBook;
Excel.Series series;
Excel.Range range;
Excel._Chart chart;
workBook = (Excel._Workbook)workSheet.Parent;
chart = (Excel._Chart)workBook.Charts.Add(Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
range = workSheet.get_Range(string.Format("C1:C{0}", rowCount), Missing.Value).get_Resize(
Missing.Value, 1); ;
chart.ChartWizard(range, Excel.XlChartType.xlColumnClustered, Missing.Value,
Excel.XlRowCol.xlColumns, Missing.Value, Missing.Value, false,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
series = (Excel.Series)chart.SeriesCollection(1);
series.XValues = workSheet.get_Range("A1", string.Format("B{0}", rowCount));
series = (Excel.Series)chart.SeriesCollection(1);
chart.ChartArea.Font.Name = "Tahoma";
chart.AutoScaling = true;
chart.ChartStyle = 27;
Excel.Axis yaxis = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary);
yaxis.MinimumScaleIsAuto = false;
yaxis.MinimumScale = 0;
yaxis.MaximumScaleIsAuto = false;
yaxis.MaximumScale = 10;
chart.HasTitle = true;
chart.ChartTitle.Text = "title";
chart.Location(Excel.XlChartLocation.xlLocationAsObject, workSheet.Name);
range = (Excel.Range)workSheet.Rows.get_Item(3, Missing.Value);
workSheet.Shapes.Item("Chart 1").Top = (float)(double)range.Top;
range = (Excel.Range)workSheet.Columns.get_Item(5, Missing.Value);
workSheet.Shapes.Item("Chart 1").Left = (float)(double)range.Left;
chart.Export(@"C:\Chart1.jpg", "JPG", false);