求助:gridview导出Excel出错
程序代码:
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; using System.Data.Odbc; using System.Data.OleDb; using System.using System.Text; using using using System.Threading; namespace cangku { public partial class _default : System.Web.UI.Page { public string con1 = "Provider=IBMDADB2;Database=mes;HOSTNAME=10.80.101.130;PROTOCOL=TCPIP;PORT=50001;uid=optiapu;pwd=optiapu"; protected void Page_Load(object sender, EventArgs e) { Response.Buffer = true; Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1); Response.Expires = 0; Response.CacheControl = "no-cache"; if (!IsPostBack) { string a = "你登录的IP地址为:"; Label1.Text = a + GetClientIP(); switch (DropDownList5.SelectedIndex) { case 0: Label2.Visible = true; Label3.Visible = true; Label4.Visible = true; Label5.Visible = true; Label6.Visible = true; Label7.Visible = true; TextBox1.Visible = true; TextBox2.Visible = true; DropDownList1.Visible = true; DropDownList2.Visible = true; DropDownList3.Visible = true; DropDownList4.Visible = true; Label8.Visible = false; TextBox3.Visible = false; break; case 1: Label8.Visible = true; TextBox3.Visible = true; Label2.Visible = false; Label3.Visible = false; Label4.Visible = false; Label5.Visible = false; Label6.Visible = false; Label7.Visible = false; TextBox1.Visible = false; TextBox2.Visible = false; DropDownList1.Visible = false; DropDownList2.Visible = false; DropDownList3.Visible = false; DropDownList4.Visible = false; break; } } } public void query_data() { //string sip = Page.Request.UserHostName.ToString(); //string ip = Page.Request.UserHostName.ToString(); //TextBox4.Text = sip; //TextBox3.Text = GetClientIP(); string one = DropDownList1.SelectedValue.ToString(); string two = DropDownList2.SelectedValue.ToString(); string three = DropDownList3.SelectedValue.ToString(); string four = DropDownList4.SelectedValue.ToString(); string time1 = TextBox1.Text + "-" + one + "." + two + ".00.000000"; string time2 = TextBox2.Text + "-" + three + "." + four + ".00.000000"; OleDbConnection myconnection = new OleDbConnection(); myconnection.ConnectionString = con1; myconnection.Open(); string sq1="select DISTINCT(F),I,QTY,LIAOHAO,NAM_ITEM,NAM_ITEMS,OUTLOTNO,P_LOT_BRANCHNO,RCV_DTM from "; string sq2=" (SELECT DISTINCT(F),I,QTY,LIAOHAO,NAM_ITEM,NAM_ITEMS,RCV_DTM FROM "; string sq3=" (SELECT DISTINCT(RUNCARD_ID)AS F,(PACKNO)AS I,nam_item,nam_items,rcv_dtm,a.cod_item AS LIAOHAO FROM (SELECT runcard_id,packno,nam_item,nam_items,rcv_dtm,a.cod_item FROM "; string sq4 = " BRPACKCONVERT AS A,BWBOTTOM AS B WHERE A.PACK_NO=B.PACKNO AND eventdatetime >='" + time1 + "'" + "AND eventdatetime <='" + time2 + "' )AS A "; string sq5="LEFT JOIN (select lotno,P_LOT_BRANCHNO from fhlotoperation) B on A.RUNCARD_ID=B.LOTNO )AS D,(SELECT NUM_ORD AS J,pack_no AS O,cod_item,SUM(QTY)AS QTY FROM "; string sq6 = "BRPACKCONVERT WHERE eventdatetime >='" + time1 + "'" + "AND eventdatetime <='" + time2 + "' GROUP BY NUM_ORD,COD_ITEM,PACK_NO)AS E WHERE D.F=E.J AND D.I=E.O ORDER BY I) AS A "; string sq7 = "LEFT JOIN(select lotno,P_LOT_BRANCHNO from fhlotoperation) B on F=B.LOTNO "; string sq8= "LEFT JOIN (SELECT OUTLOTNO,LOTNO FROM BWLOT ) C ON F=C.LOTNO order by f,i,rcv_dtm "; //string sq1 = " select DISTINCT(PACKNO),runcard_id,a.cod_item,NAM_ITEM,NAM_ITEMS,P_LOT_BRANCHNO,OUTLOTNO,RCV_DTM from "; // string sq2 = "(SELECT runcard_id,packno,nam_item,nam_items,rcv_dtm,a.cod_item FROM BRPACKCONVERT AS A,BWBOTTOM AS B WHERE A.PACK_NO=B.PACKNO AND eventdatetime >='" + time1 + "'" + "AND eventdatetime <='" + time2 + "') AS A "; //string sq3 = "LEFT JOIN(select lotno,P_LOT_BRANCHNO from fhlotoperation) B on A.RUNCARD_ID=B.LOTNO "; //string sq4 = "LEFT JOIN (SELECT OUTLOTNO,LOTNO FROM BWLOT ) C ON A.RUNCARD_ID=C.LOTNO ORDER BY PACKNO,RCV_DTM "; sq1 = sq1 + sq2 + sq3 + sq4+sq5+sq6+sq7+sq8; OleDbCommand mycmd = new OleDbCommand(sq1, myconnection); OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.SelectCommand = mycmd; DataSet myds = new DataSet(); adapter.Fill(myds); DataView dv = new DataView(); dv = myds.Tables[0].DefaultView; myconnection.Close(); GridView1.DataSource = dv; GridView1.DataBind(); } public void lotno() { string lot=TextBox3.Text.ToString(); OleDbConnection myconnection = new OleDbConnection(); myconnection.ConnectionString = con1; myconnection.Open(); lot = TextBox3.Text; string sq1 = "select distinct(f),c.i, c.qty,liaohao,nam_item,nam_items,outlotno,p_lot_branchno,rcv_dtm from "; string sq2 = " ( select distinct(num_ord)as f,a.pack_no as i, a.qty,a.cod_item as liaohao,nam_item,nam_items,cos_lot as outlotno ,rcv_dtm from "; string sq3 = " (select sum(qty)as qty ,pack_no,num_ord,cod_item from brpackconvert where num_ord='"+lot+"' group by num_ord,pack_no,cod_item)as a ,bwbottom as b "; string sq4 = " where a.num_ord=b.runcard_id)as c,fhlotoperation as d where f=d.lotno order by rcv_dtm "; sq1 = sq1 + sq2 + sq3 + sq4; OleDbCommand mycmd = new OleDbCommand(sq1, myconnection); OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.SelectCommand = mycmd; DataSet myds = new DataSet(); adapter.Fill(myds); DataView dv = new DataView(); dv = myds.Tables[0].DefaultView; myconnection.Close(); GridView1.DataSource = dv; GridView1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { string aa = DropDownList5.SelectedItem.Value.ToString(); if (aa == "時間") { query_data(); } if (aa == "流單") { lotno(); } } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { } public static void Export(string Filename, GridView gridview, Page page) { page.Response.Clear(); page.Response.Charset = "big5"; page.Response.ContentType = "application/ms-excel"; page.Response.ContentEncoding = Encoding.GetEncoding("big5"); page.Response.AppendHeader("content-disposition","attachment;filename=\""+ HttpUtility.UrlEncode(Filename + DateTime.Now.ToShortDateString(), Encoding.UTF8) +".xls\""); StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); gridview.RenderControl(htw); page.Response.Write(sw.ToString()); page.Response.End(); } /// <summary> /// 此方法必重写,否则会出错 /// </summary> /// <param name="control"></param> /// public override void VerifyRenderingInServerForm(Control control) { } protected void Button2_Click(object sender, EventArgs e) { this.GridView1.AllowPaging = false; this.GridView1.AllowSorting = false; this.GridView1.DataBind(); Export("cangchu", this.GridView1, this.Page); } private string GetClientIP() { string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (null == result || result == String.Empty) { result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } if (null == result || result == String.Empty) { result = HttpContext.Current.Request.UserHostAddress; } return result; } protected void DropDownList5_SelectedIndexChanged1(object sender, EventArgs e) { switch (DropDownList5.SelectedIndex) { case 0: Label2.Visible = true; Label3.Visible = true; Label4.Visible = true; Label5.Visible = true; Label6.Visible = true; Label7.Visible = true; TextBox1.Visible = true; TextBox2.Visible = true; DropDownList1.Visible = true; DropDownList2.Visible = true; DropDownList3.Visible = true; DropDownList4.Visible = true; Label8.Visible = false; TextBox3.Visible = false; break; case 1: Label8.Visible = true; TextBox3.Visible = true; Label2.Visible = false; Label3.Visible = false; Label4.Visible = false; Label5.Visible = false; Label6.Visible = false; Label7.Visible = false; TextBox1.Visible = false; TextBox2.Visible = false; DropDownList1.Visible = false; DropDownList2.Visible = false; DropDownList3.Visible = false; DropDownList4.Visible = false; break; } } } }这是源代码,为什么在导出EXCEL时候,excel里面只显示<div> </div>。
请各位帮忙看看,指教下,急!