Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将SQL Server 2008中的表数据提取到Excel电子表格_Excel_Sql Server 2008_Vb6 - Fatal编程技术网

将SQL Server 2008中的表数据提取到Excel电子表格

将SQL Server 2008中的表数据提取到Excel电子表格,excel,sql-server-2008,vb6,Excel,Sql Server 2008,Vb6,我在SQL Server 2008中有一个包含客户端数据的表。非常简单的名字,姓,性,米。我一直在尝试将它提取到Excel,但当我尝试这样做时,它被提取为文本文件。基本上,文本在一个单元格中,看起来很乱。我的代码有什么问题 Private Sub cmdExtract_CLick() Dim filenum as Integer Dim vSQL as String Dim cndX as Adodb.Recordset filenum=freefile

我在SQL Server 2008中有一个包含客户端数据的表。非常简单的名字,姓,性,米。我一直在尝试将它提取到Excel,但当我尝试这样做时,它被提取为文本文件。基本上,文本在一个单元格中,看起来很乱。我的代码有什么问题

Private Sub cmdExtract_CLick()
    Dim filenum as Integer
    Dim vSQL as String
    Dim cndX as Adodb.Recordset

    filenum=freefile
    dim theFilename as string
    theFIlename ="C:\Test\TestFile.CSV"

    Open theFilename for Output as #filenum
    Write #filenum, "First, Last, MI, SEX"

    vSQL = "SELECT FirstName, LastName, Sex, MI from dbo.Test_Clients2"
    cnd.open, vSQL

    set cndx =New Adodb.Recordset
    cndx.move first

    Do while no cndx.eof
    write #filenum, cndx("FirstName") & "," & cndx("LastName") & "," & cndx(Sex)   & "'" & cndx("MI"
    loop

    cndx.close
    cnd.close
    close #filenum
End Sub
这就是我所拥有的。不确定我在哪里犯了错误,但我可能遗漏了什么。数据可以很好地加载到excel工作表中,但都在一个单元格中。

为什么不使用Microsoft提供的。它可能比使用vba从SQL数据库导入数据更有效

例如:

let
    Source = Sql.Database("Servername", "DBname", [Query="SELECT FirstName, LastName, Sex, MI from dbo.Test_Clients2"])
in
    Source

当然,您可以用同样的方法从csv文件中提取数据。

Write
vs
Print
?为什么要编写csv文件?如果您希望将数据输入Excel,请使用。您的代码在某些地方被破坏甚至不完整。