Cannot draw Excel graph from EXISTING .csv file RRS feed

  • General discussion

  • private static void drawExcelGraph()
          //snippet using interop
          object missingValue = System.Reflection.Missing.Value;
          Excel.Application xlApp = new Excel.Application();
          Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(missingValue);
          Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
          //add data from your datasource like a dataset or like below.
          xlWorkSheet.Cells[1, 1] = "Value of n";
          xlWorkSheet.Cells[1, 2] = "Term";
          xlWorkSheet.Cells[2, 1] = "1";
          xlWorkSheet.Cells[3, 1] = "2";
          xlWorkSheet.Cells[4, 1] = "3";
          xlWorkSheet.Cells[2, 2] = "44";
          xlWorkSheet.Cells[3, 2] = "46";
          xlWorkSheet.Cells[4, 2] = "50";
          //Add a chart
          Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
          Excel.ChartObject xlChart = (Excel.ChartObject)xlCharts.Add(200, 200, 200, 200);
          Excel.Chart chartPage = xlChart.Chart;
          //set the source data and the chart type.
          Excel.Range chartRange = xlWorkSheet.get_Range("B2", "B"+"4");
          chartPage.SetSourceData(chartRange, missingValue);
          chartPage.ChartType = Excel.XlChartType.xlLine;
          //save the workbook
          xlWorkBook.SaveAs("TestBoox.xls", Excel.XlFileFormat.xlWorkbookNormal,
          missingValue, missingValue, missingValue, missingValue, Excel.XlSaveAsAccessMode.xlExclusive, missingValue, missingValue, missingValue, missingValue, missingValue);
          xlWorkBook.Close(true, missingValue, missingValue);
        private static void dump(object obj)
            obj = null;
          catch (Exception)
            obj = null;

    Hello everybody,

    I have data stored in a .csv file that I create and fill in my c# program. I want to draw a graph in Excel using this .csv file. I can build my own Excel worksheet from scratch, and then draw a graph from that. What I am struggling with is drawing a graph from an already existing .csv file. I presumably need to gain a reference to the .csv file that is already sitting in my 'My Document' folder. How would I do this.


    The basic code I have that creates an Excel worksheet from scatch and draws a graph from that is shown above. I want to open an already existing .csv file, and draw a graph from that.

    Thanks a lot everybody!


    Saturday, October 16, 2010 9:59 PM

All replies