日期:2014-05-18  浏览次数:20471 次

求助,DATASET導入EXCEL
大家好,我最近寫了一個方法,是DATASET導入EXCEL的,可在調用時不知道怎麼偉參數,


  protected   void   Button2_Click(object   sender,   EventArgs   e)
        {
                DataSet   ds   =   new   DataSet();
                ds=bind1();
                string   path   =   " ";
                //path   =   ;
                ExportToExcel(ds,   @ "~\EmpDimissionResult.xls ");
        },不知道怎麼寫

------解决方案--------------------
如果ExportToExcel的第二个路径是用户的下载路径的话. this.Page.ResolveClientUrl( "~/EmpDimissionResult.xls ")
------解决方案--------------------
Response.Clear();

Response.Buffer = true;

Response.Charset = "GB2312 ";

Response.ContentEncoding = System.Text.Encoding.GetEncoding( "GB2312 ");

Response.AddHeader( "Content-Disposition ", "attachement;filename=noname.xls ");

Response.ContentType = "application/vnd.ms-excel ";

this.EnableViewState = false;

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

this.show_person.RenderControl(hw);--------show_person为控件名(如gridview)
Response.Write(sw.ToString());

Response.End();

------解决方案--------------------
给个例子

SqlConnection conn=DB.createCon();;
SqlDataAdapter da=new SqlDataAdapter( "select * from product ",conn);
DataSet ds=new DataSet();
da.Fill(ds, "table1 ");
DataTable dt=ds.Tables[ "table1 "];
StringWriter sw=new StringWriter();
sw.WriteLine( "自动编号\t姓名\tSid ");
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine(dr[ "pID "]+ "\t "+dr[ "pName "]+ "\t "+dr[ "pSexID "]);
}

sw.Close();
Response.AddHeader( "Content-Disposition ", "attachment; filename=test.xls ");
Response.ContentType = "application/ms-excel ";
Response.ContentEncoding=System.Text.Encoding.GetEncoding( "GB2312 ");
Response.Write(sw);
Response.End();

------解决方案--------------------
Response.ContentType= "application/vnd.ms-excel ";//指定内容类型
Response.Charset= "UTF-8 "; //字符集是空
this.EnableViewState=false; //指定页请求结束时仍然保持现在状态
// Response.AddHeader( "Content-Disposition ", "attachment; filename=1.xls ");
System.IO.StringWriter sw=new System.IO.StringWriter();//
System.Web.UI.HtmlTextWriter hw=new HtmlTextWriter(sw);//
hw.WriteLine( " <meta http-equiv=\ "Content-Type\ " content=\ "text/html; charset=utf-8\ "> ");
dlList.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();