| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1019 人关注过本帖
标题:版主救急:GridView导出excel出错
只看楼主 加入收藏
gaoshenghua
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2010-10-28
结帖率:85.71%
收藏
已结贴  问题点数:20 回复次数:3 
版主救急:GridView导出excel出错
导出excel后,内容只显示<div></div>
搞了很久没弄出来,请大家帮忙
程序代码:
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 GridView 版主 
2010-11-10 14:12
bakhpl
Rank: 2
等 级:论坛游民
帖 子:5
专家分:20
注 册:2010-10-23
收藏
得分:20 
导出到excle的方法,希望对你有帮助:
public static void DataTable2Excel(DataTable dtData)
    {
        System.Web.UI.WebControls.DataGrid dgExport = null;
        // 当前对话
        System.Web.HttpContext curContext = System.Web.HttpContext.Current;
        // IO用于导出并返回excel文件
         strWriter = null;
        System.Web.UI.HtmlTextWriter htmlWriter = null;

        if (dtData != null)
        {
            // 设置编码和附件格式
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "";


            // 导出excel文件
            strWriter = new ();
            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

            // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
            dgExport = new System.Web.UI.WebControls.DataGrid();
            dgExport.DataSource = dtData.DefaultView;
            dgExport.AllowPaging = false;
            dgExport.DataBind();

            // 返回客户端
            dgExport.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
    }
2010-11-10 18:10
gaoshenghua
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2010-10-28
收藏
得分:0 
问题找到了,将button2_click中的 this.GridView1.AllowPaging = false;
            this.GridView1.AllowSorting = false;
            this.GridView1.DataBind();
这些代码删除就可以!
2010-11-11 08:31
gaoshenghua
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2010-10-28
收藏
得分:0 
樓上的樓上,謝謝你!
對我有幫助,只是對我編寫的代碼沒有幫助,呵呵。
2010-11-11 13:55
快速回复:版主救急:GridView导出excel出错
数据加载中...
 
   



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

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