寻找一个比较完善简单的导出Excel类,格式要是xls,最好是cs源代码。web开发用。不要那种导出成csv、txt的。欢迎大家参观讨论,网上搜了一下,方法有很多种,但都不是很完善。

解决方案 »

  1.   


    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Windows.Forms;
    using Microsoft.Office.Interop.Excel;
    using Microsoft.Office.Interop.Word;
    using Microsoft.Office.Core;
    using OWC=Microsoft.Office.Interop;
    using System.Reflection;
    using System.Text;
    namespace CaOnLine.ZWDB.DFObject
    {
     /// <summary>
     /// ExportFiles 的摘要说明。
     /// 作用:把DataSet数据集内数据转化为Excel、Word文件
     /// 描述:这些关于Excel、Word的导出方法,基本可以实现日常须要,其中有些方法可以把数据导出后
     ///       生成Xml格式,再导入数据库!有些屏蔽内容没有去掉,保留下来方便学习参考用之。   
     /// 备注:请引用Office相应COM组件,导出Excel对象的一个方法要调用其中的一些方法和属性。
     /// </summary>
      
      /// <summary>
      /// 导出Excel文件类
      /// </summary>
      /// <param name="ds"></param>
      /// <param name="FileName"></param>
      ///
      #region  //导出Excel文件类  public void DataSetToExcel(DataSet ds,string FileName)
      {
       try
       {
        //Web页面定义
        //System.Web.UI.Page mypage=new System.Web.UI.Page();
        HttpResponse resp;
        resp=HttpContext.Current.Response;
        resp.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
        resp.AppendHeader("Content-disposition","attachment;filename="+FileName+".xls");
        resp.ContentType="application/ms-excel";
        //变量定义
        string colHeaders=null;
        string Is_item=null;    //显示格式定义////////////////
        //文件流操作定义
        //  FileStream fs=new FileStream(FileName,FileMode.Create,FileAccess.Write);
        //StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.GetEncoding("GB2312"));    StringWriter sfw=new StringWriter();
        //定义表对象与行对象,同时用DataSet对其值进行初始化
        System.Data.DataTable dt=ds.Tables[0];
        DataRow[] myRow=dt.Select();
        int i=0;
        int cl=dt.Columns.Count;    //取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
        for(i=0;i<cl;i++)
        {
         //if(i==(cl-1))  //最后一列,加\n
         // colHeaders+=dt.Columns[i].Caption.ToString();
         //else
         colHeaders+=dt.Columns[i].Caption.ToString()+"\t";
        }
        sfw.WriteLine(colHeaders);
        //sw.WriteLine(colHeaders);    //逐行处理数据
        foreach(DataRow row in myRow)
        {
         //当前数据写入
         for(i=0;i<cl;i++)
         {
          //if(i==(cl-1))
          //   Is_item+=row[i].ToString()+"\n";
          //else
          Is_item+=row[i].ToString()+"\t";
         }
         sfw.WriteLine(Is_item);
         //sw.WriteLine(Is_item);
         Is_item=null;
        }
        resp.Write(sfw);
        //resp.Clear();
        resp.End();
       }
       catch(Exception e)
       {
                   throw e;
       }
      }
      #endregion
      

  2.   

    http://www.cnblogs.com/downmoon/archive/2007/12/29/1019303.html
      

  3.   

    楼主想生成2进制格式的XLS文件
      

  4.   

    NickLee.Common.ExcelLite参考
    http://www.cnblogs.com/mail-ricklee/archive/2008/07/29/1255873.html
    中的
    NickLee.Web.UI.dll
      

  5.   

    这个精华帖,讨论的很全面了
    http://topic.csdn.net/u/20071015/12/a9c49cb4-a83e-44ae-bdca-5b942b920003.html
      

  6.   

    试试这个:http://www.wfsoft.com/middleware_info.asp?id=10080连Excel都不用装,就可以导出xls
      

  7.   

    前段时间刚好做过一个,是网上找的例子,但是有的会有乱码,如果出现乱码,用utf-7试试
      

  8.   

    TO:xocom
    你的代码导出的文件看着后缀是xls,实际是txt格式的。不符合要求。
      

  9.   

    NickLee.Common.ExcelLite 太庞大了。
      

  10.   

    TO:macallkirk 
    这个帖子确实提供了很多方案,不对对于生成标准的xls格式,似乎也没有提供一种在web中比较行之有效的方法。
      

  11.   

    最后楼主不是推荐了一个老外写的组件吗,ExcelXmlWriter
    我用了一下,还是很好很强大的。