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.
Related
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
i am exporting gridview to excel,i am exporting successfully.but in my gridview i have some special symbols like ( •, "", ', -,_ ) these types of symbols cannot export, instead of those place i am getting like ( –, “, â€, ’)this.
how can i over come these problem. here is my code for exporting gridview to excel ?
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=demo.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvDetails.AllowPaging = false;
gvDetails.DataSource = dt1;
gvDetails.DataBind();
for (int i = 0; i < gvDetails.Rows.Count; i++)
{
GridViewRow row = gvDetails.Rows[i];
row.Attributes.Add("class", "textmode");
}
gvDetails.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();
You need to specify and provide encoding information.
Response.ContentEncoding = System.Text.Encoding.Unicode; /*c# uses UTF-16 internally*/
...
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble()); /*Include the UTF-16 header so excel will treat this as UTF-16*/
Response.Output.Write(sw.ToString());
...
Note that this is not the best solution, ideally you should use OpenXML or some other library to export to excel.
Please change your content type to below,
Response.ContentType = "application/ms-excel";
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();
}
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();