asp.net将数据导出为excel有什么好的方法?
我不想要那种包含table等html标签的数据导出,希望导出比较干净的纯数据到excel中去。
有代码或方法吗?大家都说说,100分相送。

解决方案 »

  1.   

    http://support.microsoft.com/kb/302084/ZH-CN/
    http://www.cnblogs.com/king_astar/archive/2006/02/17/332785.aspx
    http://www.cnblogs.com/emanlee/archive/2007/05/31/766520.aspx
    http://hi.baidu.com/zidone/blog/item/29fbad514f96bd8f8c54302e.html网上很多例子
      

  2.   

    1.建立一个excel文件。
    2.把这个excel文件当成一个数据库连接他
    3.用sql语句生成表
    4.逐条插入数据,关闭连接。
    具体查baidu
      

  3.   

    可以使用xml格式,也可以直接操作Excel COM组件对象
      

  4.   

    网上的我都找了哦,大部分都是用table等html格式直接存储为excel
      

  5.   

    http://download.csdn.net/source/376667
    这有个例子,看一下~
      

  6.   

    LZ  你的意思是不是  从库里把数据导入到excel中,样子是不是这样的   
     上面是那个字段,下面是数据??/我们做过这样的,一般情况下是做报表才用到的
      

  7.   

    我是要把一组经过处理的数据放到excel中,这个excel需要用在别的系统中做数据导入,所以需要干净的数据。
      

  8.   


    请问用这种方式,服务器如果没装excel可以吗?
      

  9.   

    大家有做过用text做中介的方式吗?感觉这种会比较干净,因为我不要求什么样式公式,只要数据。这种方式是什么原理?
      

  10.   

    用text做中介??没用过,我们用的就是用一个SQL语句  插入进去`````
      

  11.   


    我们不可能用这种,服务器没有excel,而且我自己调试的机器上也没有,我们没有这个软件的授权,只能用openoff
      

  12.   

    1.建立一个excel文件。 
    2.把这个excel文件当成一个数据库连接他 
    3.用sql语句生成表 
    4.逐条插入数据,关闭连接。 
    我的就是这样做的
      

  13.   


    不明白你的意思1,使用微软的组件 导出 EXCEL 详情参见;万能,高效-C#导出数据到Excel
    http://hi.baidu.com/tshxsky/blog/item/a6792bb4be078a768bd4b291.html2,如下FileInfo fileInfo = new FileInfo(Server.MapPath(path));
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name));
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding", "binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileInfo.FullName);
        Response.Flush();
        Response.End();3,直接从 SQL 导出到 EXCELhttp://www.cnblogs.com/Csharp-net/archive/2008/01/12/1036226.html4, 模板的方法,新建一个 EXCEL ,另存为HTML 页面,然后加上 <%%> 之后,在后台填充 值。
      

  14.   


    这些方法都有很多缺限,需要服务器安装excel
      

  15.   

    1、先将网页private string GetweatherXML() 
              { 
                   string body = ""; 
                   System.Net.WebRequest request = System.Net.WebRequest.Create(地址);//创建对weatherURL请求 
                   request.Credentials = System.Net.CredentialCache.DefaultCredentials;//安全设置 
                   System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();//得到响应 
                   if (response.StatusDescription.ToUpper() == "OK") 
                   { 
                        System.IO.Stream weatherStream = response.GetResponseStream(); 
                        System.IO.StreamReader read = new System.IO.StreamReader(weatherStream); 
                        body = read.ReadToEnd(); 
                        read.Close(); 
                        weatherStream.Close(); 
                   } 
                   else 
                   { 
                        body = string.Empty; 
                   } 
                   response.Close(); 
                   return body; 
              } 2、循环读取内容存入数组 private void AssayXML()  
              { 
                   XmlReader ObjXml = XmlReader.Create(new System.IO.StringReader(weatherXML));
                   ObjXml.MoveToContent();
                   while (ObjXml.Read()) 
                   { 
    Read//参考网上的方法
                   } 
              } 3、导出到EXCEL private void FileCrear(string[] swtreaStr)//导出Excel
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.Title = "导出Excel文件到";
                saveFileDialog.ShowDialog();
                if (saveFileDialog.FileName != "")
                {
                    try
                    {
                        File.WriteAllLines(saveFileDialog.FileName, swtreaStr, System.Text.Encoding.GetEncoding("gb2312"));
                        Process.Start(saveFileDialog.FileName);
                    }
                    catch
                    {
                        MessageBox.Show("导出失败!");
                        return;
                    }
                    finally
                    {
                        saveFileDialog.Dispose();
                    }
                }
            }
      

  16.   

    我已经找到一个比较好的,简单的方法,大家看怎么样?就是先将我的内容转换为string串,这个字符串中包/t/r等表示行列关系,输出这个字符串到excel。
      

  17.   

    但是据说只要在服务器上注册一下EXCEL的DLL就行了,不过一直没有试过!
      

  18.   

    呵呵,我提供的方法就是这个。
    先将内容转换成字符串,然后分解读取字符串存入一个一维字符数组,“/t/r”表示下个单元格。
    然后用File写入EXCLE;
      

  19.   

    long totalCount = dt.Rows.Count;   //dt的行数,一会循环要用
    //FileName为路径(注意名字要和表的名字一样,比如在次方法中应为excel,其实这3个参数应该全用变量代替的,呵呵,自己写吧,我不改了)
            string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName+ ";Extended  roperties=Excel 8.0;";
            OleDbConnection objConn = new OleDbConnection(connString);  
            OleDbCommand objCmd = new OleDbCommand();
            objCmd.Connection = objConn;
            objConn.Open();
            //创建表的结构
            objCmd.CommandText = "CREATE TABLE excel(ID int ,字段 varchar)";
            objCmd.ExecuteNonQuery();
            //插入表
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                objCmd.CommandText = "INSERT INTO excel(ID,字段) values"
                    + "('" + dt.Rows[i][0].ToString() + "','" + dt.Rows[i][1].ToString() + "')";
                objCmd.ExecuteNonQuery();
            }
            objConn.Close();你要的答案! 
      

  20.   


    这种的需要在服务器上安装excel,而我们服务器不可能装excel
    再就是这样做,数据量很大的话,几十万条的数据可能会有问题。
      

  21.   

    关注,如果不装EXCEL或不注册EXCEL.dll如何解决??
      

  22.   

    4. dataset导出到excel
    4.1 写数据
       object missing = System.Reflection.Missing.Value;
                 Microsoft.Office.Interop.Excel.ApplicationClass appc = new Microsoft.Office.Interop.Excel.ApplicationClass();//lauch excel
                 Microsoft.Office.Interop.Excel.Workbook wb = appc.Application.Workbooks.Add(true);//add new workbook            Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);//get first worksheet
                 Microsoft.Office.Interop.Excel.Range range = ws.get_Range("A1", "C1");//get range from A1 to C1
      string[] arrValue={"1","2","3"};
                 range.Value2 = arrValue;//set value
       .
    或者 
     public bool SaveFP2toExcel(string Path)
        {
        try
         {
         string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";
         OleDbConnection conn = new OleDbConnection(strConn);
         conn.Open();  
         System.Data.OleDb.OleDbCommand cmd=new OleDbCommand ();
         cmd.Connection =conn;
         //cmd.CommandText ="UPDATE [sheet1$] SET 姓名='2005-01-01' WHERE 工号='日期'";
         //cmd.ExecuteNonQuery ();
         for(int i=0;i<fp2.Sheets [0].RowCount -1;i++)
          {
          if(fp2.Sheets [0].Cells[i,0].Text!="")
           {
           cmd.CommandText ="INSERT INTO [sheet1$] (工号,姓名,部门,职务,日期,时间) VALUES('"+fp2.Sheets [0].Cells[i,0].Text+ "','"+
            fp2.Sheets [0].Cells[i,1].Text+"','"+fp2.Sheets [0].Cells[i,2].Text+"','"+fp2.Sheets [0].Cells[i,3].Text+
            "','"+fp2.Sheets [0].Cells[i,4].Text+"','"+fp2.Sheets [0].Cells[i,5].Text+"')";
           cmd.ExecuteNonQuery ();
          }
         }
         conn.Close ();
         return true;
        }
        catch(System.Data.OleDb.OleDbException ex)
         {
         System.Diagnostics.Debug.WriteLine ("写入Excel发生错误:"+ex.Message );
        }
        return false;
       }
     
    4.2 格式化
    设置字体颜色 
     range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);//white color
    设置背景色 
     range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.MidnightBlue);//set backgroud color
      

  23.   

    C#操作excel 
    1. 引用的com组件:Microsoft.Office.Interop.Excel.dll,一般安装完office就有这个文件。(office 2003下载这个dll:点击下载)。 
    2. 新建一个C#项目,添加前面的dll到引用里面来 
    3. 读取excel数据到dataset
    方法一 
     private void OpenExcel(string strFileName)
              {
                 object missing = System.Reflection.Missing.Value;
                 Microsoft.Office.Interop.Excel.ApplicationClass appc = new Microsoft.Office.Interop.Excel.ApplicationClass();//lauch excel application
                 if (appc == null)
                  {
                     MessageBox.Show("can't access excel");
                 }
                 else
                  {
                     // Microsoft.Office.Interop.Excel.Workbook wb = appc.Application.Workbooks.Add(true);
                     //open excel file
                     Microsoft.Office.Interop.Excel.Workbook wb = appc.Application.Workbooks.Open(strFileName, missing, false, missing, missing, missing, missing, missing, missing, true, missing, missing, missing, missing, missing);
                     //get first worksheet
                     Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
                     //get range from A1 to A3                Microsoft.Office.Interop.Excel.Range rng = ws.Cells.get_Range("A1", "A3");                
                     object[,] arrValue = (object[,])rng.Value2;//get range's value
                     string strValue = "";
                     for (int i = 1; i <= arrValue.GetLength(0); i++)
                      {
                         strValue +="Cell"+i+":"+ arrValue[i, 1].ToString()+System.Environment.NewLine;
                     }
                     MessageBox.Show(strValue.ToString());//show value
                 }
                 appc.Quit();
                 appc = null;
                 
             }方法二 
      public DataSet ExcelToDS(string Path)
        {
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";
        OleDbConnection conn = new OleDbConnection(strConn);
        conn.Open();  
        string strExcel = "";   
        OleDbDataAdapter myCommand = null;
        DataSet ds = null;
        strExcel="select * from [sheet1$]";
        myCommand = new OleDbDataAdapter(strExcel, strConn);
        ds = new DataSet();
        myCommand.Fill(ds,"table1");   
        return ds;
       }
     
     
      

  24.   

    实践出真知!
    是的,我写的时候也是很多人都说需要服务器上有excel!
    但是我们服务器上就没有!
    但是我写的程序依然可以正常运行!
    导出后,给给超链接让他自己去下载~
    我希望楼主去试下!
    如果你真的试过,那我没话说了!
      

  25.   


    非常感谢,你这种方法是我最选想到的,但是,我这个的实际情况和这种方法出入太大,首先是数据量很大,再就是数据很不规则,不是理论上的从一个数据库中按字段导出到excel.
    我需要做很复杂的判断和计算后,再将结果写入excel,而且每次写入的文件行列,字段名和值都是不同的。
    所以用这种方法很不现实,操作起来很麻烦。