标题:求助:gridview导出Excel出错
只看楼主
gaoshenghua
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2010-10-28
结帖率:85.71%
已结贴  问题点数:20 回复次数:5 
求助: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>。
请各位帮忙看看,指教下,急!
搜索更多相关主题的帖子: Excel gridview 
2010-11-10 13:34
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
得分:20 
代码太长了,建议先把数据显示在datagrid里面,显示正确的话再导出比较好!!
2010-11-10 16:42
gaoshenghua
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2010-10-28
得分:0 
回复 2楼 c1_wangyf
可以正常显示,只是在导出来的时候出错!问题已经找到了。
将button2_click里面的this.page全部去掉!这样就可以正常导出了!
2010-11-11 08:29
gaoshenghua
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2010-10-28
得分:0 
打错了。应该是这句 this.GridView1.AllowPaging = false;
            this.GridView1.AllowSorting = false;
            this.GridView1.DataBind();
删除掉就可以了!
2010-11-11 08:32
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
得分:0 
恭喜你了!!
2010-11-11 10:18
gaoshenghua
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2010-10-28
得分:0 
解決了就好!
不過還是謝謝版主。
2010-11-11 13:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-325465-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031226 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved