Export GridView to Excel 2007 - c#

I want to export a gridview to excel 2007,the code i'm using can import to excel 2007 with the mime type application/vnd.ms-excel (excel 2003)but i get a warning msg that says "The file you are trying to open is in a different format...",with yes and no to clic,clicking in yes the file open,buy i can't have this msg for the customers.And using the mime type for excel 2007 (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)the file doesn't even open "Excel can't open the file because the format or extansion isn't valid".
This is the code i'm using right now:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Drawing;
namespace TesteFornecedores
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
using (DataSet ds = new DataSet())
{
ds.ReadXml(Server.MapPath("~/Customers.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.BindGrid();
}
protected void ExportToExcel(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=ExcelList");
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
GridView1.AllowPaging = false;
this.BindGrid();
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex%2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = #"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
}
}
Someone know a solution the can help me open in excel 2007 this gridview?
Thanks.

You are not exporting to an actual Excel format. You're creating an HTML file, and since you're providing a MIME type that Excel knows how to handle, Excel attempts to open it. Excel does know how to read basic HTML files, but it's difficult to control the formatting and you will get that warning message.
Instead what you need to do is generate your .xlsx files is use a library that can generate them for you. Examples of this would be EPPlus and Open Office XML SDK. Note you need to steer clear of using Excel Interop based solutions, as these are not supported by Microsoft on the server side, they will be difficult to debug, and they will cause headaches.
By the way, don't think of it as "exporting a GridView". That's a bad approach. A GridView is a UI element for displaying data in HTML. Think of it instead as "how do I export this data?" In your case, think of it as exporting a DataSet or XML type data.

Response.Clear();
Response.ContentType = "application/excel";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(objectData);//objectData is binary data
Response.End();
Your response should be like this. I'm pretty sure that your code will not work, because you are not giving valid excel file to the Response, read about OleDbConnection
Use your DataSet from DataSource of the Grid. After that build OleDbConnection
OleDbConnection conn = new OleDbConnection();
string connectString = String.Format(
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR={1};'",
fileName, "YES");
conn.ConnectionString = connectString;
conn.Open();
OleDbCommand comm = new OleDbCommand();
comm.CommandText = string.Format("CREATE TABLE [{0}] ", "TableName");
Add the columns from the DataSet to the comm.CommanText. To build replica of the dataSet column structure. After that:
comm.Connection = conn;
comm.ExecuteNonQuery();
Now you have the table created with the same columns like your data set.
Now you should update the content
OleDbDataAdapter ad = new OleDbDataAdapter(
string.Format("SELECT * FROM [{0}]", "TableName"), conn);
OleDbCommandBuilder builder = new OleDbCommandBuilder(ad);
builder.QuotePrefix = "[";
builder.QuoteSuffix = "]";
// Saves the data set
ad.Update(DataSetFromTheGrid);
Now you filled the table with data.
//be aware fileName is the same as fileName in the connection string !
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fs);
excelBytes = reader.ReadBytes((int)fs.Length);
//after the returned bytes it will be good to delete the file !
Return this bytes to the Response and you have your excel file.

Related

Grid view is not being refreshed on exporting grid view to excel in asp.net

i am come up with scenario that while exporting grid view to excel its not refreshing my grid data source. Below is my code what i have done before. Below code is updating flag in database and then trying to refresh the grid data source in Fill Grid method.
protected void Process_Command(object sender, CommandEventArgs e)
{
if (!string.IsNullOrEmpty(Convert.ToString(e.CommandArgument)))
{
using (var transaction = context.Database.BeginTransaction())
{
DocumentOGP objDocumentOGP = context.DocumentOGPs.Find(Convert.ToInt64(e.CommandArgument));
objDocumentOGP.UpdationDate = DateTime.Now;
objDocumentOGP.DispatchStatusID = 2;
context.DocumentOGPs.Add(objDocumentOGP);
context.Entry(objDocumentOGP).State = System.Data.Entity.EntityState.Modified;
context.SaveChanges();
transaction.Commit();
FillGrid();
}
ExportToExcel(Convert.ToInt64(e.CommandArgument));
}
}
Below is the Export to excel method.
public void ExportToExcel(Int64 OGPCode)
{
DataTable dtPickList =GeneralQuery.GetDocumentPickList(OGPCode);
if (dtPickList != null && dtPickList.Rows.Count>0)
{
//Create a dummy GridView
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dtPickList;
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=Inbox.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//Apply text style to each Row
GridView1.Rows[i].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();
GridView1.DataSource = null;
Response.Write(Request.RawUrl.ToString());
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
Please help me what i am doing wrong. Thanks
Finally i found the way to refresh the grid view while exporting the grid view to excel. I just create new web form and put ExportGridToExcel() method in Page_Load and on button click it refresh the grid view data source and open new tab to download excel file. Below is my code.
protected void Process_Command(object sender, CommandEventArgs e)
{
if (Session["Status"] != "Refreshed")
{
if (!string.IsNullOrEmpty(Convert.ToString(e.CommandArgument)))
{
using (var transaction = context.Database.BeginTransaction())
{
DocumentOGP objDocumentOGP = context.DocumentOGPs.Find(Convert.ToInt64(e.CommandArgument));
objDocumentOGP.UpdationDate = DateTime.Now;
objDocumentOGP.DispatchStatusID = 2;
context.DocumentOGPs.Add(objDocumentOGP);
context.Entry(objDocumentOGP).State = System.Data.Entity.EntityState.Modified;
context.SaveChanges();
transaction.Commit();
FillGrid();
Session["OGPCode"] = Convert.ToString(e.CommandArgument);
}
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "OpenWindow", "window.open('http://localhost:56430/DownLoadExcel','_newtab');", true);
}
}
}
Below is my Download excel file web-form and its implementation
public partial class DownLoadExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (!string.IsNullOrEmpty(Convert.ToString(Session["OGPCode"])))
{
ExportToExcel(Convert.ToInt64(Session["OGPCode"]));
Session["OGPCode"] = null;
}
}
}
public void ExportToExcel(Int64 OGPCode)
{
DataTable dt = GeneralQuery.GetDocumentPickList(OGPCode);
//create a new byte array
byte[] bin;
string FileName = "Pick-List-" + DateTime.Now.ToString();
//create a new excel document
using (ExcelPackage excelPackage = new ExcelPackage())
{
//create a new worksheet
ExcelWorksheet ws = excelPackage.Workbook.Worksheets.Add(FileName);
//add the contents of the datatable to the excel file
ws.Cells["A1"].LoadFromDataTable(dt, true);
//auto fix the columns
ws.Cells[ws.Dimension.Address].AutoFitColumns();
//loop all the columns
for (int col = 1; col <= ws.Dimension.End.Column; col++)
{
//make all columns just a bit wider, it would sometimes not fit
ws.Column(col).Width = ws.Column(col).Width + 1;
var cell = ws.Cells[1, col];
//make the text bold
cell.Style.Font.Bold = true;
//make the background of the cell gray
var fill = cell.Style.Fill;
fill.PatternType = ExcelFillStyle.Solid;
fill.BackgroundColor.SetColor(ColorTranslator.FromHtml("#BFBFBF"));
//make the header text upper case
cell.Value = ((string)cell.Value).ToUpper();
}
//convert the excel package to a byte array
bin = excelPackage.GetAsByteArray();
}
//clear the buffer stream
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;
//set the correct contenttype
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
//set the correct length of the data being send
Response.AddHeader("content-length", bin.Length.ToString());
//set the filename for the excel package
Response.AddHeader("content-disposition", "attachment; filename=\"" + FileName + ".xlsx\"");
//send the byte array to the browser
Response.OutputStream.Write(bin, 0, bin.Length);
//cleanup
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
When you update GridView or any control values dynamically you should render and export it to other application.
Sample: check with RenderControl method of GridView.
private void ExportGridToExcel()
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName = "xyz.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);
GridView1.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.End();
}

How to export excel from clicking chart legend?

I have created a few charts dynamically and would like to export the data to excel using closedxml.
I created a custom item on legend and when I click it, the page posts back and the event handler was raised properly. But I couldn't get the save window to pop up, hence not being able to save the excel file. Help me please?
I am using C# on asp.NET Framework 4.5.
Here's a simplified version of my code.
protected void Page_Load(object sender, EventArgs e)
{
Add_Chart();
}
public void Add_Chart()
{
System.Web.UI.DataVisualization.Charting.Chart Add_Chart1 = new System.Web.UI.DataVisualization.Charting.Chart();
//Create Chart here
//Add chart to htmltablecell
Add_Chart1.Click += Chart_Legend_Click;
}
protected void Chart_Button_Click(object sender, ImageMapEventArgs e)
{
string sheetname = "trial";
string filename = "trial.xlsx";
var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add(sheetname);
HttpResponse httpResponse = Response;
httpResponse.Clear();
httpResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
httpResponse.AddHeader("content-disposition", "attachment;filename=" + filename);
//Create header and data rows here
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
workbook.SaveAs(memoryStream);
memoryStream.WriteTo(httpResponse.OutputStream);
memoryStream.Close();
}
httpResponse.End();
}

ASP Table to Excel

When viewing ASPX in website, I got a table with few text in the cells, e.g.:
tab.Rows[1].Cells[1].innerHtml = "Booked :"
(In a lot of rows and cells but with different text in each cell)
Now I just want to click a button, and data in the table will be downloaded into an Excel file.
Table ID : tab
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Table tbl = new Table();
TableRow tr = new TableRow();
TableCell tcel = new TableCell();
tcel.Text = "id";
tr.Cells.Add(tcel);
TableCell tcel1 = new TableCell();
tcel1.Text = "id1";
tr.Cells.Add(tcel1);
tab.Rows.Add(tr);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string filename = "ExportExcel.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 = tbl;
//dgGrid.DataBind();
//Get the HTML for the control.
tab.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
Modified with watraplion answer, but still not answer..
Error at:
DataTable dt = dt; //Use of unassigned local variable 'dt'
Try this :
aspx Design View :
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="Table1" runat="server">
</asp:Table>
</div>
<div>
<asp:Button ID="btnExport" onclick="btnExport_Click" Text="Export" runat="server">
</div>
</form>
</body>
aspx.cs ( code behind )
protected void Page_Load(object sender, EventArgs e)
{
TableRow tr = new TableRow();
TableCell tcel = new TableCell();
tcel.Text = "id";
tr.Cells.Add(tcel);
TableCell tcel1 = new TableCell();
tcel1.Text = "id1";
tr.Cells.Add(tcel1);
Table1.Rows.Add(tr);
}
protected void btn_Click(object sender, EventArgs e)
{
string filename = "ExportExcel.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
//Get the HTML for the control.
Table1.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
First to say that this what you are trying to do is not export to excel, but you send a html with a wrong headers to trick browser to open this content with excel.
This solution has many problems, excel it self will warn user that content is different from extension, because you send html and your response headers are saying that this is a excel file. And I can bet that some antimalware software on client or something similar on server, will block this response since serving different content than declared in headers is known malware behavior.
It's far more better and easier to use dedicated excel library, for example EPPlus, look here for ASP.NET ashx handler that exports DataTable to real excel file :
https://stackoverflow.com/a/9569827/351383

export GridView to excel #2

I am binding a GridView to a sqldatasource and then on _rowcreated event doing some validations and when the row does not meet requirements I am hiding it using e.Row.Visible = false;
This works fine and only displays the correct rows in the gridview. Now I have a button to export to excel and that works great with the exception of exporting also the hidden rows. I do not want the hidden rows exported.
Is there a way that i can tell the gridview to not add that row instead of hiding it?
Is there a simple method to delete all hidden rows before i run the export?
Can i not add the hidden rows during the export? As you can see in the code below I tried to do this one, but it does not recognize whether the row is visible or not.
Export Code:
public static void Export(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Create a form to contain the grid
Table table = new Table();
gv.GridLines = GridLines.Both;
table.GridLines = gv.GridLines;
//table.BackColor = Color.Yellow;
// add the header row to the table
if (gv.HeaderRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
//color the header
table.Rows[0].BackColor = gv.HeaderStyle.BackColor;
table.Rows[0].ForeColor = gv.HeaderStyle.ForeColor;
}
// add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
if (row.Visible == true)
{
GridViewExportUtil.PrepareControlForExport(row);
table.Rows.Add(row);
}
}
// color the rows
bool altColor = false;
for (int i = 1; i < table.Rows.Count; i++)
{
if (!altColor)
{
table.Rows[i].BackColor = gv.RowStyle.BackColor;
altColor = true;
}
else
{
table.Rows[i].BackColor = gv.AlternatingRowStyle.BackColor;
altColor = false;
}
}
// render the table into the htmlwriter
table.RenderControl(htw);
// render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html
I have never used GridViewExportUtil, but why not edit his code?
private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
//-----------------------------
// * my addition
if (!current.Visible) continue;
//-----------------------------
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
}
else if (current is ImageButton)
{
//...
Here is the simple implementation to Export GridView to Excel:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class ExportGridView : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;
filename=FileName.xls");
Response.Charset = "";
// If you want the option to open the Excel file without saving than
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
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();
}
}
Reference blog
In the first place why you have hidden rows from your gridview? Only get data that you wanted. And use this method to export;
void ExportToExcel(GridView grdData, string filename)
{
grdData.BorderStyle = BorderStyle.Solid;
grdData.BorderWidth = 1;
grdData.BackColor = Color.WhiteSmoke;
grdData.GridLines = GridLines.Both;
grdData.Font.Name = "Verdana";
grdData.Font.Size = FontUnit.XXSmall;
grdData.HeaderStyle.BackColor = Color.DimGray;
grdData.HeaderStyle.ForeColor = Color.White;
grdData.RowStyle.HorizontalAlign = HorizontalAlign.Left;
grdData.RowStyle.VerticalAlign = VerticalAlign.Top;
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Charset = "";
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename+ "\"");
using (var sw = new StringWriter())
{
using (var htw = new HtmlTextWriter(sw))
{
grdData.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}

Column Values Customisation- Export Excel in ASP.NET

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();
}

Categories

Resources