【问题标题】:Exporting data from mysql to ms excel using vb.net使用 vb.net 将数据从 mysql 导出到 ms excel
【发布时间】:2017-07-04 20:22:18
【问题描述】:

我正在使用 vb.net 将数据从 mysql 导出到 ms excel。我可以导出数据,但这里的问题是数据似乎是多余的。以下是我的代码:

    Dim unit(8) As String
    unit(0) = "RHQ"
    unit(1) = "RPSB"
    unit(2) = "ADN"
    unit(3) = "ADS"
    unit(4) = "BCPO"
    unit(5) = "DIPPO"
    unit(6) = "SDN"
    unit(7) = "SDS"

    Dim dataSet As New DataSet
    Dim datatableMain As New System.Data.DataTable()
    Dim dataAdapter As OdbcDataAdapter

    Dim dc As System.Data.DataColumn
    Dim dr As System.Data.DataRow
    Dim colIndex As Integer = 0
    Dim rowIndex As Integer = 0

    'create objects to interface to Excel
    Dim xls As New Excel.Application
    Dim book As Excel.Workbook
    Dim sheet As Excel.Worksheet

    'Export the listview to an Excel spreadsheet
    SaveFileDialog1.Title = "Save Excel File"
    SaveFileDialog1.Filter = "Excel files (*.xls)|*.xls|Excel Files (*.xlsx)|*.xslx"
    SaveFileDialog1.ShowDialog()
    'exit if no file selected
    If SaveFileDialog1.FileName = "" Then
        Exit Sub
    End If

    'create a workbook and get reference to first worksheet
    xls.Workbooks.Add()
    book = xls.ActiveWorkbook
    sheet = book.ActiveSheet

    For Each element As String In unit

        If element <> "" Then
            colIndex = 0
            'Get Number of Applicant
            connect_db()
            cmd = New OdbcCommand
            cmd.Connection = con
            cmd.CommandText = "SELECT COUNT(*) FROM personnel WHERE p_unit='" & element & "' AND p_rank='" & account_type & "'"
            rs = cmd.ExecuteReader()
            rs.Read()
            applicant = rs(0).ToString()
            con.Close()
            MessageBox.Show(element)
            'Generate Qouta
            Dim rnk As String = ""
            connect_db()
            cmd = New OdbcCommand
            cmd.Connection = con
            If account_type = "PO1" Then
                rnk = "PO1-PO2"
            ElseIf account_type = "PO2" Then
                rnk = "PO2-PO3"
            ElseIf account_type = "PO3" Then
                rnk = "PO3-SPO1"
            ElseIf account_type = "SPO1" Then
                rnk = "SPO1-SPO2"
            ElseIf account_type = "SPO2" Then
                rnk = "SPO2-SPO3"
            ElseIf account_type = "SPO3" Then
                rnk = "SPO3-SPO4"
            End If
            cmd.CommandText = "SELECT p_qouta, p_promotable, p_rank, rec_date FROM promotion_qouta WHERE p_rank = '" & rnk & "' AND rec_date = '" & reckon_date & "'"
            rs = cmd.ExecuteReader()
            rs.Read()
            quota = rs(0).ToString()
            promotable = rs(1).ToString()
            dist = Val(applicant) * Val(quota)
            dist = Val(dist) / Val(promotable)
            dist = Math.Round(Val(dist))
            con.Close()

            'Fill data
            connect_db()
            cmd.Dispose()
            cmd = New OdbcCommand
            cmd.Connection = con
            cmd.CommandText = "SELECT rec_no, badge_no, p_rank, f_name, m_name, l_name, qualifier, point, p_unit FROM personnel WHERE rec_date = '" & reckon_date & "' AND p_rank = '" & account_type & "' AND p_unit='" & element & "' ORDER BY point DESC LIMIT 0," & dist & ""
            dataAdapter = New OdbcDataAdapter
            dataAdapter.SelectCommand = cmd

            'Fill data to datatable

            dataAdapter.Fill(datatableMain)
            con.Close()

            'step through rows and columns and copy data to worksheet
            'Export the Columns to excel file

            rowIndex = rowIndex + 2
            sheet.Cells(rowIndex, 1) = element
            sheet.Range("A" & rowIndex).Font.Bold = True
            sheet.Range("A" & rowIndex).VerticalAlignment = ContentAlignment.TopCenter
            sheet.Range("A" & rowIndex).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
            sheet.Range(sheet.Cells(rowIndex, 1), sheet.Cells(rowIndex, 9)).Merge()
            rowIndex = rowIndex + 1


            For Each dc In datatableMain.Columns
                colIndex = colIndex + 1
                sheet.Cells(rowIndex, colIndex) = dc.ColumnName
            Next

            'Export the rows to excel file
            For Each dr In datatableMain.Rows
                rowIndex = rowIndex + 1
                colIndex = 0
                For Each dc In datatableMain.Columns
                    colIndex = colIndex + 1
                    sheet.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
                Next

            Next

        End If
    Next
    'save the workbook and clean up
    book.SaveAs(SaveFileDialog1.FileName)
    xls.Workbooks.Close()
    xls.Quit()
    releaseObject(sheet)
    releaseObject(book)
    releaseObject(xls)

    MessageBox.Show("Export Successful!", "Export", MessageBoxButtons.OK, MessageBoxIcon.Information)

这是输出: Output

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    也许你应该重新考虑你的嵌套 for 循环:你得到的正是你所编程的。

    for each table
       write the header 
       write datas
    next table
    

    如果我正确理解了您的问题,您应该这样写:

    Write the header
    for each table
        write datas
    next table
    

    【讨论】:

      猜你喜欢
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多