gridview数据如何导入到Excell
发布网友
发布时间:2022-04-27 12:16
我来回答
共3个回答
懂视网
时间:2022-04-27 16:37
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data .SqlClient ;
using System.Data ;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String sqlconn = "Server=.; DataBase=db; Integrated Security=SSPI ";
string sql = "select top 10 * from table";
SqlConnection myConnection = new SqlConnection(sqlconn);// 创建数据库连接实例
myConnection.Open(); //打开数据库
SqlCommand myCommand = new SqlCommand(sql, myConnection);//创建sql的实例,执行一个sql
SqlDataAdapter Adapter = new SqlDataAdapter();//创建一个sql数据适配器
Adapter.SelectCommand = myCommand;//属性设置为 从数据源中检索记录
DataSet myDs = new DataSet(); //创建数据集实例
Adapter.Fill(myDs);//填充数据集
GridView1.DataSource = myDs.Tables[0].DefaultView;//
GridView1.DataBind();
// DataToExcel("测试的cxcel", GridView1);
myConnection.Close();//关闭数据库连接
}
public void DataToExcel(string fileName, GridView myGridView)
{
//定义文档类型、字符编码
Response.Clear();
Response.Buffer = false;
//Response.Charset = "utf-8";
Response.Charset = "GB2312";
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
//filename=FileFlow.xls 指定
输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc || .xls || .txt ||.htm
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || application/ms-html || 或其他浏览器可直接支持文档
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
//System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
//定义一个输入流
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
//将目标数据绑定到输入流输出
myGridView.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
//下面这个空语句一定要加,否则会出现“必须放在具有 runat=server 的窗体标记内。”的错误
public override void VerifyRenderingInServerForm(Control control)
{
}
//点击事件,生成excel
protected void Button1_Click(object sender, EventArgs e)
{
DataToExcel("测试的cxcel", GridView1);
}
}
热心网友
时间:2022-04-27 13:45
//GridView重写
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
//导入Excel方法
private void ExportDataGrid(string FileType, string FileName)
{
Response.Charset = "UTF8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8));
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw); //GridView1是GridView控件名,也可以是其他数据绑定控件
Response.Write(tw.ToString());
Response.End();
}
//导入Excel按钮事件
protected void Button3_Click(object sender, EventArgs e)
{
ExportDataGrid("application/ms-excel", "库存表.xlc");
}
绝对能用
热心网友
时间:2022-04-27 15:03
// 导出按钮
protected void btnExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = false;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=filename.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
cGridView.RenderControl(oHtmlTextWriter);//cGridView 是Gridview的id
Response.Write(oStringWriter.ToString());
Response.End();
}
//这个方法必须写
public override void VerifyRenderingInServerForm(Control control)
{
}