asp.net中实现GridView中的数据导入到Excel中
2008-07-28 10:13
597 查看
看了一些书,也看了很多人的代码 得到了几个核心的东西 于是总结一下:
sp.net中实现GridView中的数据导入到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.IO;
using BLL;
public partial class GridView导入到Excel : System.Web.UI.Page
{
private string fileName = "Information.xls";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["LogStatus"] != "LoggedIn")
Response.Redirect("用户登陆.aspx");
}
GridDataBinds();
}
public void GridDataBinds()
{
DataSet ds = new DataSet();
BLL.UserControl user = new BLL.UserControl();
ds = user.GetAllInfo(Convert.ToInt32(Session["UserLoID"]));
GridView_display.DataSource = ds;
GridView_display.DataBind();
}
public void ExportExcel()
{
Response.Clear();
Response.AddHeader("Content-Disposition","attachment;filename="+fileName+"");
Response.Charset = "";
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-xml";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridDataBinds();
GridView_display.AllowPaging = false;
GridView_display.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
GridView_display.AllowPaging = true;
GridDataBinds();
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
protected void Button_export_Click(object sender, EventArgs e)
{
ExportExcel();
}
}
然后导出可能出现乱码,因为汉字的原因,于是我们需要设置一下
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
这两句是改变属性的
sp.net中实现GridView中的数据导入到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.IO;
using BLL;
public partial class GridView导入到Excel : System.Web.UI.Page
{
private string fileName = "Information.xls";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["LogStatus"] != "LoggedIn")
Response.Redirect("用户登陆.aspx");
}
GridDataBinds();
}
public void GridDataBinds()
{
DataSet ds = new DataSet();
BLL.UserControl user = new BLL.UserControl();
ds = user.GetAllInfo(Convert.ToInt32(Session["UserLoID"]));
GridView_display.DataSource = ds;
GridView_display.DataBind();
}
public void ExportExcel()
{
Response.Clear();
Response.AddHeader("Content-Disposition","attachment;filename="+fileName+"");
Response.Charset = "";
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-xml";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridDataBinds();
GridView_display.AllowPaging = false;
GridView_display.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
GridView_display.AllowPaging = true;
GridDataBinds();
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
protected void Button_export_Click(object sender, EventArgs e)
{
ExportExcel();
}
}
然后导出可能出现乱码,因为汉字的原因,于是我们需要设置一下
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
这两句是改变属性的
相关文章推荐
- asp.net实现 EXCEL数据导入到数据库功能
- [asp.net]把gridview的数据导入excel
- asp.net/c#代码实现excel to mssql数据导入
- asp.net实现 EXCEL数据导入到数据库功能
- asp.net/c#代码实现excel to mssql数据导入
- [导入]Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- ASP.NET下将Excel表格中的数据规则的导入数据库思路分析及实现
- asp.net实现将Excel中多个sheet数据导入到SQLSERVER中的方法
- ASP.NET把gridview中的数据导入到Excel中
- asp.net实现将Excel中多个sheet数据导入到SQLSERVER中的方法
- asp.net 将Excel数据导入到GridView和SQL数据库
- asp.net如何实现excel导入到sql或者根据需要再将sql中的数据导出到excel表格中
- asp.net将页面中gridview中的数据导入excel表中
- asp.net实现excel数据到sql server的导入
- 在ASP.NET中实现Excel数据导入数据库
- asp.net 中将数据库数据导入Excel时, 避免Excel的科学计数法使数据丢失.
- .net实现将Excel中的数据导入数据库
- ASP.NET中GridView数据导出EXCEL小结
- asp.net 将gridview 导入到excel