我需要做一个简单的在服务端生成EXCEL的程序,不是VS建立的工程,只有一个ASPX文件。引用了一个C#类文件,类里using Excel = Microsoft.Office.Interop.Excel;不能用,(因为没有添加引用),这个用什么方法可以解决,有像工程里添加引用的类似代码方法,谢谢。新手,不太了解VS工程的结构。

解决方案 »

  1.   

    你这不是一个项目,你应该在项目的DLL中添加引用。
    你还是得有一个项目,
      

  2.   

    如果没有特殊要求,你可以直接用Response.Write来导出
    参考:
        /// <summary>
        /// 将DataTable数据源导出为Excel文件
        /// </summary>
        /// <param name="dt">DataTable数据源</param>
        /// <param name="fname">Excel文件名</param>
        public static void ToExcel(DataTable dt, string fname)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "gb2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;        HttpContext.Current.Response.ContentType = "application/vnd.ms-xls";
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);        DataGrid dg = new DataGrid();
            //dg.Style.Add(HtmlTextWriterStyle.BorderWidth, "1px");
            dg.DataSource = dt;
            dg.DataBind();
            dg.RenderControl(htmlWrite);
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(fname));
            HttpContext.Current.Response.Write(stringWrite.ToString());
            HttpContext.Current.Response.End();        
        }
      

  3.   

    引用了一个C#类文件,类里using Excel = Microsoft.Office.Interop.Excel;不能用,(因为没有添加引用),这个用什么方法可以解决,有像工程里添加引用的类似代码方法,谢谢。基本上在引用里添加excel.exe就可以了,所以前提是你安装了office哦