gridview导出excel2
来源:互联网 发布:sql存储过程写法 编辑:程序博客网 时间:2024/05/29 15:21
http://www.cnblogs.com/stswordman/archive/2006/08/24/485641.html
导出中文乱码,0123,去了0还未处理。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//为导出excel增加:
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
private void BindData()
{
SqlConnection myConnection = new SqlConnection("Server=.\\sqlexpress;Database=kyj2008;Trusted_Connection=true");
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM zzc", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
//修改为GridView1
//GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Btn_ExportClick(object sender, EventArgs e)
{
string style = @"<style> .text { mso-number-format:\@; } </script> ";
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
//修改为GridView1
GridView1.RenderControl(htw);
// Style is added dynamically
Response.Write(style);
Response.Write(sw.ToString());
//HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=GB2312>");
//string fileName = HttpUtility.UrlEncode(ExcelFileName + ".xls", Encoding.GetEncoding("GB2312"));
//HttpContext.Current.Response.AddHeader("content-disposition",
//"attachment;filename=" + fileName);
Response.End();
}
protected void Btn_ExportExcelPaging(object sender, EventArgs e)
{
//修改为GridView1
DisableControls(GridView1);
Response.ClearContent();
Response.Charset = "GB2312";
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
//修改为GridView1
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
private void DisableControls(Control gv)
{
LinkButton lb = new LinkButton();
Literal l = new Literal();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{
if (gv.Controls[i].GetType() == typeof(LinkButton))
{
l.Text = (gv.Controls[i] as LinkButton).Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(DropDownList))
{
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
if (gv.Controls[i].HasControls())
{
DisableControls(gv.Controls[i]);
}
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
//修改为GridView1
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Attributes.Add("class", "text");
}
}
}
- gridview导出excel2
- java操作poi导出Excel2
- 导出Excel2 - 项目分解篇
- 读取excel2
- Elasticsearch获取ES查询的所有结果,并批量导出Excel2
- gridView 导出
- GridView导出Excel研究
- 用GridView导出Execl
- GridView导出Excel研究
- GridView导出Excel研究
- GridView导出Execl
- GridView导出Excel
- GridView格式化导出Excel
- GridView导出Excel研究
- GridView导出到Excel
- GridView导出Excel研究
- GridView导出Excel研究
- GridView导出Excel研究
- 《Windows高级调试》分析(一)
- 高频天线识别盲区的处理方法之一
- 用java代码实现查询每个字母的次数
- 随笔:我的未来是不是梦
- 深入理解C语言
- gridview导出excel2
- C语言小函数
- http协议简洁
- 使用GDI绘制简单曲线
- USB的八个问题和答案
- 解决request中文乱码的问题
- 私有云vs公有云vs混合云
- OSG的优点
- Delphi从应用程序资源中直接执行dll(不用先把dll存到磁盘上)