【问题标题】:Dynamic export from SQL Server Database to Excel从 SQL Server 数据库动态导出到 Excel
【发布时间】:2021-06-21 00:48:37
【问题描述】:

我正在构建一个患者记录导出系统以从 SQL Server 中提取数据。客户想要 Excel 工作簿形式的数据。

数据的保存方式是动态创建数据库中的表,并且列名根据导出参数而变化。

我想要做的是枚举数据库中的表格,然后将这些表格(包括列标题)导出(或导入)到 Excel 中的单独工作表中。

所以:-

如果数据库是:-

dbo.table1 (name, address, postcode)
dbo.table2 (height, weight, headcirc)

我最终得到了一个 Excel 工作簿:-

Sheet1: Name Address Postcode
Sheet2: Height Weight, Headcirc

目前,我研究过的每种方法都需要 Excel 提前知道它接收到的数据的结构。对于给定的导出,不可能事先知道表的数量或这些表中列的名称。

例如,OPENROWSET 要求列标题出现在 Excel 工作簿中。 SSIS 似乎需要对 SQL 表列和 Excel 工作簿列的固定映射。

我能想到的唯一其他方法是使用动态 BCP 进程将所有表导出为 CSV,然后查看是否可以找到一些 VBA 允许我将文件夹中的所有 CSV 文件同化到单个 Excel 工作簿中作为单独的工作表 - 但这看起来很笨重。

我相信这一定是可能的 - 有没有人做到这一点?什么方法可行?

【问题讨论】:

    标签: excel tsql import ssis export


    【解决方案1】:

    可能有多种方法可以做这种事情。其中一些技术可能有效,也可能无效,具体取决于您的设置、您安装的 Excel 和 SQL Server 版本、您安装的其他软件等等...

    1) Export data to existing EXCEL file from SQL Server table
    insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
        'Excel 8.0;Database=D:\testing.xls;', 
        'SELECT * FROM [SheetName$]') select * from SQLServerTable
    
    
    2) If you don't want to create an EXCEL file in advance and want to export data to it, use
    EXEC sp_makewebtask 
        @outputfile = 'd:\testing.xls', 
        @query = 'Select * from Database_name..SQLServerTable', 
        @colheaders =1, 
        @FixedFont=0,@lastupdated=0,@resultstitle='Testing details'
    

    32 位和 64 位驱动程序:

    https://www.microsoft.com/en-us/download/details.aspx?id=13255

    您可以在 Excel 中运行此 VBA。

    Sub TestMacro()
    
    ' Create a connection object.
    Dim cnPubs As ADODB.Connection
    Set cnPubs = New ADODB.Connection
    
    ' Provide the connection string.
    Dim strConn As String
    
    'Use the SQL Server OLE DB Provider.
    strConn = "PROVIDER=SQLOLEDB;"
    
    'Connect to the Pubs database on the local server.
    strConn = strConn & "DATA SOURCE=(local);INITIAL CATALOG=Name_of_your_DB;"
    
    'Use an integrated login.
    strConn = strConn & " INTEGRATED SECURITY=sspi;"
    
    'Now open the connection.
    cnPubs.Open strConn
    
    ' Create a recordset object.
    Dim rsPubs As ADODB.Recordset
    Set rsPubs = New ADODB.Recordset
    
    With rsPubs
        ' Assign the Connection object.
        .ActiveConnection = cnPubs
        ' Extract the required records.
        .Open "SELECT * FROM Categories"
        ' Copy the records into cell A1 on Sheet1.
        Sheet1.Range("A1").CopyFromRecordset rsPubs
        
        ' Tidy up
        .Close
    End With
    
    cnPubs.Close
    Set rsPubs = Nothing
    Set cnPubs = Nothing
    
    End Sub
    

    或者……

    Sub ADOExcelSQLServer()
         
        Dim Cn As ADODB.Connection
        Dim Server_Name As String
        Dim Database_Name As String
        Dim User_ID As String
        Dim Password As String
        Dim SQLStr As String
        Dim rs As ADODB.Recordset
        Set rs = New ADODB.Recordset
         
        Server_Name = "your_server_name" ' Enter your server name here
        Database_Name = "your_database_name" ' Enter your  database name here
        User_ID = "" ' enter your user ID here
        Password = "" ' Enter your password here
        SQLStr = "SELECT * FROM Orders" ' Enter your SQL here
         
        Set Cn = New ADODB.Connection
        Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
        ";Uid=" & User_ID & ";Pwd=" & Password & ";"
         
        rs.Open SQLStr, Cn, adOpenStatic
        
        With Worksheets("Sheet1").Range("A1:Z500")
            .ClearContents
            .CopyFromRecordset rs
        End With
        
        rs.Close
        Set rs = Nothing
        Cn.Close
        Set Cn = Nothing
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多