找回密码
 立即注册

QQ登录

只需一步,快速开始

vbagcl

初级会员

24

主题

100

帖子

210

积分

初级会员

积分
210

活字格认证微信认证勋章

vbagcl
初级会员   /  发表于:2011-10-28 14:57  /   查看:12694  /  回复:11
如题,多谢!

11 个回复

正序浏览
vbagcl
初级会员   /  发表于:2011-11-3 21:30:00
12#
多谢指点!
我可能对你的“临时的HtmlForm”理解的不对,我是在工程中添加一个空的web页面充当“临时的HtmlForm”,代码如下:
C1.Web.UI.Controls.C1GridView.C1GridView T1 = (C1.Web.UI.Controls.C1GridView.C1GridView)this.form1.FindControl("T1");
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.Charset = "gb2312";
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            WebForm3 tmpForm = new  WebForm3();
            tmpForm.Controls.Add(T1);
            T1.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
不知道,我错在哪里啦,好像还是报错:
类型“C1GridView”的控件“T1”必须放在具有 runat=server 的窗体标记内。
不过,没关系,我已经用另外的方法解决了导入EXCEL的功能。
  public string OutputExcel(System.Data.DataTable dt_CH,System.Data.DataTable dt, string strTitle, string FilePath)
        {
            DateTime beforeTime = System.DateTime.Now;
            int titleColorindex = 15;
            Excel.Application excel;
            Excel._Workbook xBk;
            Excel._Worksheet xSt;

            int rowIndex = 4;
            int colIndex = 1;

            //excel = new Excel.ApplicationClass();
            excel = new Excel.Application();
            
            xBk = excel.Workbooks.Add(true);
            xSt = (Excel._Worksheet)xBk.ActiveSheet;

            //取得列标题                       
            foreach (DataRow row in dt_CH.Rows)
            {
                colIndex++;
                excel.Cells[4, colIndex] = row["FIELD_NAME_CH"].ToString();// col.Caption;// col.ColumnName;
            }


            //取得表格中的数据                       
            foreach (DataRow row in dt.Rows)
            {
                rowIndex++;
                colIndex = 1;
                foreach (DataRow row2 in dt_CH.Rows)
                {
                    DataColumn col = dt.Columns[row2["ALIAS_NAME"].ToString()];
                    colIndex++;
                    if (col.DataType == System.Type.GetType("System.DateTime"))
                    {
                        excel.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
                        xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
                    }
                    else
                        if (col.DataType == System.Type.GetType("System.String"))
                        {
                            excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString();
                            //xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
                        }
                        else
                        {
                            excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
                        }
                }
            }

            DateTime afterTime = DateTime.Now;


            string filename = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xls";
            excel.ActiveWorkbook.SaveAs(FilePath + filename, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
           

            xBk.Close(null, null, null);
            excel.Workbooks.Close();
            excel.Quit();


            if (xSt != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
                xSt = null;
            }
            if (xBk != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
                xBk = null;
            }
            if (excel != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
                excel = null;
            }
            GC.Collect();//垃圾回收
            Excel.Application excel2 =   new Excel.Application(); //引用Excel对象  
            Excel.Workbook book = excel2.Application.Workbooks.Add("c:\\" + filename);
            excel2.Visible = true; //使Excel可视           }
回复 使用道具 举报
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2011-11-3 09:57:00
11#

回复 10# vbagcl 的帖子

十分抱歉,现在才回复你的问题。出现该问题的原因没有一个准确的解释,包括在MSDN上也没有一个很详细的解释。我寻找了一个替代方法来回避了该问题,添加一个临时的HtmlForm,然后将C1GridView添加到里面进行导出:
            C1.Web.UI.Controls.C1GridView.C1GridView T1 = (C1.Web.UI.Controls.C1GridView.C1GridView)this.form1.FindControl("T1");
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.Charset = "gb2312";
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            HtmlForm tmpForm = new HtmlForm();
            tmpForm.Controls.Add(C1GridView1);

            C1GridView1.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
回复 使用道具 举报
vbagcl
初级会员   /  发表于:2011-10-31 21:13:00
10#
我又写个简单的小例子,这次没有母版页:aspx的代码是
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="C1GRIDVIEW.aspx.cs" Inherits="WebApplication1.QCM.C1GRIDVIEW" %>
<%@ Register assembly="C1.Web.UI.Controls.2" namespace="C1.Web.UI.Controls.C1GridView" tagprefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
     <cc1:C1GridView ID="T1" runat="server" UseEmbeddedVisualStyles="True" VisualStyle="Vista" ></cc1:C1GridView>
        <asp:Button ID="gggg" runat="server" OnClick="btn_click" />
    </div>
    </form>
</body>
</html>
cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1.QCM
{
    public partial class C1GRIDVIEW : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String sqlstr = "select * from BQE_IPRECORD";

            publicclass.GeneralCommon gl = new publicclass.GeneralCommon();
            int rnt = 0;
            int cnt = 0;
            System.Data.DataTable dt = gl.exe_sql_table(sqlstr, ref rnt, ref cnt);
            C1.Web.UI.Controls.C1GridView.C1GridView T1 = (C1.Web.UI.Controls.C1GridView.C1GridView)this.form1.FindControl("T1");
            T1.DataSource = dt;
            T1.DataBind();
        }
        protected void btn_click(object sender, EventArgs e)
        {
            C1.Web.UI.Controls.C1GridView.C1GridView T1 = (C1.Web.UI.Controls.C1GridView.C1GridView)this.form1.FindControl("T1");
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.Charset = "gb2312";
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            T1.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
        }
    }
}
仍然报错:
类型“C1GridView”的控件“T1”必须放在具有 runat=server 的窗体标记内。
回复 使用道具 举报
vbagcl
初级会员   /  发表于:2011-10-31 11:29:00
9#
publicclass.BasePage的代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1.QCM;

namespace WebApplication1.publicclass
{
    public class BasePage : Page
        {
        public virtual void Ref()
        {}

        public virtual void Ins()
        { }

        public virtual void Del()
        { }

        public virtual void Save()
        { }

        public virtual void Clear()
        { }

        public virtual void Prn()
        { }

        public virtual void Mod()
        { }

        public virtual void Last()
        { }

        public virtual void Next()
        { }

        public virtual void Leap()
        { }

        public virtual void Table_Btn_Click(String row_seq)
        { }

        public virtual void Mod_Row(String row_seq)
        { }
        public virtual void Excel()
        { }
        }
}
然后,我利用你的提醒,还会报同样的错误!
回复 使用道具 举报
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2011-10-31 09:24:00
8#
你好,
因为看不到你publicclass.BasePage页面的信息,无法准确定位问题的原因。
从你的页面来看使用了ScriptManager,我曾经在使用UpdatePanel时遇到这样的问题:通过UpdatePanel中Button1.Click事件来访问Reponse会报错,最终设置UpdatePanel的Triggers解决了这个问题,希望这一信息对你有帮助:
  1.         <asp:UpdatePanel ID="UpdatePanel1" runat="server">   
  2.             <Triggers>
  3.             <asp:PostBackTrigger ControlID="Button1" />                     
  4.             </Triggers>
  5.             </Triggers>
  6.             <ContentTemplate>
  7.             </ContentTemplate>
  8.             </asp:UpdatePanel>
复制代码
回复 使用道具 举报
vbagcl
初级会员   /  发表于:2011-10-28 22:04:00
7#

我母版页及内容页

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 使用道具 举报
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2011-10-28 17:19:00
6#
请参考这个帖子:http://gcdn.grapecity.com/showtopic-385.html,并使用IE8浏览论坛

如果点击【添加附件】没有反应,请关闭【回复主题】窗口,然后再进行【高级回复】操作
回复 使用道具 举报
vbagcl
初级会员   /  发表于:2011-10-28 17:09:00
5#
要不你把你的邮箱给,我把附件发到你的邮箱里。
回复 使用道具 举报
vbagcl
初级会员   /  发表于:2011-10-28 17:08:00
地板
好像不能上传附件啊,你是要我把源码贴上吗?很多啊!
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 立即注册
返回顶部