protected void ExportToExcelImageWithOtherColumn()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages.
grdAwardShow.AllowPaging = false;
string varPlant = ddlPlant.SelectedItem.ToString();
this.BindGrid(varPlant, "");
grdAwardShow.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in grdAwardShow.HeaderRow.Cells)
{
cell.BackColor = grdAwardShow.HeaderStyle.BackColor;
}
foreach (GridViewRow row in grdAwardShow.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = grdAwardShow.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = grdAwardShow.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
grdAwardShow.RenderControl(hw);
//Style to format numbers to string.
string style = @"";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}


Abhishek YadavRank #217 · 11.3K reputationPosted Aug 31, 2023, 3:42 AM EDT
Here is an example that demonstrates these steps:
protected void ExportToExcel() { // Create a new Excel workbook var workbook = new XLWorkbook(); // Add a worksheet to the workbook var worksheet = workbook.Worksheets.Add("Sheet1"); // Get the row and column indexes starting point int rowIndex = 1; int colIndex = 1; // Iterate through each row in the GridView foreach (GridViewRow row in GridView1.Rows) { // Reset the column index for each row colIndex = 1; // Iterate through each cell in the row foreach (TableCell cell in row.Cells) { // Get the cell value string cellText = cell.Text; // Check if the cell contains an image if (cell.Controls.Count > 0 && cell.Controls[0] is System.Web.UI.WebControls.Image) { // Get the URL of the image string imageUrl = ((System.Web.UI.WebControls.Image)cell.Controls[0]).ImageUrl; // Load the image from the URL using (var webClient = new WebClient()) { byte[] imageBytes = webClient.DownloadData(imageUrl); using (MemoryStream stream = new MemoryStream(imageBytes)) { // Add the image to the worksheet var image = worksheet.AddPicture(stream); image.MoveTo(worksheet.Cell(rowIndex, colIndex)); image.Scale(0.5); // Increment the column index colIndex++; } } } else { // Set the cell value in the worksheet worksheet.Cell(rowIndex, colIndex).Value = cellText; // Increment the column index colIndex++; } } // Increment the row index rowIndex++; } // Save the Excel file to the response stream Response.Clear(); Response.Buffer = true; Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xlsx"); using (MemoryStream stream = new MemoryStream()) { workbook.SaveAs(stream); stream.WriteTo(Response.OutputStream); Response.Flush(); Response.End(); }