Gridview to Excel file - save output instead of showing it - c#

I have this code below which takes my GridView, populates it, and generates and opens an Excel file with the griddata in my browser.
What if I want to save that file directly to my server or attach it to an email message, instead of the display prompt that automatically pops up.
Can I do that with small modifications to the script?
protected void btnExcelExport_Click(object sender, EventArgs e)
{
HtmlForm form = new HtmlForm();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", "Student.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
BindGridDetails(GridView1);
form.Attributes["runat"] = "server";
form.Controls.Add(GridView1);
this.Controls.Add(form);
form.RenderControl(hw);
string style = #"<!--mce:2-->";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}

you can save your grid data as a Comma Separated Value file on the server and email or save it directly on your server.
CSV files can be opened using Excel.

Related

Export html page with data(Grid view) and images to excel in Asp.net C#

Title may not describing well what I'm asking for,
Actually I'm looking for some guidelines to solve my code issue,
I'm creating a application that will export the HTML page with data in
Grid view and logo images to excel. What I've done so far in below:
private void ExportExcel()
{
//This method is defined in ButtonClick Event
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
GridView2.AllowPaging = false;
string FileName = "Exported" + DateTime.Now + ".xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
this.EnableViewState = false;
GridView2.DataBind();
Response.Write(ExportDiv.InnerHtml);
GridView2.GridLines = System.Web.UI.WebControls.GridLines.Both;
GridView2.HeaderStyle.Font.Bold = true;
GridView2.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.End();
}
This above code is working fine, but they issue is with the logo image Attach images shows issue with logo image,
Check Image here
The logo image is not set inside a cell what i actually need. What i
need is shown in this pic Please check image
Without using any outsource library you can do this, Things that was causing issue the alignment of the Grid view and HTML code.
So what I've done is create a div and named it ExportDiv and add
HTML Table inside ExportDiv, after that I write my HTML code inside
table by adding row and columns again and again, and add grid view
inside my Table which was created inside ExportDiv.
Following is the code that works behind the click event.
private void ExportToExcel(string strFileName)
{
StringBuilder sb = new StringBuilder();
string attachment = "attachment; filename=" + strFileName;
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
StringWriter oStringWriter = new StringWriter();
HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
Response.Output.Write(sb.ToString());
GridView3.AllowPaging = false;
GridView3.DataBind();
ExportDiv.RenderControl(oHtmlTextWriter);
string style = #"<style> TD { mso-number-format:\#; } </style>";
Response.Output.Write(oStringWriter.ToString());
Response.Write(style);
Response.End();
}
The good thing about the above code is it set HTML design and Grid view alignment accordingly.
Anyone else facing issue in this type of work can ask in comments
surely help him out.

error exporting griddata to MS excel from Asp.net webform application

I have use Asp.net webform c#.When exporting grid data to excel using the, I receive the following error message
"the file formate and extension of Mode of transport.xls don't match. the file could be corrupted or unsafe. unless you trust its source, don't open it. do you want to open it anyway"
The data appears fine once you click "Yes".
code:
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvmodeoftransport.AllowPaging = false;
for (int i = 0; i < gvmodeoftransport.HeaderRow.Cells.Count; i++)
{
gvmodeoftransport.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
}
gvmodeoftransport.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

Programmatically insert data to word file and save in asp.net

i have one asp.net application , and i want to insert header and some text to word file programmatically. then insert gridview data in same word file and then save here is code
protected void btnPrint_Click(object sender, EventArgs e)
{
GridView1.AllowPaging = false;
GridView1.DataBind();
Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.doc"));
Response.Charset = "";
Response.ContentType = "application/ms-word";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
now , gridview data is inserting in that word doc file, but before gridview i want insert some text in to same file,
please help......
Add the following line
Response.Write("enter your text here");
before
GridView1.RenderControl(htw);
Response.Write(sw.ToString());

Image not visible during table export to MS WORD in asp.net

I have the following code with me to export table present on aspx page with id "UserDetails".
When I export this table to MS Excel file images with absolute path are visible but when I export the table to MS WORD (.doc) file then the same images are not visible.
I searched a lot on net but nothing was helpful.
Please help me out to solve this issue.
protected void btnExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
//Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.ContentType = "application/vnd.ms-word";
Response.AddHeader("Content-Disposition", "attachment; filename=WORK_ORDER.doc");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Charset = "";
EnableViewState = false;
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter html = new System.Web.UI.HtmlTextWriter(writer);
UserDetails.RenderControl(html);
Response.Write(writer);
Response.End();
}

Export to Excel using C# (in WebApplication) and identify the button pressed

I exported Excel code from GridView to Excel Sheet. I have a Download Dialog Dox. How do I get the button value whether the data is Saved/Downloaded or the Dialog Box is closed by hitting cancel button?
I want to identify the button pressed is Save or Cancel in the Download Dialog Box.
public void Export(GridView GridView1, DataTable dtGuid)
{
string attachment = "attachment; filename=ScratchcardDetails.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.Visible = true;
GridView1.DataSource = dtGuid;
GridView1.DataBind();
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
this.GridView1.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(this.GridView1);
frm.RenderControl(htw);
//Response.Write(style);
Response.Write(sw.ToString());
Response.End();
}
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
// Save hit
}

Categories

Resources