Skip to content

Export excel chart to image for sending in email using C# without interop

dsd kj

Asked Nov 17, 2012, 11:23 AM EST2 answers
Share
LinkedInX
I've server side automation to create some chart in excel using OpenXML. I need to export a chart from my report to an image and send it in an email. The excel libraries are not installed on servers so interop COM is not an option. How can I achieve this without using anything external dlls or anything? Suggestions appreciated. thanks 

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I am afraid it will be so hard not using any external tools
    0
  • 0

    Hi, as Evaln mention this is a very difficult task to achieve without using any external helpers, unfortunately .NET does not provide any means to accomplish this.
    Also I'm not sure why your requirement is not to use any external DLL because as you mention you are already using an external DLL (OpenXML) for chart creation.

    Nevertheless first I would suggest you to watch the video on the following link:
    http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2013/09/26/screen-cast-updating-chart-data-in-wordprocessingml-and-presentationml.aspx
    It explains very nicely charting markups which are contained in your XLSX file that has a chart.

    After that you need to create a chart renderer, for example you can use GDI+ and map those chart markups with some drawing action and thus create an image.
    I must admit I'm not a fan of reinventing the wheel so I would probably use 
    this external .NET DLL for excel type files, it has a free version which is more than capable to achieve this, like the following:

    - 
    First I would create the required chart in an excel file with C#:

    var file = new ExcelFile();
    var sheet = file.Worksheets.Add("Sheet 1");
     
    var chart = sheet.Charts.Add(ChartType.Area, "D1", "H8");
    chart.SelectData(
    "A1:C8");
     
    // ...


    - 
    Second I would save that chart to an image stream:

    using(var stream = new MemoryStream())
    {
        chart.Format().Save(stream, 
    new ImageSaveOptions(ImageSaveFormat.Png));

       
    // ...
    }

    - After that I would use that stream to create an Attachment instance...

    0