0
我正在使用vb.net將數據從mysql導出到ms excel。我可以導出數據,但這裏的問題是數據似乎是多餘的。下面是我的代碼:使用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