A generic error occurred in GDI+

Nov 24, 2016 at 12:12 PM
I created workflows which generating data in excel files using EPPLUS. these excel files are working fine but suddenly it got error in "Auto Fit Column method". with error of "A generic error occurred in GDI+"

Below is the code.

var stream = Helper.ExportAllData(dTable, null) as MemoryStream;

***********Helper Class ********************

public static class Helper
{
    internal static Stream ExportAllData(DataTable dt, Stream stream = null)
    {
        //var datarecords = dt
        using (var excelpackage = new ExcelPackage(stream ?? new MemoryStream()))
        {
            excelpackage.Workbook.Properties.Author = "CRM Administrator";
            excelpackage.Workbook.Properties.Title = " Report";
            excelpackage.Workbook.Properties.Comments = "This is an Exported Excel with CRM Data";
            // set some extended property values
            excelpackage.Workbook.Properties.Company = "zv";

            excelpackage.Workbook.Worksheets.Add("Report");
            var worksheet = excelpackage.Workbook.Worksheets[1];
            worksheet.Cells[1, 1].LoadFromDataTable(dt, true);
            //Create an autofilter for the range
            //worksheet.Cells["A1:D" + (dt.Rows.Count + 1)].AutoFilter = true;

            worksheet.Cells.AutoFitColumns(); //  __Error comes in this method__

*****************Auto fit Column Method ***********************

public void AutoFitColumns(double MinimumWidth, double MaximumWidth)
{

*

**

var nf = styles.Fonts[styles.CellXfs[0].FontId];
var fs = FontStyle.Regular;
if (nf.Bold) fs |= FontStyle.Bold;
if (nf.UnderLine) fs |= FontStyle.Underline;
if (nf.Italic) fs |= FontStyle.Italic;
if (nf.Strike) fs |= FontStyle.Strikeout;

var nfont = new Font(nf.Name, nf.Size, fs); __At this line it stops and getting A generic error
                                                                                                                  occured in gdi+__
                      *
*