要求就是:    我能把查询出来的结果(DataTable)导出到客户机上,由客户自己填写文件名,把数据保存成EXCEL表形式在客户机上。

解决方案 »

  1.   

    http://xml.sz.luohuedu.net/xml/Content.asp
    这里看看
      

  2.   

    楼上是C#的,倒不是看不懂,只是觉得不太熟,不过参考价值是有的有哪位兄弟给段VB.NET写的函数直接让我传一个DATATABLE和一个文件名然后就成了,因为急用所以以方便实用为准,呵呵
      

  3.   

    Dim xlApp As New Excel.Application()
            Dim xlBook As Excel.Workbook
            Dim xlSheet As Excel.Worksheet
            Dim rowIndex, colIndex As Integer
            rowIndex = 1
            colIndex = 0
            xlBook = xlApp.Workbooks().Add
            xlSheet = xlBook.Worksheets("sheet1")
            Dim Col1 As DataColumn
            Dim Row1 As DataRow
            '得到的表所有行,赋值给单元格
            rowIndex = 0
            colIndex = 0
            rowIndex = rowIndex + 1
            Dim cou1 = Session("tb").rows.count
            For i = 0 To cou1 - 1
                Dim r As DataRow = Session("tb").rows(i)
                colIndex = colIndex + 1
                xlApp.Cells(rowIndex, colIndex) = r!name ' Col1.ColumnName
                xlApp.Cells(rowIndex, colIndex).Font.Name = "宋体"
                xlApp.Cells(rowIndex, colIndex).Font.Bold = True
                xlApp.Cells(rowIndex, colIndex).Font.size = 10
                xlApp.Cells(rowIndex, colIndex).Borders.LineStyle = 1
            Next
            For Each Row1 In dv.Table.Rows
                colIndex = 0
                For Each Col1 In dv.Table.Columns
                    colIndex = colIndex + 1
                    xlApp.Cells(rowIndex + 1, colIndex) = "'" + Trim(Row1(Col1.ColumnName))
                    xlApp.Cells(rowIndex + 1, colIndex).Font.Name = "宋体"
                    xlApp.Cells(rowIndex + 1, colIndex).Font.size = 10
                    xlApp.Cells(rowIndex + 1, colIndex).Borders.LineStyle = 1
                Next
                rowIndex = rowIndex + 1
            Next
            xlApp.ActiveSheet.protect()
            xlApp.Visible = True
            xlBook.Saved = True
            xlApp.UserControl = False        Dim mm = Server.MapPath(".") + "\temp\" + Session("username") + ".xls" '服务器保存地址 ")
            xlApp.ActiveWorkbook.SaveCopyAs(mm)
            'xlApp.ActiveWindow.SelectedSheets.PrintOut(mm, Copies:=1, Collate:=True)
            Session("mm") = "temp\" + Session("username") + ".xls"
            Session("m2") = Server.MapPath(".") + "\temp\" + Session("username") + ".xls"
            xlApp.Quit()
      

  4.   

    未定义类型“Excel.Workbook”WHY?
      

  5.   

    我在试了下孟子的导出方法不行提示这个错误错误:HRESULT 中的异常:0xE004002A可不可以有人提供一个可行的方法函数
      

  6.   


    首先你要得到一个数据集     ds.Fill(da, "table1")然后你要把其中的表取出来方便操作     Dim dt As DataTable = da.Tables(0)你这时需要定义一个字符串读取变量,主要目的就是往里写数据然后保存到文件     Dim sw As StringWriter = New StringWriter这里就应该往里写数据了     sw.WriteLine("客户名称,产品名称,单价")  这是表的字段名,这个就是自己写上数据罢了这是循环,从数据集的第一行读到尾,并写入咱们刚才定义的变量     For Each dr As DataRow In dt.Rows
            sw.WriteLine(dr(0) + "," + dr(1) + "," + dr(2))
         Next这段代码是比较常见到的一段,目的很简单就是写文件头,在这里的CSV格式是用“,”号当做分隔符的,所以我们上面的数据也是用的“,”号,如果想保存成XLS也可以,据说分隔符得用"/t",但我试过都集中在一个单元格里了,所以建议还是这样用,到时候客户端自己再保存其它格式就行了        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()
    友情提示:如果你数据库里的某些字段是浮点型或者是数字格式的,请使用CSTR()或者TRIM()之类的转成字符串形式,这样才能写入咱们定义的那个字符串写入变量中。好了,这帖先不结,放着大家有问题再讨论讨论,明天星期一结帖。
      

  7.   

    据说分隔符得用"/t"  ================因为用错了。转意思符号是\t(C#), VB.NET 直接是使用Chr(9)
      

  8.   

    使用EXCEL COM组件完成你的工作,
    首先要引用Microsoft Excel Object Library 9.0 (不同的Office,版本号不同)
    以下是C#的代码,如有需要我可以给你改成VB.NET的。/t --- 表示每个单元格之间的间隔是 Tab 键 ( VB.NET里可以使用 VbTab, Chr(9) )
    /n --  表示每行之间的间隔是 回车 键 (VB.NET里可以使用VbCrLf , VbCr, Chr(13) )  /// <summary>
      /// 将DataSet里所有数据导入Excel.
      /// 需要添加COM: Microsoft Excel Object Library.
      /// using Excel;
      /// </summary>
      /// <param name="filePath"></param>
      /// <param name="ds"></param>
      public static void ExportToExcel(string filePath, DataSet ds)
      {
       object oMissing = System.Reflection.Missing.Value;
       Excel.ApplicationClass xlApp = new Excel.ApplicationClass();
       try
       {
        // 打开Excel文件。以下为Office 2000.
        Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filePath, oMissing, oMissing, oMissing, oMissing, oMissing, 
         oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
         oMissing);
        Excel.Worksheet xlWorksheet;
        // 循环所有DataTable
        for( int i=0; i<ds.Tables.Count; i++ )
        {
         // 添加入一个新的Sheet页。
         xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.Add(oMissing,oMissing,1,oMissing);
         // 以TableName作为新加的Sheet页名。
         xlWorksheet.Name = ds.Tables[i].TableName;
         // 取出这个DataTable中的所有值,暂存于stringBuffer中。
         string stringBuffer = "";
         for( int j=0; j<ds.Tables[i].Rows.Count; j++ )
         {
          for( int k=0; k<ds.Tables[i].Columns.Count; k++ )
          {
           
           stringBuffer += ds.Tables[i].Rows[j][k].ToString();
           if( k < ds.Tables[i].Columns.Count - 1 )
            stringBuffer += "\t";
          }
          stringBuffer += "\n";
         }
         // 利用系统剪切板
         System.Windows.Forms.Clipboard.SetDataObject("");
         // 将stringBuffer放入剪切板。
         System.Windows.Forms.Clipboard.SetDataObject(stringBuffer);
         // 选中这个sheet页中的第一个单元格
         ((Excel.Range)xlWorksheet.Cells[1,1]).Select();
         // 粘贴!
         xlWorksheet.Paste(oMissing,oMissing);
         // 清空系统剪切板。
         System.Windows.Forms.Clipboard.SetDataObject("");
        }
        // 保存并关闭这个工作簿。
        xlWorkbook.Close( Excel.XlSaveAction.xlSaveChanges, oMissing, oMissing );
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook);
        xlWorkbook = null;
       }
       catch(Exception ex)
       {
        MessageBox.Show(ex.Message);
       }
       finally
       {
        // 释放...
        xlApp.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
        xlApp = null;
        GC.Collect();
       }
      }生成临时文件之后,就是按你后面说的方法写回客户端。Response.AddHeader("Content-Disposition", "attachment; filename=test.csv")这里filename是客户另存时的暂时文件名。
    把attachment改为inline是直接打开(如果客户是xp sp2以上版本,仍然探出询问对话框,问是保存还是打开。)
      

  9.   

    Web应用中,使用EXCEL COM组件这种单线程组件,除了需要配置Dcomcnfg为访问Excel提高权限外,还要在Web页上加上AspCompat=true属性。
      

  10.   

    http://blog.csdn.net/hoker_long/archive/2004/09/20/110687.aspx