要从SqlServer中导出数据表到Excel,或是从SqlServer中导出数据到Excel,求实例。。开发的程序是Winform哦

解决方案 »

  1.   

    从数据库导出  无非是一个table  一个table    有sql语句能找到所有的table
    然后把每个table 生成excel 教程   datable to excel    http://blog.eastmoney.com/fk/blog_120139321.htmldatable 到excel还有很多方法  比如说先复制table到内存  然后粘贴到excel 这样很快 
      

  2.   

    NPOI
    http://www.cnblogs.com/tonyqus/archive/2009/03/16/1409966.html
      

  3.   

    http://www.google.com.hk/search?hl=zh-CN&newwindow=1&safe=strict&biw=771&bih=445&q=datagridview%E5%AF%BC%E5%87%BA%E5%88%B0excel&btnG=Google+%E6%90%9C%E7%B4%A2&aq=f&aqi=&aql=&oq=借助 DataGridView。
      

  4.   

    实现方法: 
       SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]); 
       SqlDataAdapter da=new SqlDataAdapter("select * from tb1",conn); 
       DataSet ds=new DataSet(); 
       da.Fill(ds,"table1"); 
       DataTable dt=ds.Tables["table1"]; 
       string name=System.Configuration.ConfigurationSettings.AppSettings["downloadurl"].ToString()+DateTime.Today.ToString("yyyyMMdd")+new Random(DateTime.Now.Millisecond).Next(10000).ToString()+".csv";//存放到web.config中downloadurl指定的路径,文件格式为当前日期+4位随机数 
       FileStream fs=new FileStream(name,FileMode.Create,FileAccess.Write); 
       StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.GetEncoding("gb2312")); 
       sw.WriteLine("自动编号,姓名,年龄"); 
       foreach(DataRow dr in dt.Rows) 
       { 
        sw.WriteLine(dr["ID"]+","+dr["vName"]+","+dr["iAge"]); 
       } 
       sw.Close(); 
       Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name)); 
       Response.ContentType = "application/ms-excel";// 指定返回的是一个不能被客户端读取的流,必须被下载 
       Response.WriteFile(name); // 把文件流发送到客户端 
       Response.End(); 方法二:导出到csv文件,不存放到服务器,直接给浏览器输出文件流 优点: 
    1、随时生成,不需要占用资源 
    2、可以结合身份认证 
    3、同样利于数据交换 实现方法: 
    SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]); 
       SqlDataAdapter da=new SqlDataAdapter("select * from tb1",conn); 
       DataSet ds=new DataSet(); 
       da.Fill(ds,"table1"); 
       DataTable dt=ds.Tables["table1"]; 
       StringWriter sw=new StringWriter(); 
       sw.WriteLine("自动编号,姓名,年龄"); 
       foreach(DataRow dr in dt.Rows) 
       { 
        sw.WriteLine(dr["ID"]+","+dr["vName"]+","+dr["iAge"]); 
       } 
       sw.Close(); 
       Response.AddHeader("Content-Disposition", "attachment; filename=test.csv"); 
       Response.ContentType = "application/ms-excel"; 
       Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); 
       Response.Write(sw); 
       Response.End(); 对方法一,二补充一点,如果你希望导出的是xls文件分隔符用\t就可以了,不要用逗号 代码修改如下: 
    sw.WriteLine("自动编号\t姓名\t年龄"); 
       foreach(DataRow dr in dt.Rows) 
       { 
        sw.WriteLine(dr["ID"]+"\t"+dr["vName"]+"\t"+dr["iAge"]); 
       } 
    另外,修改输出的文件扩展名为xls即可。 方法三:从datagrid导出html代码,生成excel文件,给客户端下载 优点: 
    1、有固定的格式,样子好看(datagrid的样子) 局限性: 
    1、不适合数据交换,里面有html代码,比较乱,没有固定格式 
    2、datagrid不能有分页、排序等,否则出错 实现方法: 
    Response.Clear(); 
       Response.Buffer= false; 
       Response.Charset="GB2312"; 
       Response.AppendHeader("Content-Disposition","attachment;filename=test.xls"); 
       Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");    Response.ContentType = "application/ms-excel";    this.EnableViewState = false; 
       System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); 
       System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); 
       this.DataGrid1.RenderControl(oHtmlTextWriter); 
       Response.Write(oStringWriter.ToString()); 
       Response.End(); 在这里说明一点:有的网友反映代码出现“没有dr["id"]”之类的错误,这个代码是按照我的数据结构来写的,到时候相关的字段要换成你自己的才是。 还有就是如果文件名需要中文的话,这么修改Response.AddHeader("Content-Disposition", "attachment; filename="+System.Web.HttpUtility.UrlEncode("中文",System.Text.Encoding.UTF8)+".xls"); 
      

  5.   

    最简单的,把table 里面的行列带表头写按格式写入流,保存的时候保存成 .xls,打开就是excel文件id name sex
    1 aaa 22
    2 bbb 33就这种格式就行。excel识别能力老强了。
      

  6.   

    直接用DataSet读出来,然后直接写入Excel,写入Excel DataSet有默认方法