I am exporting excel from gridview and it is not giving me good format. Images are showing on excel file. I don't want the images, what can I do?
here is my code
protected void Export_to_Excel(object sender, EventArgs e)
{
//System.Diagnostics.Debugger.Break();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=vault-extract-nsf.xls");
Response.ContentType = "application/vnd.xlsx";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
I did it and images are ignored.
protected void Export_to_Excel(object sender, EventArgs e)
{
//System.Diagnostics.Debugger.Break();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=PatientSearchReport.xls");
Response.ContentType = "application/vnd.xlsx";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
string html2 = Regex.Replace(stringWrite.ToString(), #"(<input type=""image""\/?[^>]+>)", #"", RegexOptions.IgnoreCase);
html2 = Regex.Replace(html2, #"(<input class=""checkbox""\/?[^>]+>)", #"", RegexOptions.IgnoreCase);
html2 = Regex.Replace(html2, #"(<a \/?[^>]+>)", #"", RegexOptions.IgnoreCase);
Response.Write(html2.ToString());
Response.End();
}
You can do like this
protected void Export_to_Excel(object sender, EventArgs e)
{
GridView tmpGrid = new GridView();
// here bind this grid with same datasource which you have used for GridView1
//System.Diagnostics.Debugger.Break();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=vault-extract-nsf.xls");
Response.ContentType = "application/vnd.xlsx";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
tmpGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
Related
I'm trying to export a gridview that shows up in an window (modal) but it exports the whole page.
public void ExportToXLS(GridView gv)
{
gv.AllowPaging = false;
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridView.xls");
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView gvExp = new GridView();
gvExp = gv;
gvExp.RenderControl(htmlWrite);
HttpContext.Current.Response.Write(stringWrite.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
}
The problem was i had a link button in the gridview and it was causing the page to not render properly.
The solution was simple, i just removed the columns where the link buttons were considering that i really didn't need them.
public void ExportToXLS(GridView gv)
{
GV.Columns[4].Visible = false;
GV.Columns[5].Visible = false;
gv.AllowPaging = false;
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridView.xls");
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView gvExp = new GridView();
gvExp = gv;
gvExp.RenderControl(htmlWrite);
HttpContext.Current.Response.Write(stringWrite.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
}
The GV.Columns[ ].Visible = false; lines of code at the beginning just solved my whole problem.
I have written the below code to generate the excel file.
private void ExportReport(IList ApproveListData, string fileName)
{
var gv = new GridView();
gv.AllowPaging = false;
gv.DataSource = ApproveListData;
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write("<b> Pending approval report </b>");
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
I have checked data in the grid view but excel file is not generating.
Please help.
Thanks in advance.
I have a page with Gridview and when button clicked I should export data from Gridview to .xls file. I have tried it in many ways. This is my code:
protected void export_Click(object sender, EventArgs e)
{
Response.ClearContent();
//Response.Buffer = true;
//Response.ClearHeaders();
//Response.CacheControl = "no-cache";
//Response.AddHeader("Pragma", "no-cache");
Response.AddHeader("Content-Disposion","attachment; filename=Clients.xls");
Response.ContentType = "application/excel";
StringWriter swriter = new StringWriter();
HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
grid.RenderControl(hwriter);
Response.Write(swriter.ToString());
Response.End();
}
The problem is when I click the button it is exporting aspx file. I have tried all answers in Stackoverflow and from other sources as well. However, I am not being able to correct it. I need exact reason why it is so and some possible solutions.(I tried Content-type as "application/excel", "application/vn", "application/vn-excel" as well.)
You can just follow following code, it is working fine..
protected void cmdExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=SupplierList.xls");
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter WriteItem = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlText = new HtmlTextWriter(WriteItem);
GridView1.AllowPaging = false;
DataTable dtSupplier = (DataTable)ViewState["dtSupplier"];
GridView1.DataSource = dtSupplier;
GridView1.DataBind();
GridView1.RenderControl(htmlText);
Response.Write(WriteItem.ToString());
Response.End();
}
Hope this will help you.
thanks
In aspx code i set GridLines="None"
How can i show GridLines from code behind when exporting grid-view to excel file.
protected void btnexcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=UsersReport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gridusers.DataBind();
gridusers.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();
}
In function start, set:
grdAdslCompanyAdvisers.GridLines = GridLines.Both;
after export again
grdAdslCompanyAdvisers.GridLines = GridLines.None;
Hope this helps or gives you an idea.
I am using following function to export my data to excel sheet:
string filename = "Test.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
//Get the H`enter code here`TML for the control.
yourGrid.RenderControl(hw);
//Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
Response.Write(tw.ToString());
but excel sheet is not created. Although it shows in chrome browser bottom that excel is downloading. When it finishs downloading, I click it and it says file cant be opened. Please suggest me what is wrong in it?
[EDIT]
Response.Clear();
Response.Buffer = true;
//use Response.AddHeader
Response.AddHeader("content-disposition", "attachment;filename=Test.xlsx");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvLogs.RenderControl(hw);
gvLogs.AllowPaging = false;
gvLogs.DataBind(); // bind data
Response.Output.Write(sw.ToString());
//need to call Flush and End methods
Response.Flush();
Response.End();
give same error
Epplus is a good library for excel import/export. You can check it out.
You can try this one
private void ExportToExcel(DataTable dt)
{
if (dt.Rows.Count > 0)
{
string filename = "DownloadReport.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
Change the 'Response.ContentType' value in your code as like below.
Response.ContentType = "application/vnd.xls";
Hope this will help you.
check below code
Response.Clear();
Response.Buffer = true;
//use Response.AddHeader
Response.AddHeader("content-disposition", "attachment;filename=Test.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
GridView1.AllowPaging = false;
GridView1.DataBind(); // bind data
Response.Output.Write(sw.ToString());
//need to call Flush and End methods
Response.Flush();
Response.End();