【问题标题】:Exporting powerpivot data to csv将 powerpivot 数据导出到 csv
【发布时间】:2017-12-23 07:07:34
【问题描述】:

我有一个 Excel 工作簿,其中包含 Excel 数据模型中的 powerpivot 数据。我没有用于将数据导入 powerpivot 的文件。我的目标是将数据从 powerpivot 导出到 csv,以便我可以在其他一些软件中使用它。

我在 powerpivot 中找不到任何直接导出选项,并且由于数据大于 110 万行,因此无法将其推送到 Excel 中。

我发现这个 VBA 似乎适用于较小的文件,但对于较大的文件,我会收到超时错误。

Option Explicit

Public Sub ExportToCsv()

    Dim wbTarget As Workbook
    Dim ws As Worksheet
    Dim rs As Object
    Dim sQuery As String

    'Suppress alerts and screen updates
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With

    'Bind to active workbook
    Set wbTarget = ActiveWorkbook

    Err.Clear

    On Error GoTo ErrHandler

    'Make sure the model is loaded
    wbTarget.Model.Initialize

    'Send query to the model
    sQuery = "EVALUATE 'combine 2010 - Q2 2015'"
    Set rs = CreateObject("ADODB.Recordset")
    rs.Open sQuery, wbTarget.Model.DataModelConnection.ModelConnection.ADOConnection
    Dim CSVData As String
    CSVData = RecordsetToCSV(rs, True)

    'Write to file
    Open "D:\tempMyFileName.csv" For Binary Access Write As #1
        Put #1, , CSVData
    Close #1

    rs.Close
    Set rs = Nothing

ExitPoint:
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
    Set rs = Nothing
    Exit Sub

ErrHandler:
    MsgBox "An error occured - " & Err.Description, vbOKOnly
    Resume ExitPoint
End Sub



Public Function RecordsetToCSV(rsData As ADODB.Recordset, _
        Optional ShowColumnNames As Boolean = True, _
        Optional NULLStr As String = "") As String
    'Function returns a string to be saved as .CSV file
    'Option: save column titles

    Dim K As Long, RetStr As String

    If ShowColumnNames Then
        For K = 0 To rsData.Fields.Count - 1
            RetStr = RetStr & ",""" & rsData.Fields(K).Name & """"
        Next K

        RetStr = Mid(RetStr, 2) & vbNewLine
    End If

    RetStr = RetStr & """" & rsData.GetString(adClipString, -1, """,""", """" & vbNewLine & """", NULLStr)
    RetStr = Left(RetStr, Len(RetStr) - 3)

    RecordsetToCSV = RetStr
End Function

【问题讨论】:

    标签: vba export-to-csv powerpivot


    【解决方案1】:

    通过一次执行 1k 行并使用FileSystemObject,这似乎可以在没有导出文件大小限制的情况下工作。您需要添加 Microsoft ActiveX 数据对象库和 Microsoft 脚本运行时作为参考。

    Option Explicit
    
    Public FSO As New FileSystemObject
    
    Public Sub ExportToCsv()
    
        Dim wbTarget As Workbook
        Dim ws As Worksheet
        Dim rs As Object
        Dim sQuery As String
    
        'Suppress alerts and screen updates
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
        End With
    
        'Bind to active workbook
        Set wbTarget = ActiveWorkbook
    
        Err.Clear
    
        On Error GoTo ErrHandler
    
        'Make sure the model is loaded
        wbTarget.Model.Initialize
    
        'Send query to the model
        sQuery = "EVALUATE <Query>"
        Set rs = CreateObject("ADODB.Recordset")
        rs.Open sQuery, wbTarget.Model.DataModelConnection.ModelConnection.ADOConnection
        Dim CSVData As String
        Call WriteRecordsetToCSV(rs, "<ExportPath>", True)
    
        rs.Close
        Set rs = Nothing
    
    ExitPoint:
        With Application
            .ScreenUpdating = True
            .DisplayAlerts = True
        End With
        Set rs = Nothing
        Exit Sub
    
    ErrHandler:
        MsgBox "An error occured - " & Err.Description, vbOKOnly
        Resume ExitPoint
    End Sub
    
    
    
    Public Sub WriteRecordsetToCSV(rsData As ADODB.Recordset, _
            FileName As String, _
            Optional ShowColumnNames As Boolean = True, _
            Optional NULLStr As String = "")
        'Function returns a string to be saved as .CSV file
        'Option: save column titles
    
        Dim TxtStr As TextStream
        Dim K As Long, CSVData As String
    
        'Open file
        Set TxtStr = FSO.CreateTextFile(FileName, True, True)
    
        If ShowColumnNames Then
            For K = 0 To rsData.Fields.Count - 1
                CSVData = CSVData & ",""" & rsData.Fields(K).Name & """"
            Next K
    
            CSVData = Mid(CSVData, 2) & vbNewLine
            TxtStr.Write CSVData
        End If
    
        Do While rsData.EOF = False
            CSVData = """" & rsData.GetString(adClipString, 1000, """,""", """" & vbNewLine & """", NULLStr)
            CSVData = Left(CSVData, Len(CSVData) - IIf(rsData.EOF, 3, 2))
            TxtStr.Write CSVData
        Loop
    
        TxtStr.Close
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 2013-10-17
      • 2018-02-12
      相关资源
      最近更新 更多