Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

how to create a standalone excel chart in C#

$
0
0

Hi,

I have a set of data in a already existing excel spreadsheet, and want to draw graphs regarding the data. So far I can successfully create the graph, but the problem is that I want to have more than one graph, and ideally want each graph to go onto its own spreadsheet.Please advise?

The code I have so far is below:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using Excel;namespace ExcelGraphs
{///<summary>/// Interaction logic for Window1.xaml///</summary>publicpartialclass Window1 : System.Windows.Window
  {public Window1()
    {
      InitializeComponent();

      OpenExcelFile();
      init();
      DrawGraph();
    }
    
    Excel.Workbook     workBook;
    Excel.Worksheet     workSheet;
    Excel.ApplicationClass app = new ApplicationClass(); object misValue = System.Reflection.Missing.Value;publicvoid init()
    {
      workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(1);
    }publicvoid OpenExcelFile()
    {string Path = @"c:\test.csv";// open the file at the above locationtry
      {
        workBook = app.Workbooks.Open(Path, 0, true, 5,"","",true,
                             Excel.XlPlatform.xlWindows,"\t",false,false,
                             0,true,
                             1,
                             0);
      }catch ( Exception ex ) 
      { // Log error (including InnerExceptions!) // Handle exception 
        System.Windows.MessageBox.Show(ex.ToString());
      } 
    }publicvoid DrawGraph()
    {
      Excel.Range chartRange;

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

      chartRange = workSheet.get_Range("E4", "L4");
      chartPage.SetSourceData(chartRange, misValue);
      chartPage.ChartType = Excel.XlChartType.xlColumnClustered;try
      {
        myChart.Chart.HasTitle = true;
        Excel.Range objRange = (Excel.Range)workSheet.Cells[4, 3];
        String strData = objRange.get_Value(misValue).ToString();

        myChart.Chart.ChartTitle.Text = strData;
        
      }
      catch (Exception ex)
      {
      }


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

      releaseObject(workSheet);
      releaseObject(workBook);
      releaseObject(app);

      MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");
    }privatevoid releaseObject(object obj)
    {try
      {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        obj = null;
      }catch (Exception ex)
      {
        obj = null;
        MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
      }finally
      {
        GC.Collect();
      }
    }


  }
}


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>