C#DataGrid Export data to Excel
来源:互联网 发布:网店美工培训班 编辑:程序博客网 时间:2024/04/27 20:37
方法1
public void DataGridToExcel(DataGrid dgExport , HttpResponse response){ //clean up the response.object response.Clear(); response.Charset = ""; //'set the response mime type for excel response.ContentType = "application/vnd.ms-excel"; //'create a string writer System.IO.StringWriter stringWrite = new System.IO.StringWriter(); //create an htmltextwriter which uses the stringwriter System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite); //instantiate a datagrid DataGrid dg As New DataGrid(); //just set the input datagrid = to the new dg grid dg = dgExport; //I want to make sure there are no annoying gridlines dg.GridLines = GridLines.None; '//Make the header text bold dg.HeaderStyle.Font.Bold = True; //If needed, here's how to change colors/formatting at the component level //dg.HeaderStyle.ForeColor = System.Drawing.Color.Black //dg.ItemStyle.ForeColor = System.Drawing.Color.Black //bind the modified datagrid dg.DataBind(); //tell the datagrid to render itself to our htmltextwriter dg.RenderControl(htmlWrite); //output the html response.Write(stringWrite.ToString); response.End();}
方法2
private void ExportAllDO() { //lvExportAll: DataGrid/GridView/ListView lvExportAll.DataSource = GetAllItemDict(); lvExportAll.DataBind(); Response.Clear(); HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.UTF8; lvExportAll.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); lvExportAll.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); }
- C#DataGrid Export data to Excel
- export data from DataGrid to Excel
- Export data to Excel from Silverlight/WPF DataGrid
- C#Export data to CSV
- Export ASP.NET DataGrid To Excel
- Export data from SQL Server to Excel
- Export large data to excel use poi
- Silverlight Export DataGrid Data
- 5 Solutions to Export Data to Excel for ASP.NET
- struts1 excel export data
- Export Data from GridView to Excel, Word, HTML with C#
- Export large data from GridView to Excel file using C#
- Export DataSets to Excel...
- export to excel
- Export GridView to Excel
- Export to excel
- Export to Excel
- EXCEL export to TXT
- UML之用例图
- 内存拷贝要考虑地址重叠问题啊!!!
- hdu 3363 ice sugar gourd
- MyEclipse修改默认的workspace路径
- poj 2418 Hardwood Species
- C#DataGrid Export data to Excel
- ubuntu 11.04 如何调开机分辨率
- Ubuntu Server 10.10安装配置Nginx+php-fpm+mysql
- java程序设计与问题解决(高级篇)-读书笔记-5
- Ubuntu 11.04更改分辨率
- poj 1860
- 类、方法、成员变量和局部变量的可用修饰符
- SQLite使用手记(转自:http://blog.bossma.cn/database/sqlite_use_record/)
- 选择排序