I saw in the documentation how to export data from a datatable in Excel,
in my datatable I have the path of an image, in which case how do I render the image in the excel?
Hi Pio,
Greetings from Syncfusion.
We are checking your query to import the data table with images in Excel. We will share further details tomorrow 6th April 2022.
Regards,
Ramya.
Hi Pio,
We don’t have any option to convert the image path into the image while exporting the Data table into Excel. We have suggested you export the Data table into Excel and replace the image path as an image using the following code snippet.
Code snippet:
|
using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Xlsx; IWorkbook workbook = application.Workbooks.Create(1); IWorksheet worksheet = workbook.Worksheets[0];
#region Import from Data Table //Initialize the DataTable DataTable table = SampleDataTable(); //Import DataTable to the worksheet worksheet.ImportDataTable(table, true, 1, 1);
for (int i = 2; i <= worksheet.UsedRange.LastRow; i++) { IPictureShape shape = worksheet.Pictures.AddPicture(i, 3, worksheet.Range[i,3].Value); worksheet.Range[i, 3].Text = ""; shape.Width = 50; shape.Height = 50; }
worksheet.UsedRange.RowHeight = 40; worksheet.Range["C1"].EntireColumn.ColumnWidth = 10; #endregion
#region Save //Saving the workbook FileStream outputStream = new FileStream("ImportDataTable.xlsx", FileMode.Create, FileAccess.Write); workbook.SaveAs(outputStream); #endregion
//Dispose streams outputStream.Dispose();
System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo("ImportDataTable.xlsx") { UseShellExecute = true }; process.Start(); }
private static DataTable SampleDataTable() { string dataPath = @"C:\Users\Data\";
string image1 = dataPath + "Man1.jpg"; string image2 = dataPath + "Man2.png"; string image3 = dataPath + "Woman1.jpg";
DataTable table = new DataTable(); table.Columns.Add("Dosage", typeof(int)); table.Columns.Add("Drug", typeof(string)); table.Columns.Add("Image", typeof(string));
table.Rows.Add(25, "Indocin", image1); table.Rows.Add(50, "Enebrel", image2); table.Rows.Add(10, "Hydralazine", image3);
return table; } |
Kindly try the above snippet and let us know if the solution helps.
Regards,
Ramya.