how to fix the exception in var chartObject = charts.Add(60, 10, 600, 300) as Microsoft.Office.Interop.Excel.ChartObject?

1

i try to create a chart using values in excel, when i run the code bellow :

private void DesignChartInExcel(int nbLigne)
        {
       line 1  string fileName = @"C:\Users\LUNA\Documents\lili.xlsx";
       line 2 object missing = Type.Missing;

       line 3 object misValue = System.Reflection.Missing.Value;

            //create excel
       line 4 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

            //add excel workbook
       line 5 Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Open(fileName);
            // Add chart.
       Line 6  var sheet = (Worksheet)excel.ActiveSheet;
       Line 7 var charts = sheet.ChartObjects() as
                Microsoft.Office.Interop.Excel.ChartObjects;
       Line 8     var chartObject = charts.Add(60, 10, 600, 300) as
                Microsoft.Office.Interop.Excel.ChartObject;
           var chart = chartObject.Chart;
            // Set chart range.
           var range = (Range)sheet.get_Range("A1", "b101");
           // range.NumberFormat = "0";
            chart.SetSourceData(range,Missing.Value);

            // Set chart properties.
            chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;
            chart.ChartWizard(Source: range,
                Title: "Complexity Chart - AFDC is satisfied",
                CategoryTitle: "X",
                ValueTitle: "Y");
            //wb.Save();
            wb.Close(true, misValue, misValue);
            excel.Quit();

            releaseObject(sheet);
            releaseObject(wb);
            releaseObject(excel);
        }

i have to following exception : The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)).the exception is generated at line 8. How to fix it?

excel
visual-studio
exception
charts
asked on Stack Overflow Jan 30, 2017 by Strong Marissa

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0