将gridview中的数据导入到excle中的方法 怎么样将gridview中的数据输出到excle中,我在网上找了些方法但是都不能实现,哪位大哥大姐指点下,最好有代码,谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 sgvExcel 是GridView DataSet ds = 获取数据的方法 this.sgvExcel.DataSource = ds.Tables[0]; this.sgvExcel.DataBind(); string style = @"<style> .text { mso-number-format:\@; } </script> "; Response.ClearContent(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("活动列表", System.Text.Encoding.UTF8) + ".xls"); //Response.AddHeader("content-disposition", "attachment; filename=House.xls"); Response.ContentType = "application/excel"; System.IO.StringWriter sw = new System.IO.StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); sgvExcel.RenderControl(htw); Response.Write(style); Response.Write(sw.ToString()); Response.End(); sgvExcel.Visible = false; Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("活动列表", System.Text.Encoding.UTF8) + ".xls");这个是给你导出的excle文件命名 你自己定义 http://dotnet.aspx.cc/file/Export-Gridview-To-Excel-With-Multi-Sheet.aspx孟子E章前辈的方法呵呵。挺好用的。 主要是 可以不考虑 服务器没安装office的情况 private void Button4_Click(object sender, System.EventArgs e) { string searchcon=SearchStr_Excel(); SqlConnection con = new SqlConnection(ConnectionString); SqlDataAdapter adapter=new SqlDataAdapter(searchcon ,ConnectionString+ ";Connect Timeout=3000"); DataTable pDataTable=new DataTable(); adapter.Fill(pDataTable); int tRowCount = pDataTable.Rows.Count; int tColumnCount = pDataTable.Columns.Count; Response.Expires = 0; Response.Clear(); Response.Buffer = true; Response.Charset = "utf-8"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("Content-Disposition", "attachment; filename=Excel.xls"); Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>"); // Response.Write("<style type=\"text/css\">.format{mso-number-format:\'\@\';}<\style>"); Response.Write("<Table borderColor=black border=1>"); Response.Write("\n <TR>"); for (int i = 0; i < tColumnCount; i++) { Response.Write("\n <TD bgcolor = #fff8dc>"); Response.Write(pDataTable.Columns[i].ColumnName); Response.Write("\n </TD>"); } Response.Write("\n </TR>"); for (int j = 0; j < tRowCount; j++) { Response.Write("\n <TR>"); for (int k = 0; k < tColumnCount; k++) { // if (k==0)// {// Response.Write("\n<TD style='mso-number-format:\"@\">"); // }// else Response.Write("\n <TD align=\"right\" style='vnd.ms-excel.numberformat:@'>"); Response.Write(pDataTable.Rows[j][k].ToString()); Response.Write("\n </TD>"); } Response.Write("\n </TR>"); } Response.Write("</Table>"); Response.End(); } public static void CreateExcel(DataSet ds, string FileName) { //resp = Page.Response; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls"); HttpContext.Current.Response.ContentType = "application/ms-excel"; string colHeaders = "", ls_item = ""; int i = 0; //定义表对象与行对像,同时用DataSet对其值进行初始化 System.Data.DataTable dt = ds.Tables[0]; DataRow[] myRow = dt.Select(""); //取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符 for (i = 0; i < dt.Columns.Count - 1; i++) colHeaders += dt.Columns[i].Caption.ToString() + "\t"; colHeaders += dt.Columns[i].Caption.ToString() + "\n"; //向HTTP输出流中写入取得的数据信息 HttpContext.Current.Response.Write(colHeaders); //逐行处理数据 foreach (DataRow row in myRow) { //在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n for (i = 0; i < dt.Columns.Count - 1; i++) ls_item += row[i].ToString() + "\t"; ls_item += row[i].ToString() + "\n"; //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据 HttpContext.Current.Response.Write(ls_item); ls_item = ""; } //写缓冲区中的数据到HTTP头文件中 HttpContext.Current.Response.End(); } public void OutPutExcel() { //定义文档类型、字符编码 Response.Clear(); Response.Buffer= true; Response.Charset="GB2312"; //下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开 //filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm Response.AppendHeader("Content-Disposition","attachment;filename=FileFlow.xls"); 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.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.RenderControl(oHtmlTextWriter); //this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件 Response.Write(oStringWriter.ToString()); Response.End(); } del sql的弱智问题 如何获取在gridview或repeater中的控件 怎么用程序去掉浏览器安全性警告? 求一个应用Ado.net Entity Framework的开源项目 ASP.NET网站开发过程是怎么样? treeview使用问题?(树型数据库用treeview显示) mvc ActionFilters验证问题 学了asp.net后 数据库数据如何生成树形结构 放在DataList里的DataGrid无法进入编辑模式! 在线求助 cuteeditor的小问题,200分奉上
sgvExcel 是GridView
DataSet ds = 获取数据的方法
this.sgvExcel.DataSource = ds.Tables[0];
this.sgvExcel.DataBind(); string style = @"<style> .text { mso-number-format:\@; } </script> ";
Response.ClearContent();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("活动列表", System.Text.Encoding.UTF8) + ".xls");
//Response.AddHeader("content-disposition", "attachment; filename=House.xls");
Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw); sgvExcel.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End(); sgvExcel.Visible = false;
孟子E章前辈的方法
呵呵。挺好用的。 主要是 可以不考虑 服务器没安装office的情况
{ string searchcon=SearchStr_Excel();
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter adapter=new SqlDataAdapter(searchcon ,ConnectionString+ ";Connect Timeout=3000");
DataTable pDataTable=new DataTable();
adapter.Fill(pDataTable);
int tRowCount = pDataTable.Rows.Count;
int tColumnCount = pDataTable.Columns.Count;
Response.Expires = 0;
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=Excel.xls");
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
// Response.Write("<style type=\"text/css\">.format{mso-number-format:\'\@\';}<\style>");
Response.Write("<Table borderColor=black border=1>");
Response.Write("\n <TR>");
for (int i = 0; i < tColumnCount; i++)
{
Response.Write("\n <TD bgcolor = #fff8dc>");
Response.Write(pDataTable.Columns[i].ColumnName);
Response.Write("\n </TD>");
}
Response.Write("\n </TR>");
for (int j = 0; j < tRowCount; j++)
{
Response.Write("\n <TR>");
for (int k = 0; k < tColumnCount; k++)
{
// if (k==0)
// {
// Response.Write("\n<TD style='mso-number-format:\"@\">");
// }
// else
Response.Write("\n <TD align=\"right\" style='vnd.ms-excel.numberformat:@'>"); Response.Write(pDataTable.Rows[j][k].ToString()); Response.Write("\n </TD>");
}
Response.Write("\n </TR>");
}
Response.Write("</Table>");
Response.End(); }
{
//resp = Page.Response;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
HttpContext.Current.Response.ContentType = "application/ms-excel";
string colHeaders = "", ls_item = "";
int i = 0; //定义表对象与行对像,同时用DataSet对其值进行初始化
System.Data.DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select("");
//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
for (i = 0; i < dt.Columns.Count - 1; i++)
colHeaders += dt.Columns[i].Caption.ToString() + "\t";
colHeaders += dt.Columns[i].Caption.ToString() + "\n";
//向HTTP输出流中写入取得的数据信息
HttpContext.Current.Response.Write(colHeaders);
//逐行处理数据
foreach (DataRow row in myRow)
{
//在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
for (i = 0; i < dt.Columns.Count - 1; i++)
ls_item += row[i].ToString() + "\t";
ls_item += row[i].ToString() + "\n";
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
HttpContext.Current.Response.Write(ls_item);
ls_item = "";
}
//写缓冲区中的数据到HTTP头文件中
HttpContext.Current.Response.End();
}
public void OutPutExcel()
{
//定义文档类型、字符编码
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm
Response.AppendHeader("Content-Disposition","attachment;filename=FileFlow.xls");
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.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.RenderControl(oHtmlTextWriter);
//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
Response.Write(oStringWriter.ToString());
Response.End();
}