如何将 Quickbooks 报告导出到 Excel?

How do I export a Quickbooks report to Excel?

我开始了一个 Windows 表单项目,以根据 Quickbooks 报告生成现金流量报告。我想使用 Quickbooks SDK 登录到 Quickbooks 并将报告导出到 Excel。我已成功登录,但无法将报告导出到 Excel。是否可以使用 SendKeys

这是我的代码:

Imports QBFC13Lib

Public Class Form1

    Private Sub GetReportToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GetReportToolStripMenuItem.Click
        Dim sessionBegun As Boolean
        sessionBegun = False
        Dim connectionOpen As Boolean
        connectionOpen = False
        Dim sessionManager As QBSessionManager
        sessionManager = Nothing

        Try

            'Create the session Manager object
            sessionManager = New QBSessionManager


            'Connect to QuickBooks and begin a session
            sessionManager.OpenConnection("", "Your application")
            connectionOpen = True
            sessionManager.BeginSession("", ENOpenMode.omDontCare)
            sessionBegun = True

            MsgBox("started session!")

            'End the session and close the connection to QuickBooks
            sessionManager.EndSession()
            sessionBegun = False
            sessionManager.CloseConnection()
            connectionOpen = False

            MsgBox("Connection Closed")

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error")
            If (sessionBegun) Then
                sessionManager.EndSession()
            End If
            If (connectionOpen) Then
                sessionManager.CloseConnection()
            End If
        End Try


        sessionManager = Nothing
        sessionBegun = Nothing
        connectionOpen = Nothing

    End Sub

QuickBooks SDK 本身不支持导出到 Excel,因此您必须编写一些代码来执行此操作。

完成登录后,您可以按照以下方式导出报告:

var query = msgSetRq.AppendGeneralSummaryReportQueryRq();
query.GeneralSummaryReportType.SetValue(ENGeneralSummaryReportType.gsrtLotNumberInStockBySite);

msgSetRq.Attributes.OnError = ENRqOnError.roeContinue;

var msgSetRs = sessManager.DoRequests(msgSetRq);

我强烈建议您查看 Intuit 关于使用 QBFC 报告的文档:

  1. https://developer-static.intuit.com/qbSDK-current/doc/PDF/QBSDK_ProGuide.pdf
  2. https://developer.intuit.com/app/developer/qbdesktop/docs/develop/exploring-the-quickbooks-desktop-sdk/preparing-report-requests

您将取回一个包含所有报告数据的对象。然后您可以遍历报告并构建您的 Excel sheet。

创建 Excel sheets 看起来像这样:

Dim appXL As Excel.Application
      Dim wbXl As Excel.Workbook
      Dim shXL As Excel.Worksheet
      Dim raXL As Excel.Range

      ' Start Excel and get Application object.
      appXL = CreateObject("Excel.Application")
      appXL.Visible = True

      ' Add a new workbook.
      wbXl = appXL.Workbooks.Add
      shXL = wbXl.ActiveSheet


      ' Create an array to set multiple values at once.
      Dim qbdata(5, 2) As String
      qbdata(0, 0) = "data from QB here"
      qbdata(0, 1) = "and here"

      ' Fill A2:B6 with an array of data
      shXL.Range("A2", "B6").Value = qbdata


      ' Make sure Excel is visible and give the user control
      ' of Excel's lifetime.
      appXL.Visible = True
      appXL.UserControl = True

      ' Release object references.
      raXL = Nothing
      shXL = Nothing
      wbXl = Nothing
      appXL.Quit()
      appXL = Nothing