如何将数据导出到word文档,而不是SQL Query? VB.NET

时间:2014-06-18 15:45:19

标签: mysql vb.net word-automation

我一直在努力解决这个问题。我编写了一个SQL查询来选择与特定机构有关系的某些数据。现在,当我在MySQL Workbench中测试时,SQL查询工作得非常好,但是,当我尝试将这些数据从VB.NET导出到word文档时,它会打印出SQL。

以下是我的代码:

    Dim sqlAdapter As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim sqlTable As New DataTable

    Dim sqlFundText As String = "select mutual_Fund_name, concat(contact_first_name,' ',contact_last_name) from mutual_fund mf, contact c, mutual_fund_has_contact mfhc, institution i, institution_has_mutual_Fund ihmf where mf.mutual_fund_id = mfhc.mutual_fund_id and c.contact_id = mfhc.contact_id and ihmf.mutual_fund_id = mf.mutual_fund_id and i.institution_id = ihmf.institution_id and i.institution_name ='" & InstitutionNameTextBox.Text & "' order by mutual_fund_name;"

    With sqlCommand
        .CommandText = sqlFundText
        .Connection = sConnection
    End With

    With sqlAdapter
        .SelectCommand = sqlCommand
        .Fill(sqlTable)
    End With

    oPara9 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
    With oPara9
        .Range.Font.Bold = False
        .Range.Text = sqlFundText
        .Range.Font.Size = 10
        .Format.SpaceAfter = 5
        .Range.InsertParagraphAfter()
    End With

结果是:

enter image description here

如您所见,它打印出SQL语句。

我知道这与

有关
.Range.Text = sqlFundText

我只是不知道如何解决它。任何人都可以指导我正确的方法解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

您的查询中的数据位于sqlTable。您需要从数据表中提取数据并将其添加到文档而不是sqlFundText

With sqlAdapter ... End With阻止后,您需要执行以下操作:

Dim fundName as String
Dim contactName as String
For Each row in sqlTable.Rows
    fundName = row[0].ToString()
    contactName = row[1].ToString()
    ' Do something to put fundName and contactName into the document
Next

答案 1 :(得分:0)

这是一个接受SQL并返回CSV的Sub()。它不是防弹,但它适用于我的实用程序代码。

在您的情况下,您可以使用制表符作为分隔符,以便在Word中存在数据后,可以轻松地将其转换为表格。

Yu也可以使用代码在Word中创建/填充表格。

   Function CSVReportFromSQL(SQL As String, Optional ToFilePath As String = "") As String
    ' note return differences if ToFilePath is provided
    '   If ToFilePath: check for 'OK' on return
    '   Else check return length = 0 for error 
    Try
        Dim sOut As String = ""

        Using con As New SqlConnection(g.OISConnectString)
            Dim command As New SqlCommand(SQL, con)
            con.Open()

            ' column headers
            Dim rdr As SqlDataReader = command.ExecuteReader(CommandBehavior.SchemaOnly)
            For i As Integer = 0 To rdr.FieldCount - 1
                sOut &= "," & rdr.GetName(i)
            Next
            sOut = sOut.Substring(1) & vbCrLf
            rdr.Close()

            rdr = command.ExecuteReader()
            While rdr.Read()
                For i As Integer = 0 To rdr.FieldCount - 1
                    'Debug.Print(rdr.GetFieldType(i).Name & " " & rdr.GetName(i))
                    'http://msdn.microsoft.com/en-us/library/4e5xt97a(v=vs.80).aspx
                    Select Case rdr.GetFieldType(i).Name
                        Case "String", "DateTime"
                            sOut &= """" & rdr(i).ToString.Replace("""", """""") & ""","
                        Case Else
                            sOut &= rdr(i).ToString & ","
                    End Select
                Next
                sOut &= vbCrLf
            End While
            rdr.Close()
        End Using

        If ToFilePath.Length > 0 Then
            My.Computer.FileSystem.WriteAllText(ToFilePath, sOut, False)
            Return "OK"
        Else
            Return sOut
        End If

    Catch ex As Exception
        If ToFilePath.Length > 0 Then
            Return ex.Message
        Else
            MsgBox("Problem creating CSV Report" & vbCrLf & ex.Message)
            Return ""
        End If
    End Try
End Function