我是新手  公司让做个C#的 excel报表
网上的例子很多 
可是我打开VS还是不知道如何开始 excel是用类 还是窗体的进行编写呢
求教知情者
最好带 excel报表 简单功能开发 代码 谢谢

解决方案 »

  1.   

    Excel操作类
      

  2.   

    /// <summary>
        /// 数据导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDataExport_Click(object sender, EventArgs e)
        {
            try
            {
                this.Response.Clear();
                this.Response.Buffer = true;
                this.Response.Charset = "GB2312";
                //page.Response.Charset = "UTF-8";            //文件名称
                string fileName = "";
                //返回的数据
                DataSet ds = null;
                //时间
                string time = this.txtTime.Text.Trim() == "" ? DateTime.Now.ToString("yyyy-MM-dd") : this.txtTime.Text.Trim();            if (this.rdoHttp.Checked == true)
                {
                    fileName = "http";
                    ds = DC9000.BLL.DataCenter.GetHttpList(time);
                }
                else if (this.rdoPost.Checked == true)
                {
                    fileName = "post";
                    ds = DC9000.BLL.DataCenter.GetPostList(time);
                }
                
                Response.ClearHeaders();
                this.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName +" "+ time + " .xls");
                //this.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + System.DateTime.Now.ToString("_yyMMdd_hhmm") + ".xls");
                this.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
                this.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
                this.EnableViewState = false;
                Response.Write(ExportTable(ds));
                Response.End();
            }
            catch (Exception)
            {        }
            finally
            {        }
        }    /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static string ExportTable(DataSet ds)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                int count = 0;            foreach (DataTable tb in ds.Tables)
                {
                    sb.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">");
                    sb.AppendLine("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">");
                    //写出列名 
                    sb.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">");
                    foreach (DataColumn column in tb.Columns)
                    {
                        sb.AppendLine("<td>" + column.ColumnName + "</td>");
                    }
                    sb.AppendLine("</tr>");                //写出数据 
                    foreach (DataRow row in tb.Rows)
                    {
                        sb.Append("<tr>");
                        foreach (DataColumn column in tb.Columns)
                        {
                            //if (column.ColumnName.Equals("证件编号") || column.ColumnName.Equals("报名编号"))
                            //    sb.Append("<td style=\"vnd.ms-excel.numberformat:@\">" + row[column].ToString() + "</td>");
                            //else
                           sb.Append("<td>" + row[column].ToString() + "</td>");
                        }
                        sb.AppendLine("</tr>");
                        count++;
                    }
                    sb.AppendLine("</table>");
                }            return sb.ToString();
            }
            catch (Exception)
            {
                return "";
            }
            finally
            {        }
        }
      

  3.   

    这个功能类和窗体都应该使用到,需要准备以下知识或工具:
    1、VSTO excel文档级的程序知识如一楼提到的excel操作类;
    2、如果软件要求在应用程序中嵌入excel文档请使用DSOFramer控件,这是微软发布的(直接在百度里能找到源代码和案例);
    3、如果软件要求用户的操作是在Office应用程序里操作,请使用excel外接程序的方式进行,这样出来的操作嵌在excel应用程序中;
    个人认为准备以上内容应该可以完成你的任务,因为我已经完成了。