How to Export Binary data to Word and Excel.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Class="table table-striped table-bordered table-hover"
AllowPaging="false" DataKeyNames="AdmissionNo" RowStyle-Wrap="false" HeaderStyle-Wrap="false" EmptyDataText="No records has been added."
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText = "S.I" ItemStyle-Width="70">
<ItemTemplate>
<asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" class="img-circle img-responsive" ImageUrl='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("SPic")) %>' Height="35px" Width="50px" />
<br />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="120px" DataField="AdmissionNo" HeaderText="Admission No" />
<asp:BoundField ItemStyle-Width="130px" DataField="AdmissionDate" HeaderText="Admission Date" />
<asp:BoundField ItemStyle-Width="175px" DataField="SName" HeaderText="Student Name" />
<asp:BoundField ItemStyle-Width="175px" DataField="FName" HeaderText="Father Name" />
<asp:BoundField ItemStyle-Width="100px" DataField="DOB" HeaderText="Birth Date" />
<asp:BoundField ItemStyle-Width="300px" DataField="Age" HeaderText="Year" />
<asp:BoundField ItemStyle-Width="300px" DataField="Ages" HeaderText="Age in Words" />
</Columns>
</asp:GridView>
protected void Export_Grid(object sender, EventArgs e)
{
Button btn = (Button)sender;
switch (btn.CommandArgument)
{
case "Word":
Word_Export();
break;
case "Excel":
Excel_Export();
break;
case "PDF":
PDF_Export();
break;
}
}
private void Word_Export()
{
try
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=StudentInfo.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
catch (Exception)
{
}
}
private void Excel_Export()
{
try
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=StudentInfo.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
catch (Exception)
{
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
it is exporting nothing to word and Excel file whereas the following code is exporting all data except binary data
protected void btnExportWord_Click(object sender, EventArgs e)
{
ExportGridWord();
}
private void ExportGridWord()
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName = "StuAgeDetails-" + DateTime.Now.ToString("dd.MM.yyyy") + ".doc";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/msword";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
GridView1.GridLines = GridLines.Both;
GridView1.HeaderStyle.Font.Bold = true;
GridView1.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.End();
}
protected void btnExportExcel_Click(object sender, EventArgs e)
{
//ExportGridToExcel();
DataTable dt = new DataTable("GridView_StuAgeDetails");
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
dt.Columns.Add(cell.Text);
}
foreach (GridViewRow row in GridView1.Rows)
{
dt.Rows.Add();
for (int i = 0; i < row.Cells.Count; i++)
{
dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text;
}
}
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt);
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=StuAgeDetails.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}