vinoth kumar

vinoth kumar

  • NA
  • 23
  • 11.4k

Saving the chart in excel

Feb 3 2013 11:50 PM
hai,
   i have written the codes for saving the chart in excel but i got some problem in those i will upload the codes below if i add a grid view table chart and the grid view table is saved in a single excel but i need to save the chart only so please help me to solve this problem.


 Excel.Application xlApp;
             Excel.Workbook xlWorkBook;
             Excel.Worksheet xlWorkSheet;
             object misValue = System.Reflection.Missing.Value;

             xlApp = new Excel.ApplicationClass();
             xlWorkBook = xlApp.Workbooks.Add(misValue);
             xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
           
             Excel.Range chartRange;

             Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
             Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
             Excel.Chart chartPage = myChart.Chart;

          
             chartRange = xlWorkSheet.get_Range("A1", "d5");
             chartPage.SetSourceData(chartRange, misValue);
             chartPage.ChartType = Excel.XlChartType.xlColumnClustered;
     
               Int i, j;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            for (i = 0; i <= dataGridView1.RowCount - 2; i++)
            {
                for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
                {
                    xlWorkSheet.Cells[i + 1, j + 1] = dataGridView1[j, i].Value.ToString();
                }
            }


             //export chart as picture file
             chartPage.Export(@"d:\excel_chart_export.bmp", "BMP", misValue);

             xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
             xlWorkBook.Close(true, misValue, misValue);
             xlApp.Quit();

             releaseObject(xlWorkSheet);
             releaseObject(xlWorkBook);
             releaseObject(xlApp);

             MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls");
         }


Answers (1)