DataGrid导出EXCEL的几个方法(WebControl)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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

{

///

/// myExcel µÄժҪ˵Ã÷¡£

///

public class myExcel

{

public myExcel()

{

}

///

/// ½«DATAGRIDµ¼³öΪEXCELÎļþ·½·¨Ò»,

/// ²ÎÊýÊÇ:Òªµ¼³öµÄDATAGRIDµÄIDºÍÒª±£´æÏÂÀ´µÄEXCELÎļþÃû

///

/// page

/// datagrid

/// filename

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();

}

///

/// ½«DATAGRIDµ¼³öΪEXCELÎļþ·½·¨¶þ,

/// ²ÎÊýÊÇ:Òªµ¼³öµÄDATAGRIDµÄIDºÍÒª±£´æÏÂÀ´µÄEXCELÎļþÃû

///

/// page

/// datagrid

/// filename

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="

";

string Fieldstring="";