问题:导出到Excel的时候,要引用哪几个命名空间,还需要下载别的xxx.dll文件引用进去么?最好说详细一点,谢谢。引用自带的和自己下载引用的,这方面的原理我一点都不懂,都不知道怎么问大家,知道的稍微指点下,不知道也没关系,谢谢大家。

解决方案 »

  1.   

    命名空间是:using System.Drawing;using System.IO;using System.Text;   //导出方法
        private void Export(string FileType, string FileName)
        {
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            GridView1.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
        }
    调用方法: Export("application/ms-excel", "单据" + DateTime.Now.ToShortDateString() + ".xls");
      

  2.   

    gridview导出excel很简单
    打开excel模板,引用COM:Microsoft Excel 11.0 Object Library
      

  3.   

    这几天刚做了个.NET导出EXCEL的:http://blog.csdn.net/gdjlc/archive/2010/08/13/5810311.aspx
      

  4.   

    如果是gridview导出excel,下面程序即可:
    protected void btExcel_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");        Response.Charset = "gb2312";
            Response.ContentEncoding = System.Text.Encoding.UTF7;  //没有这一行,在Excel中的中文以乱码显示
            Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。         System.IO.StringWriter stringWrite = new System.IO.StringWriter();        System.Web.UI.HtmlTextWriter htmlWrite =
            new HtmlTextWriter(stringWrite);
            gvYearView.AllowPaging = false;
            BindgvYearView(0);
            gvYearView.RenderControl(htmlWrite);        Response.Write(stringWrite.ToString());        Response.End();        gvYearView.AllowPaging = true;
            BindgvYearView(0);
        }
        public override void VerifyRenderingInServerForm(Control control) //虽然该函数没有函数体,但只有重载此函数程序才能正常运行
        {    }
      

  5.   

    嗯,谢谢大家,我主要想知道的是引用COM里的哪几个文件,还需不需要引用自己下载的DLL。麻烦在告诉下,呵呵。
      

  6.   

    如果你要用Excel COM组件的话,需要添加对Excel com的引用。
    Add Reference...
    COM 页.
    找到Microsoft Excel 11.0 Object Library
    引用后就可以使用
    Microsoft.Office.Interop.Excel.ApplicationClass excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); 
      

  7.   

    你想知道的是这个把 
    Microsoft.Office.Interop.Excel.dll
      

  8.   


    不需要下载DLL,但是需要机器上安装了Excel.
      

  9.   

    1装excel
    2,添加引用,选择com