SfDatagrid - Excel to Export is not working when one of the imageColumn is having text and image both

Excel to export is not working. Getting following error...


System.ArgumentNullException
  HResult=0x80004003
  Message=Buffer cannot be null.
Parameter name: buffer
  Source=mscorlib
  StackTrace:
   at System.IO.MemoryStream..ctor(Byte[] buffer, Boolean writable)
   at System.IO.MemoryStream..ctor(Byte[] buffer)
   at Syncfusion.WinForms.DataGridConverter.DataGridToExcelConverter.ExportImageColumnToExcel(IRange excelRange, Object record, Object cellValue, GridColumn gridColumn)
   at Syncfusion.WinForms.DataGridConverter.DataGridToExcelConverter.ExportRecordCellToExcel(SfDataGrid grid, IRange excelrange, ExcelExportingOptions excelExportingOptions, Object record, IPropertyAccessProvider propertyAccessProvider, GridColumn gridColumn)
   at Syncfusion.WinForms.DataGridConverter.DataGridToExcelConverter.ExportRecordToExcel(SfDataGrid grid, IWorksheet sheet, ExcelExportingOptions excelExportingOptions, Object record, Int32 startColumnIndex, IPropertyAccessProvider propertyAccessProvider)
   at Syncfusion.WinForms.DataGridConverter.DataGridToExcelConverter.ExportRecordsToExcel(SfDataGrid grid, IWorksheet sheet, ExcelExportingOptions excelExportingOptions, IEnumerable records, IPropertyAccessProvider propertyAccessProvider, Group group)
   at Syncfusion.WinForms.DataGridConverter.DataGridToExcelConverter.ExportToExcelWorksheet(SfDataGrid grid, ICollectionViewAdv view, IWorksheet sheet, ExcelExportingOptions excelExportingOptions)
   at Syncfusion.WinForms.DataGridConverter.DataGridToExcelConverter.ExportToExcel(SfDataGrid grid, ICollectionViewAdv view, IWorksheet sheet, ExcelExportingOptions excelExportingOptions)
   at Syncfusion.WinForms.DataGridConverter.DataGridExcelExportExtension.ExportToExcel(SfDataGrid grid, ICollectionViewAdv gridCollectionView, ExcelExportingOptions excelExportingOptions, IWorksheet worksheet)
   at Invoice_Managment.frmEmp.btnExport_Click(Object sender, EventArgs e) in C:\Vb.Net Project\Projects\Managment\Forms\frmEmp.vb:line 229
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at A.cc014134f2c8c66e06f8fae530cd440ed.ca57c1e0bab2450f10ead76be6a9fd350(Object c9edee0b5f2cc840f45fd42b4cec216f2, EventArgs c9edee0b5f2cc840f45fd42b4cec216f2)
   at Guna.UI2.WinForms.Suite.CustomButtonBase.OnClick(EventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at Managment.My.MyApplication.Main(String[] Args) in :line 81

  This exception was originally thrown at this call stack:
    [External Code]
    Managment.frmEmp.btnExport_Click(Object, System.EventArgs) in frmEmp.vb
    [External Code]

Below is code

Imports Syncfusion.WinForms.Controls
Imports Syncfusion.WinForms.DataGrid
Imports System
Imports System.Collections
Imports System.Linq
Imports System.Windows.Forms
Imports Syncfusion.WinForms.DataGrid.Enums
Imports System.Drawing
Imports Syncfusion.WinForms.DataGridConverter
Imports Syncfusion.XlsIO
Imports System.IO

Private Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click
        Dim options = New ExcelExportingOptions()
        Dim excelEngine As ExcelEngine = New ExcelEngine()
        Dim workBook As IWorkbook = excelEngine.Excel.Workbooks.Create()
        dgvEmp.ExportToExcel(dgvEmp.View, options, workBook.Worksheets(0))
        workBook.Version = ExcelVersion.Excel2013
        Dim saveFilterDialog As SaveFileDialog = New SaveFileDialog With {
                .FilterIndex = 2,
                .Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx|Excel 2013 File(*.xlsx)|*.xlsx"
            }

        If saveFilterDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

            Using stream As Stream = saveFilterDialog.OpenFile()

                If saveFilterDialog.FilterIndex = 1 Then
                    workBook.Version = ExcelVersion.Excel97to2003
                ElseIf saveFilterDialog.FilterIndex = 2 Then
                    workBook.Version = ExcelVersion.Excel2010
                Else
                    workBook.Version = ExcelVersion.Excel2013
                End If

                workBook.SaveAs(stream)
            End Using

            If MessageBox.Show(Me, "Do you want to view the workbook?", "Workbook has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
                System.Diagnostics.Process.Start(saveFilterDialog.FileName)
            End If
        End If
    End Sub

Programming Langauge - VB>NET

This issue occurs when one of the imageColumn is having text and image both. Refer attached screenshot.


Attachment: Image_Column_98b3d568.zip

9 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team March 8, 2021 05:30 PM UTC

Hi RAAHUL, 
Thank you for contacting Syncfusion support. 

Currently, we are analyzing your requirement of “SfDatagrid - Excel to Export is not working when one of the imageColumn is having text and image both” We will validate and update you the details on or before March 10, 2021. 
 
We appreciate your patience until then. 
 
Regards, 
Vijayarasan S 



MA Mohanram Anbukkarasu Syncfusion Team March 10, 2021 01:33 PM UTC

Hi RAAHUL,  

Thanks for your patience. 

We have checked the reported scenario and we are able to reproduce the issue in our end and it is confirmed as a defect. We have logged a bug report on this regard.  


We will fix the issue and provide patch for the fix on March 24, 2021. Meanwhile kindly revert to us with the current Syncfusion version you are using in your end. So that, we can provide the patch in the same version. 

Regards, 
Mohanram A. 



RA RAAHUL March 10, 2021 03:44 PM UTC

Hi Mohanram,

Thanks for the update.

Currently, I am using WinForms - Service Pack Release: 18.4.0.39. Waiting for the fix.

Regards,
Rajendra Biradar.


MA Mohanram Anbukkarasu Syncfusion Team March 11, 2021 10:27 AM UTC

Hi RAAHUL,   

Thanks for the update.  

We will provide the patch for the fix in your version 18.4.0.39 on March 24, 2021 as promised. We appreciate your patience until then.  

Regards, 
Mohanram A. 



MA Mohanram Anbukkarasu Syncfusion Team March 24, 2021 02:12 PM UTC

Hi RAAHUL,    

Thanks for your patience.  

I have fixed the reported issue in our end. Due to some issues in creating patch we are unable to provide the patch as promised. We will provide the patch on March 25, 2021. We appreciate your patience until then.  

Regards, 
Mohanram A. 



MA Mohanram Anbukkarasu Syncfusion Team March 25, 2021 03:46 PM UTC

Hi RAAHUL,  

Thanks for your patience. 

We have fixed the reported issue “ArgumentNullException occurs while exporting GridImageColumn with Text to Excel” and the patch for this fix can be downloaded from the following location.  

Recommended approach - exe will perform automatic configuration 
Please find the patch setup from below location: 
Advanced approach – use only if you have specific needs and can directly replace existing assemblies for your build environment 

Please find the patch assemblies alone from below location: 

Please find the NuGet from below location: 

Assembly Version: 18.4.0.39 
Installation Directions :  
This patch should replace the files “Syncfusion.SfDataGridConverter.WinForms” under the following folder. 
$system drive:\ Files\Syncfusion\Essential Studio\$Version # \precompiledassemblies\$Version#\4.6 
Eg : $system drive:\Program Files\Syncfusion\Essential Studio\18.4.0.39\precompiledassemblies\18.4.0.39\4.6 

To automatically run the Assembly Manager, please check the Run assembly manager checkbox option while installing the patch. If this option is unchecked, the patch will replace the assemblies in precompiled assemblies’ folder only. Then, you will have to manually copy and paste them to the preferred location or you will have to run the Syncfusion Assembly Manager application (available from the Syncfusion Dashboard, installed as a shortcut in the Application menu) to re-install assemblies. 

Note :  
You can change how you receive bug fixes by navigating to the following link and updating your preferences. 


Disclaimer :  
Please note that we have created this patch for version 16.2.0.50 specifically to resolve the following issue reported in this forum 163271. 

If you have received other patches for the same version for other products, please apply all patches in the order received. 

Regards, 
Mohanram A 


Marked as answer

MA Mohanram Anbukkarasu Syncfusion Team March 26, 2021 05:13 AM UTC

Hi RAAHUL,     

The provided patch will resolve the exception. You can export the data with image by using ExcelExportingOptions.CellExporting event as shown in the following code example.  

Code example :  

private void button1_Click(object sender, EventArgs e) 
 { 
     var options = new ExcelExportingOptions(); 
     options.ExcelVersion = ExcelVersion.Excel2013; 
     options.CellExporting += Options_CellExporting1; 
     var excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, options); 
     var workBook = excelEngine.Excel.Workbooks[0]; 
 
     SaveFileDialog saveFilterDialog = new SaveFileDialog() 
     { 
         FilterIndex = 2, 
         Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx|Excel 2013 File(*.xlsx)|*.xlsx" 
     }; 
 
     if (saveFilterDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
         using (Stream stream = saveFilterDialog.OpenFile()) 
         { 
             if (saveFilterDialog.FilterIndex == 1) 
                 workBook.Version = ExcelVersion.Excel97to2003; 
             else if (saveFilterDialog.FilterIndex == 2) 
                 workBook.Version = ExcelVersion.Excel2010; 
             else 
                 workBook.Version = ExcelVersion.Excel2013; 
 
             workBook.SaveAs(stream); 
         } 
 
         if (MessageBox.Show(this, "Do you want to view the workbook?", "Workbook has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) 
             System.Diagnostics.Process.Start(saveFilterDialog.FileName); 
     } 
 } 
 
public byte[] ImageToByteArray(System.Drawing.Image imageIn) 
{ 
    MemoryStream ms = new MemoryStream(); 
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); 
    return ms.ToArray(); 
} 
 
private void Options_CellExporting1(object sender, Syncfusion.WinForms.DataGridConverter.Events.DataGridCellExcelExportingEventArgs e) 
{ 
    if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "Location") 
    {                 
        byte[] image = ImageToByteArray(Image.FromFile(@"..\..\Images\location.png"));                             
        MemoryStream stream = new MemoryStream(image); 
        e.Range.HorizontalAlignment = Syncfusion.XlsIO.ExcelHAlign.HAlignCenter; 
        e.Range.Worksheet.Pictures.AddPicture(e.Range.Row, e.Range.Column, stream); 
          
        e.Range.Cells[0].Value = e.CellValue.ToString(); 
 
        e.Handled = true; 
    } 
}       


Please let us know if you require any other assistance from us.  

Regards, 
Mohanram A. 



RA RAAHUL March 26, 2021 02:24 PM UTC

Dear Mohanram,

Have downloaded the patch provided by you.

Kindly note I am able to export Datagrid content to excel when one of the imageColumn is having text and image both

Thanks.
Raahul


MA Mohanram Anbukkarasu Syncfusion Team March 29, 2021 05:18 AM UTC

Hi Raahul, 

Thanks for the update.  

We are glad to know that the reported problem has been resolved at your end. Please let us know if you require any other assistance from us. We are happy to help you.  

Regards, 
Mohanram A. 


Loader.
Up arrow icon