I have to export gridview in to the excel sheet 2003. With my code I can Export the data into eXcel Sheet directly. But If there is a null value for the column, Then how do I replace with empty string in Excel sheet. There is a date column which is null, is there any event handler that occurs between export button click and the loading the data into excel. If there is one I can compare the values in the database and can replace the null value with empty String. in the evnt handler.
Please point me in right direction.
Thank you
On click of the export button, you can iterate through your grid rows and replace whatever you need. For example, the code below will replace : with .:
protected void btnExport_Click(object sender, EventArgs e)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
string attachment = "attachment; filename=SummaryReport" + DateTime.Now.ToString() + ".xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
foreach (GridViewRow grdRow in grdProjectTasks.Rows)
{
Label lblActualDuration = (Label)grdRow.FindControl("lblActualDuration");
lblActualDuration.Text = lblActualDuration.Text.Replace(":", ".");
}
grdProjectTasks.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
Related
I am Creating a web app in which i have a master page with 5 other page
welcome
user
employee
admin
company
Every page have a gridview (i.e I have 5 gridviews) gridview contain different data for different page
and i used this to export the gridview data into excel
protected void imgexcel_Click(object sender, ImageClickEventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
row.BackColor = System.Drawing.Color.White;
row.Attributes.Add("class", "textmode");
}
GridView1.RenderControl(hw);
string style = #"<style> .textmode { mso-number-format:\#; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
this is my normal coding for export
but i want to add button on my masterpage and exporting of data depends on which page is user using (if the user is on welcome page data of welcome page must be exported on button click and if he is on user page and click on button click the data of user must be exported and so on)
string s = this.Page.Request.FilePath; // "/Welcome.aspx"
You can use this code for holding the page name in a string so you can use this string in a switch-case structure (in button OnClick).
switch(string)
{
case:"welcome": //...
case:"user": //...
case:"employee": //...
}
Hope it helps.
You can use FindControl to find the GridView on every page. You just have to make sure they all have the same ID.
protected void Button1_Click(object sender, EventArgs e)
{
//first find the ContentPlaceHolder control
ContentPlaceHolder contentPlaceHolder = FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
//then find the GridView control inside the ContentPlaceHolder
GridView gridview = contentPlaceHolder.FindControl("GridView1") as GridView;
//or the same as above but in a single line of code
GridView gridview = FindControl("ContentPlaceHolder1").FindControl("GridView1") as GridView;
//do stuff with the GridView
gridview.Visible = false;
}
protected void ExportToExcel(object sender, EventArgs e)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;filename=Detail.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htm = new HtmlTextWriter(sw);
Repeater repAllUsers = this.rptrRegistrationReport;
repAllUsers.DataSource = objAdmin.Getregdetails(start, enddate, usrtype);
repAllUsers.DataBind();
repAllUsers.RenderControl(htm);
Response.Write("<Table border='1'" + "cellSpacing='0' cellPadding='0' " +"style='font-size:12.0pt; '>");
Response.Write(sw.ToString());
Response.End();
}
All data are export proper but only date column showing #####.How to show date in excel file.
I am using following code to export my GridView to Excel:
string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvReports.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
This code is copied from this site. I have followed all the instructions but after executing nothing happened. No exception (Except Thread.Abort which is, I think, because of Response.End()).
I have also used Response.Flush() BUT nothing is happening no exception or file etc.
Thanks.
EDIT Now Code Is like following:
My GridView is in Control. The control is on asp.net page and that page has following method.
public override void VerifyRenderingInServerForm(Control control)
{
}
Click event of button in ascx control:
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
ExportToExcel();
}
private void ExportToExcel()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvReports.AllowPaging = false;
gvReports.DataBind();
gvReports.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
gvReports has no link, checkbox etc. 5 bound columns and 1 button.
Nothing is happening.
itextsharp dlls ,you will get the best method to do that
Click here
I'm using MVC3 for my solution, but the code I'm using to push data out to an excel file is:
var grid = new GridView
{
DataSource = from lineItem in rows
select new
{
lineItem.ProjectName,
lineItem.Sat,
lineItem.Sun,
lineItem.Mon,
lineItem.Tue,
lineItem.Wed,
lineItem.Thu,
lineItem.Fri
}
};
var fileName = string.Format("{0}:{1}", userName, timesheetDate);
grid.DataBind();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xls");
Response.ContentType = "application/msexcel";
var sw = new StringWriter();
var htw = new HtmlTextWriter(sw);
grid.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
The immediate differences that I see are:
I take my data and create a new GridView
Response.ClearContent() instead of a Response.Clear()
Response.Write instead of Response.Output.Write
Everything else looks the same, and we both are using a click event to call a void function. So I'm guessing that if you make those few changes, your code should start working for you.
It's been a while since I implemented this solution and I couldn't find the sites that I got this off of so I can't show you my original sources. Sorry about that.
UserControl code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
this.gridView.DataSource = this.SqlDataSource1;
this.gridView.DataBind();
}
private void excel_Export()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.gridView.AllowPaging=false;
this.gridView.DataBind();
this.gridView.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
protected void Button1_Click(object sender, EventArgs e)
{
excel_Export();
}
}
User Control Hosting page:
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
</asp:Content>
You really dont need following event in code behind of User control hosting page, because excel export is being done via user control which contains grid view and button.
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
Hopefully this will you help to solve the issue.
I'm exporting a GridView to Excel with the following code:
protected void export_OnClick(object sender, EventArgs e)
{
string style = #"<style> .text { mso-number-format:\#; } </style> ";
// Let's hide all unwanted stuffing
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
// Let's bind data to GridView
BindGrid();
//Change the color back to white
GridView1.HeaderRow.Style.Add("background-color", "#ffffff");
//Apply color to the header
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#e0e0e0");
}
// Let's output the Gridview
Response.Clear();
Response.ContentType = "application/vnd.xls";
Response.AddHeader("content-disposition", "attachment;filename=" + reportid + ".xls");
StringWriter swriter = new StringWriter();
HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
GridView1.RenderControl(hwriter);
Response.Write(style);
Response.Write(swriter.ToString());
Response.End();
}
Now I need to append a Name on the first row, Date & Time on the second row and possibly some other information on the third row. I will also like to leave a row between the actually GridView data and this information. Is this possible? If so, would you be so kind and provide some examples on how can I accomplish this. Thank you.
Just go ahead and add it. Add a carriage return after each to make Excel advance to the next row (use tabs to advance to the next cell).
Response.Write(style);
Response.Write("Pretty Excel Export File\n");
Response.Write("Generated on " + DateTime.Now.ToShortDateString() + "\n\n");
Response.Write(swriter.ToString());
Response.End();
I have a GridView and I intend to export it into .xls file. Paging is enabled in this gridview. The codes I am currently using can only export the first page of the gridview.
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=DataTable.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//As you notice, below I tried to disable the paging yet it's unsuccessful
//FYI I am able to really prevent first column, header row, and footer row to
//be exported through this
gvGridView.Columns[0].Visible = false;
gvGridView.HeaderRow.Visible = false;
gvGridView.FooterRow.Visible = false;
gvGridView.AllowPaging = false;
for (int i = 0; i < gvGridView.Rows.Count; i++)
{
gvGridView.Rows[i].Attributes.Add("class", "textmode");
}
gvGridView.RenderControl(hw);
string style = #"<style> .textmode { mso-number-format:\#; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
I noticed this question was previously asked yet there was no correct answer.
Any help is appreciated.
Thank's
You should rebind your GridView after you specify gvGridView.AllowPaging = false;, then export. Otherwise .RenderControl(hw); will only be rendering the currently selected GridView page.
Just make a GridView.DataBind() after change the AllowPaging property.