我要的结果类似于:第一行是固定表头,
第二列开始填写数据,比如:性别列是可选男或女,工作组列可选A组或B组 
希望求得一个类似的例子,环境是在VS2010和.net4.0

解决方案 »

  1.   

    预先定义EXCEL结构MODEL,把导出的数据存入List<MODEL>   
    foreach(MODEL model in  LstModel)
    {        
                StringBuilder sql = new StringBuilder();
                sql.Append("insert into [sheet1$] values(");
                sql.AppendFormat("'{0}',", MODEL.No.Replace("'","''"));
                sql.AppendFormat("'{0}',", MODEL.Name.Replace("'", "''"));
                sql.AppendFormat("'{0}',", MODEL.UserName.Replace("'", "''"));
                sql.AppendFormat("'{0}',", MODEL.Password.Replace("'", "''"));
                sql.AppendFormat("'{0}',", MODEL.Ip);
                sql.AppendFormat("'{0}',", MODEL.Port);
                sql.AppendFormat("'{0}',", MODEL.ChannelCount);
                sql.AppendFormat("'{0}',", MODEL.ConnectMatrixNo.Replace("'", "''"));
                string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath(文件路径) + ";Extended Properties=Excel 8.0;";
                OleDbConnection connection = new OleDbConnection(connectionString);
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection ;
                command.CommandText = sql;
                command.ExecuteNonQuery();
    }
      

  2.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using Kevin.Lib;
    using Kevin.Model;
    using Kevin.IBLL;
    using Kevin.BLLFactory;
    using System.Collections.Generic;
    using System.Web.UI.HtmlControls;public partial class Admin_Page_OrderManage_OrderOutPrint : System.Web.UI.Page
    {
       
        protected void Button_Click(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "save":
                    switch (listType.SelectedValue)
                    {
                        case "excel":
                            OutPut("attachment;filename=out.xls", "application/ms-excel");
                            break;
                        case "word":
                            OutPut("attachment;filename=out.doc", "application/ms-word");
                            break;
                    }
                    break;
                case "open":
                    switch (listType.SelectedValue)
                    {
                        case "excel":
                            OutPut("online;filename=out.xls", "application/ms-excel");
                            break;
                        case "word":
                            OutPut("online;filename=out.doc", "application/ms-word");
                            break;
                    }
                    break;
            }
        }
        private void OutPut(string fileType, string strType)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            //追加文件表头值
            Response.AppendHeader("Content-Disposition", fileType);
            Response.ContentType = strType;
            this.EnableViewState = false;
            //创建HtmlTextWriter 
            System.IO.StringWriter swOut = new System.IO.StringWriter();
            HtmlTextWriter hTw = new HtmlTextWriter(swOut);
            //将服务器控件的内容输出到所提供的 HtmlTextWriter 对象中;
            myGW.RenderControl(hTw);
            Response.Write(swOut.ToString());
            Response.End();
        }
    }
    导出到word或Exsl可以这样做 不个的定义好Exsl 中的字段与 数据库中字段一样