×

gridview控件怎么转换到excel

gridview控件怎么转换到excel(求gridview导出为excel问题解决)

admin admin 发表于2023-12-24 07:53:04 浏览33 评论0

抢沙发发表评论

各位老铁们,大家好,今天由我来为大家分享gridview控件怎么转换到excel,以及求gridview导出为excel问题解决的相关问题知识,希望对大家有所帮助。如果可以帮助到大家,还望关注收藏下本站,您的支持是我们最大的动力,谢谢大家了哈,下面我们开始吧!

本文目录

求gridview导出为excel问题解决

DataGird导出EXCEL的几个方法(WebControl)using System;using System.Data;using System.Text;using System.Web;using System.Web.UI;using System.Diagnostics;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.Data.SqlClient;using System.Collections;namespace bookstore{ /// 《summary》 /// myExcel 的摘要说明。 /// 《/summary》 public class myExcel { public myExcel() { }/// 《summary》 /// 将DATAGRID导出为EXCEL文件方法一, /// 参数是:要导出的DATAGRID的ID和要保存下来的EXCEL文件名 /// 《/summary》 /// 《param name="myPage"》page《/param》 /// 《param name="dg"》datagrid《/param》 /// 《param name="name"》filename《/param》 private void OutExcel(Page myPage,DataGrid dg,string name) { HttpResponse Response; Response=myPage.Response; string name1="attachment;filename="+name+".xls"; dg.Visible=true; Response.Clear(); Response.Buffer= true; Response.Charset="GB2312"; Response.AppendHeader("Content-Disposition",name1); Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); Response.ContentType ="application/ms-excel"; dg.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); dg.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End(); }/// 《summary》 /// 将DATAGRID导出为EXCEL文件方法二, /// 参数是:要导出的DATAGRID的ID和要保存下来的EXCEL文件名 /// 《/summary》 /// 《param name="myPage"》page《/param》 /// 《param name="ctl"》datagrid《/param》 /// 《param name="filename"》filename《/param》 public void ExportToExcel(Page myPage,DataGrid ctl,string filename) { HttpResponse Response; Response=myPage.Response; bool CurrCtlVisible=ctl.Visible; ctl.Visible=true; Response.AppendHeader("Content-Disposition","attachment;filename="+filename+".xls"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); Response.ContentType = "application/ms-excel"; ctl.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw); ctl.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); ctl.Page.EnableViewState = true; ctl.Visible=CurrCtlVisible; } private void DgOutExcel(Page myPage,DataGrid dg,string name) { HttpResponse Response; Response=myPage.Response; string name1="attachment;filename="+name+".xls"; dg.Visible=true; Response.Clear(); Response.Buffer= true; Response.Charset="GB2312"; Response.AppendHeader("Content-Disposition",name1); Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); Response.ContentType ="application/ms-excel"; dg.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); dg.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End(); }#region 导出EXCEL,用DATASET public string myExportString(DataGrid DG,DataSet ds) { string HTstring="《table》《tr》"; string Fieldstring=""; ArrayList myAL=new ArrayList(); string sRows="《tr》"; for(int i=0;i《DG.Columns.Count;i++) { HTstring+="《td》"+DG.Columns.HeaderText+"《/td》"; Fieldstring+="《td》"+((System.Web.UI.WebControls.BoundColumn)(DG.Columns)).DataField+"《/td》"; myAL.Add(((System.Web.UI.WebControls.BoundColumn)(DG.Columns)).DataField); } for(int k=0;k《ds.Tables.Rows.Count;k++) { foreach(string field in myAL) { sRows+= "《td》"+ds.Tables+"《/td》"; } sRows+="《/tr》"; } HTstring+="《/tr》"+sRows+"《/table》"; return HTstring; }public void SaveToExcel(Page myPage, string myExportString,string myFileName) { HttpResponse resp; resp=myPage.Response; resp.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); resp.AppendHeader("Content-Disposition","attachment;filename="+myFileName+".xls"); resp.ContentType="application/ms-excel"; resp.Write(myExportString); resp.End(); //resp.Clear(); //resp.Close(); }public void Export(DataGrid myDG,DataTable dt,HttpResponse response) { // clean up response object response.Clear(); response.Charset = ""; response.Charset = "UTF-8"; // response.ContentEncoding = System.Text.Encoding.Default; response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); // set response object’s mime type // response.ContentType = "application/vnd.ms-excel"; response.AppendHeader("Content-Disposition","attachment;filename=mytest.xls"); System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw); DataGrid dg = new DataGrid(); dg.AllowPaging = false; dg.AllowSorting = false; ArrayList myAL=new ArrayList(); for(int i=0;i《myDG.Columns.Count;i++) { // dg.Columns.AddAt(i,myDG.Columns); // dg.Columns.HeaderText; myAL.Add(((System.Web.UI.WebControls.BoundColumn)(myDG.Columns)).DataField); for(int k=0;k《dt.Columns.Count;k++) { foreach(string field in myAL) { if(dt.Columns.ColumnName==field) { dt.Columns.HeaderText; } } } } dg.DataSource = dt; dg.ShowHeader = true; dg.HeaderStyle.BackColor = System.Drawing.Color.DarkGray; dg.HeaderStyle.ForeColor = System.Drawing.Color.White; dg.HeaderStyle.Font.Bold = true; dg.AlternatingItemStyle.BackColor = System.Drawing.Color.LightGray; dg.DataBind(); dg.RenderControl(htw); response.Write(sw.ToString()); response.End(); } #endregion}}

gridview导出excel

参考以下:1)网上直接搜索DGV保存到Excel2)附上链接,同样是百度知道注意:1)如果缺少提示缺少office程序集,需要在下载一个,然后添加引用和引入命名空间

GridView 数据导出到Excel 里面,因为我数据可能有几百条,所有导出到Excel里,我想在Excel上面能分页

说实在, 我对ASP.NET2不是很懂,下面这些方法,可以试试!一是使用专门用于导出为excel表的一些浏览器对象组件,插件等。如“Microsoft Excel 11.0 Object Library”这是2003版的二是一种较简单通用的做法,输出为Txt/HTML纯文本格式的,你把整个excel表做好(包含你要的标题啊什么的),然后“文件”另存为HTML文件,对这个保存的HTML版进行分析,或许你会了解其中的一些原理。 三、如果是把Gridview控件的数据导出到excel,可以直接用该控件自带的功能!(即不需要用到excel的组件的)。下面来自微软社区(social.microsoft.com)的 public void Export(string fileName, GridView gv) { Response.Clear(); //Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.ContentType = "application/ms-excel"; //"application/ms-word"; using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { this.GridView1.RenderControl(htw); // Create a table to contain the grid Table table = new Table(); // include the gridline settings table.GridLines = gv.GridLines; // add the header row to the table if (gv.HeaderRow != null) { PrepareControlForExport(gv.HeaderRow); table.Rows.Add(gv.HeaderRow); } // add each of the data rows to the table foreach (GridViewRow row in gv.Rows) { PrepareControlForExport(row); table.Rows.Add(row); } // add the footer row to the table if (gv.FooterRow != null) { PrepareControlForExport(gv.FooterRow); table.Rows.Add(gv.FooterRow); } // 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(); } } } /// 《summary》 /// Replace any of the contained controls with literals /// 《/summary》 /// 《param name="control"》《/param》 private void PrepareControlForExport(Control control) { for (int i = 0; i 《 control.Controls.Count; i++) { Control current = control.Controls; if (current is LinkButton) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text)); } else if (current is ImageButton) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText)); } else if (current is HyperLink) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text)); } else if (current is DropDownList) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text)); } else if (current is CheckBox) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False")); } if (current.HasControls()) { PrepareControlForExport(current); } } }}--------------------------------------------------------------------------------Microsoft Online Community Support***隐藏网址***

gridview怎么导出到excel

引用的别人的:Gridview导出为Excel 尝试了一下Gridview导出为Excel,原本以为很简单,可是真正应用起来还是不太好弄的,呵呵,所想非所得。总结了一下应该注意下面几点:1.由于gridview的内容可能是分页显示的,因此,这里在每次导出excel时,先将gridview的allowpaging属性设置为false,然后databind()一下,确保搂到所有数据;2.不用单独设置导出的路径,导出时会弹出对话框让你确认保存位置;3.要写一个空的VerifyRenderingInServerForm方法(必须写),以确认在运行时为指定的ASP.NET 服务器控件呈现HtmlForm 控件;4.导出后别忘记再重新设置其allowpaging属性; 当我把这些都设置好以后,点击,出现了 只能在执行 Render() 的过程中调用 RegisterForEventValidation(RegisterForEventValidation can only be called during Render(); ) 的错误,又检查代码,没发现问题啊,搞了一会弄不出来,然后搜索了一下,发现了解决办法:修改你的aspx文件中的:《%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true" CodeFile="SysUser.aspx.cs" Inherits="Autho_SysUser2" %》增加红色的部分就ok了。

如何将gridview的数据导出到EXCEL

给你个思路,分页的话,在导出时先关闭分页,绑数据,导出后,再打开分页方法1简单: public void ToExcel()//整个GRIDVIEW导出到EXCEL { string filename="数据表" + DateTime.Now.ToString("yyyyMMdd") + ".xls"; string style = @"《style》 .text { mso-number-format:\@; } 《/script》 "; //解决第一位字符为零时不显示的问题 this.GridView1.AllowPaging = false;//关闭分页 this.GridView1.DataBind();//绑定数据 filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);//解决导出EXCEL时文件名为汉字时乱码的问题 Response.ClearContent; Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.ContentType = "application/excel"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename); System.IO.StringWriter sw = new System.IO.StringWriter();//定义一个字符串写入对象 HtmlTextWriter htw = new HtmlTextWriter(sw);//将html写到服务器控件输出流 this.GridView1.RenderControl(htw);//将控件GRIDVIEW中的内容输出到HTW中 Response.Write(style); Response.Write(sw); Response.End(); this.GridView1.AllowPaging = true; }***隐藏网址***

vs2008中如何将同一页面中的两个GridView导出到同一个Excel

两个办法:1.一个是将两个gridview的内容以html页面的table形式输出,在页面中添加一句<%response.ContentType ="application/vnd.ms-excel"%>;2.第二个方案就是在后台将两个数据源的数据都以数据的方式通过office的excel.dll来存储到一个excle文件中。具体的网上的demo很多,还有些现成的插件。 方法一的优点在于快,代码少,方法二的优点是灵活性号,可以控制excel的样式,行列等属性。

c#中 如何将gridview中数据导出到已经存在的Excel

把DataGrid的数据,导出到excel中1:在页面的类中添加publicoverridevoidVerifyRenderingInServerForm(Controlcontrol){}2:写上如下代码:Response.ContentType="application/vnd.ms-excel";System.IO.StringWritertw=newSystem.IO.StringWriter();System.Web.UI.HtmlTextWriterhw=newSystem.Web.UI.HtmlTextWriter(tw);this.GridView2.RenderControl(hw);//设置你要导出内容的控件,我这里是RepeaterResponse.Write(tw.ToString());Response.End();

c#.net 中 如何将gridview中的数据导出到excel中 网上的好多办法是将HTML以Excel格式输出,但是导入的时

这涉及到一个标准EXCEL格式的问题。通常基于WEB的开发为了方便 大家都采用直接将表格内容导出为EXCLE格式。而导入的时候我们会发现基于EXCEL的OLEDB数据连接对这种格式的EXCEL格式是不支持的。为了解决以上问题。我开发中有几个办法。1:需要导入的EXCEL采用引用EXCEL类的方法输出。这种方法保证了EXCEL输出的标准格式。但是需要服务器安装EXCEL 且容易造成进程锁死,给系统稳定性造成影响。2:将导出的EXCEL另存为一次。这种方法固然好使,但是是不是能接受每次都另存为是个问题。3:采用第三方控件导出。目前我接触的第三方控件是 Myxls 你可以google下 这个控件使用起来跟调用EXCEL差不多。可以生成标准XLS文件。但是对样式支持不是太好。希望对你有帮助

c#中怎样将gridview中的数据导出excel

两个方法,一个是将datatable转为excel,一个是将DataGridView 导出为excel

在C#中,如何把DataGridview中的数据导出到一个Excel表中

真是巧,我昨天刚刚做了这个public bool ExportDataGridview(DataGridView gridView, bool isShowExcle) { if (gridView.Rows.Count == 0) { return false; } //创建Excel对象 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); excel.Application.Workbooks.Add(true); //生成字段名称 for (int i = 0; i 《 gridView.ColumnCount;i++) { excel.Cells.HeaderText; } //填充数据 for (int i = 0; i 《 gridView.RowCount - 1; i++) //循环行 { for(int j = 0;j 《 gridView.ColumnCount ;j++) //循环列 { if(gridView.ValueType==typeof(string)) { excel.Cells.Value.ToString(); } else { excel.Cells.Value.ToString(); } } } //设置禁止弹出保存和覆盖的询问提示框 excel.Visible = false; excel.DisplayAlerts = false; excel.AlertBeforeOverwriting = false; //保存到临时工作簿 //excel.Application.Workbooks.Add(true).Save(); //保存文件 excel.Save("D:" + "\\234.xls"); excel.Quit(); return true; }

关于gridview控件怎么转换到excel和求gridview导出为excel问题解决的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。