Search code examples
c#xlsxepplus

How to export Chart created in EPPlus to Image file


I have tried this;

var excelPicture = sheet.Drawings[0] as OfficeOpenXml.Drawing.ExcelPicture;
var img = excelPicture.Image;

However the excelPicture variable become null. How can I create image file from ExcelDrawing??


Solution

  • It seems that this can't be done via EPPlus API. There is the opened issue in the GitHub project repository how export the drawings to a file. But the provided solution in the question does not work (it seems @Onchomngebul himself wrote the the comment)

    Alternative solution is to use Workbook class in Spire.XLS nuget-package (or via free version FreeSpire.XLS).

    var workbook = new Workbook();
    workbook.LoadFromFile(workbookFileName, ExcelVersion.Version2010);
    var sheet = workbook.Worksheets[0]; // index or name of your worksheet
    var image = workbook.SaveChartAsImage(sheet, 0); // chart index
    img.Save(chartFileName, ImageFormat.Png);
    

    For more details please see this comment in the issue.

    Another solution is to try to use Excel.ChartObject. I think this question Exporting Excel Charts as Images may help you.