The HTML and the code used in the demo, can be found at the link below.
http://csharp-video-tutorials.blogspo...
Link for csharp, asp.net, ado.net, dotnet basics and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat...
In this video, we will discuss about exporting gridview data to excel.
Step 1: Create an asp.net web application project.
Step 2: Drag and drop a gridview control and a button control on webform1.aspx. Autoformat the gridview control to use "BrownSugar" scheme. Set the following properties of the button control.
ID="btnExportToExcel"
Text="Export to excel"
OnClick="btnExportToExcel_Click"
Step 3: Copy and paste the following code in webform1.aspx.cs. The code is well commented and is self-explanatory.
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack) { BindData(); }
}
private void BindData()
{ string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; using (SqlConnection con = new SqlConnection(CS)) { SqlDataAdapter da = new SqlDataAdapter("Select * from tblEmployee", con); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); }
}
protected void btnExportToExcel_Click(object sender, EventArgs e)
{ // Clear all content output from the buffer stream Response.ClearContent(); // Specify the default file name using "content-disposition" RESPONSE header Response.AppendHeader("content-disposition", "attachment; filename=Employees.xls"); // Set excel as the HTTP MIME type Response.ContentType = "application/excel"; // Create an instance of stringWriter for writing information to a string System.IO.StringWriter stringWriter = new System.IO.StringWriter(); // Create an instance of HtmlTextWriter class for writing markup // characters and text to an ASP.NET server control output stream HtmlTextWriter htw = new HtmlTextWriter(stringWriter); // Set white color as the background color for gridview header row GridView1.HeaderRow.Style.Add("background-color", " #FFFFFF"); // Set background color of each cell of GridView1 header row foreach (TableCell tableCell in GridView1.HeaderRow.Cells) { tableCell.Style["background-color"] = " #A55129"; } // Set background color of each cell of each data row of GridView1 foreach (GridViewRow gridViewRow in GridView1.Rows) { gridViewRow.BackColor = System.Drawing.Color.White; foreach (TableCell gridViewRowTableCell in gridViewRow.Cells) { gridViewRowTableCell.Style["background-color"] = " #FFF7E7"; } } GridView1.RenderControl(htw); Response.Write(stringWriter.ToString()); Response.End();
}
Step 4: Run the application and click on "Export to excel" button. You will get an error stating - Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server. This is because, we are calling GridView1.RenderControl(htmlTextWriter) method. This causes .net to think that a Server-Control is being rendered outside of a Form. To fix this error, we need to override VerifyRenderingInServerForm() method. Copy and paste the following method in webform1.aspx.cs.
public override void VerifyRenderingInServerForm(Control control)
{
}
At this point, run the application and click on "Export to excel button. The data gets exported to excel as expected. However, when you try to open the file you will get a warning message stating - The file you are trying to open, 'Employee.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?
According to MSDN - The warning message is a user-notification function that was added to Excel 2007. The warning message can help prevent unexpected problems that might occur because of possible incompatibility between the actual content of the file and the file name extension.
One way to resolve this error, is to edit the registry. Microsoft knowledge base article related to this error
http://support.microsoft.com/kb/948615
If you want to export the same data to microsoft word, there are only 2 changes that you need to do.
First Change: Change the file extension to ".doc" instead of ".xls"
Response.AppendHeader("content-disposition", "attachment; filename=Employees.doc");
Second Change: Set the content type to word
Response.ContentType = "application/word";
asp.net core docker Export gridview to excel in asp.net - Part 57 | |
| 251 Likes | 251 Dislikes |
| 61,470 views views | 524K followers |
| Education | Upload TimePublished on 16 Apr 2013 |
Related keywords
wcf vs web api,sql server management studio,c#.net versions,webkinz,webadvisor,craigslist nj,asp.net core 3,ajax players,csharp corner,asp.net machine account,asp.net core dependency injection,c#.net developer,document.addeventlistener,ajax ontario,grid view xamarin forms,word search,c#.net online compiler,asp.net mvc tutorial,content lab la nacion,tutorialspoint python,word scrambler,c#.net interview questions,services briefcase,data entry,ajax jquery,craigslist ny,server memes,document ready,services online,server resume,tutorialspoint javascript,gridview rowcommand,mvcc connect,services technologies gps,grid view html,document js,server jobs,website,mvc tutorial,costco hours,word scramble,wcf service application,tutorialspoint spring,serverless,document ready js,wcf cat,services & training hse colombia sas,citibank,ajax parking,sql database,mvconnect,asp.net guida,cunyfirst,csharp online,c#.net notes pdf,http://asp.net,server 2019,ado.net descargar,chernobyl,ado.net visual studio 2019,word finder,data star trek,ado.net visual studio 2017,word generator,contentamiento,sql like,wcfm,tutorial gateway,grid view c# asp.net,asp.net core mvc,ajax soccer,data scientist salary,wcfi,words that start with z,words that start with x,data mining,craigslist,word reference,tutorialspoint python 3,sql developer,webster,services transmission company sas,sql group by,gridviewrow,mvc near me,c#.net framework,mvc map,asp.net download,sql between,ado.net c# pdf,wcf 2019 nba,ado.net ventajas y desventajas,wcf tutorial,tutorials dojo,central park 5,ado.net c#,gridview android example,server status,server jobs nyc,asp.net core swagger,gridview css,gridview yii2,server error in '/' application,data entry jobs,services windows,asp.net core 3.0,sql injection,gridview asp.net,tutorialspoint c#,c#.net vs asp.net,word online,ado.net entity data model,sql insert,tutorialspoint tableau,contenta,sqlyog,sql server,tutorials by a,servicenow,c#.net to vb.net,c#.net w3schools,contento,csharp array,c#.net projects,csharp enum,asp.net core 2. guida completa per lo sviluppatore,gridview c# asp.net,grid view devexpress mvc,server status ffxiv,c#.net basics,document.createelement,cheap flights,asp.net core hosting,tutorialspoint java,webmd,documentary,ado.net dataset,tutorial meaning,cnn,website builder,asp.net web api,weber grills,sql server 2017,data breach,asp.net core download,webster bank,gridview column width,document.getelementbyid,grid view en android studio,tutorial teacher,ajax post,services fms publish announcement,gridview devexpress,services.msc no abre,content manager assetto corsa,content creator,csharp operator,content manager,word counter,server duties,contentura,tutorial on variational autoencoders,csharp random,c#.net latest version,mvcsd,mvcsc,ado.net pdf,asp.net core configuration,contentful,mvc medical,wcfs international curriculum,grid view selectedindexchanged in asp.net,wcf api,server jobs near me,webex,sql meaning,sql tutorial,sql commands,data universe,ado.net entity framework,content lab,c#.net pdf,word art,document.queryselectorall,contentar,word cloud,contento significado,content id,ajax greek,csharp tutorial,ado.net connection,asp.net identity,grid view devexpress,gridview android,databricks,gridview bootstrap,content words,grid view react native,data analyst,wcf web service,mvc webadvisor,web store,mvctc,wordpress login,content //com.android.browser.home/,ado.net core,document.ua,gridview datasource,wcf one piece,document flow,c#.net core,weber grill parts,webtoon,tutorials by hugo,mvc design pattern,servers for minecraft,ado.net tutorial,grid view android,document onload,services angular,tutorial definition,tutorial on excel,asp.net cos'è,csharp assembly,sql join,asp.net core web api,wccftech,gridview flutter,data warehouse,asp.net zero,csharp foreach,sql date format,chase,c#.net tutorial pdf,serverless architecture,data science,wcf c#,grid view xamarin,server books,ado.net mysql,data scientist,ajax deadpool,cvs,wcf soap,wcf test client,cool math games,servicestack,asp.net core identity,sql union,asp.net core logging,gridview devexpress c#,grid view bootstrap,documents,wcf nba,wordpress,csharp download,wcfi foundation,wcf authentication,tutorials near me,web of dreams,documents google,serverminer,gridview pagination,ajax cleaner,tutorial for windows 10,webassign,ado.net vs entity framework,data lake,csharp list,gridview height,grid view flutter,asp.net mvc,asp.net core tutorial,sqlite,gridview c#,ajax roster,mvc architecture,http://ado.net,server hosting,document.cookie,ajax dish soap,grid view android example,capital one,grid view c#,server rack,tutorialspoint html,csharp interface,contento sinonimos,webroot,tutorialspoint reactjs,ajax request,wcf dragon ball,asp.net core 2.2,content marketing,asp.net core signalr,services manager,word to pdf,mvc framework,ajax paving,data analysis,tutorialspoint spring boot,c#.net concatenate strings,csharp online compiler,data analyst salary,services tag dell,csharp switch,c#.net mvc,csharpstar,tutorialspoint,ajax meaning,contentina,csharp-video-tutorials.blogspot,document.write,tutorial mission gta online,csharp string format,words per minute test,asp.net core github,document online,ajax fc,sql formatter,credit karma,services group,wordscapes,wcf ria services,calculator,data analytics,document.getelementsbytagname,services google play apk,asp.net core 3 release date,c#.net tutorial,server job description,webcam,mvc hours,webmd symptom,ajax call,asp.net core,server pro,webcrims,services sas,document.getelementsbyclassname,tutorialspoint java compiler,datadog,document object model,csharp to json,grid view asp.net bootstrap,college football,csharp dictionary,tutorial.mc-complex,tutorialspoint sql,server side rendering,mvc nj,tutorialspoint spark,ado.net oracle,csharp to vb.net,document.queryselector,webstaurant,tutorial systems,datacamp,tutorialsystems,grid view yii2,ajax jersey,asp.net core razor pages,asp.net core environment variables,century 21,services consultores,services consulting,gridview hide column,services.msc,words with friends cheat,ajax marvel,tutorial synonym,sql update,asp.net tutorial,ado.net entity data model visual studio 2019,words with friends,mvc2,grid view css,ado.net entity data model visual studio 2017,c#.net tutorialspoint,chase online,costco,gridview bootstrap 4,ajax tavern,tutorialsteacher,ajax javascript,grid view laravel,c#.net programs,database,word of the day,services desk,data visualization,grid view scroll bar in asp.net,content traduccion,grid view asp.net,mvc pattern,ado.net sql server,mvcu,contento es un adjetivo,asp.net core middleware,gridview xamarin forms,tutorial music,mvcc,webmail,mvci,
Không có nhận xét nào:
Đăng nhận xét