我正在VB中编写代码,以使公司站点具有更新的员工信息。 我的目标是: 从Excel文件中收集某些数据 将数据保存到2D阵列中 将阵列数据更新到网站可以访问的数据库中 创建网站并轻松使用数据库数据(以便轻松编辑网站)
我遇到了两个问题:
当前,我们使用带有宏&Excel VB的Excel文件来生成站点(将输出直接写入html文件-因此非常静态。)
由于私人员工的信息,我无法共享excel文件,但是我共享了代码(我知道,这有点草率,因为这是我第一次使用excel数据,因此我做了很多“重新-编辑我的代码)
(最后我添加了一个listbox1更新以验证是否正确添加了最后一条记录)
如果有更好的解决方案(从excel直接导入数据库),请共享:)
谢谢大家!
Imports Excel = Microsoft.Office.Interop.Excel
Public Class WieiswieForm
Private Sub WieiswieForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Public Sub ChoseFile_Click(sender As Object, e As EventArgs) Handles ChoseFile.Click
OpenFileDialog1.Filter = "Excel | *.xlsm"
Dim result As DialogResult = OpenFileDialog1.ShowDialog()
FileTextBox.Text = OpenFileDialog1.FileName
UpdateButton.Enabled = True
End Sub
Public Sub UpdateButton_Click(sender As Object, e As EventArgs) Handles UpdateButton.Click
UpdateButton.Enabled = False
ListBox1.Items.Add(DateAndTime.Now.ToString & ": starten...")
Dim Filepath As String = OpenFileDialog1.FileName
Dim Afdeling_cell As String = "C"
Dim Functie_cell As String = "D"
Dim Naam_cell As String = "E"
Dim Organistaie_cell As String = "N"
Dim Badgenummer_cell As String = "R"
Dim Draagbaar_cell As String = "Y"
Dim VastNummer_cell As String = "X"
Dim GSM_cell As String = "AB"
Dim Werkrooster_cell As String = "AE"
Dim Normtijd_cell As String = "AF"
Dim Afwezigheid_cell As String = "AG"
Dim Vestiging_cell As String = "AI"
Dim Cell_index(11) As String
Cell_index(0) = Naam_cell
Cell_index(1) = Afdeling_cell
Cell_index(2) = Functie_cell
Cell_index(3) = Organistaie_cell
Cell_index(4) = Vestiging_cell
Cell_index(5) = Werkrooster_cell
Cell_index(6) = VastNummer_cell
Cell_index(7) = Draagbaar_cell
Cell_index(8) = GSM_cell
Cell_index(9) = Afwezigheid_cell
Cell_index(10) = Badgenummer_cell
Cell_index(11) = Normtijd_cell
If Filepath = "" Then
MsgBox("Geen bestand geselecteerd..")
Else
ListBox1.Items.Add(DateAndTime.Now.ToString & ": Excel bestand openen...")
Dim range As Excel.Range
Dim file As System.IO.StreamWriter
' Get the Excel application object.
Dim excel_app As New Excel.ApplicationClass()
' Make Excel visible (optional).
excel_app.Visible = False
' Open the workbook read-only.
Dim workbook As Excel.Workbook =
excel_app.Workbooks.Open(
Filename:=Filepath, ReadOnly:=True)
' Get the first worksheet.
Dim sheet As Excel.Worksheet =
DirectCast(workbook.Sheets(1), Excel.Worksheet)
' Get the number of rows (to avoid an index out-of-range)
Dim LastRow As Integer
LastRow = sheet.UsedRange.Rows.Count
'################## SUB START
'Create the data array
ListBox1.Items.Add(DateAndTime.Now.ToString & ": data verzamel object maken...")
Dim DataArray(11, LastRow - 1) As String
'Create the object to retrieve the data from the range
Dim saRet(,) As Object
Dim iRows As Long
Dim iCols As Long
' #########################################################################################
' Start the data colletion loop
Dim arrayindex1 As Integer = 0
Dim arrayindex2 As Integer = 0
Dim c As Integer = 0
ProgressBar1.Value = 10
'Namen oplijsten :
ListBox1.Items.Add(DateAndTime.Now.ToString & ": namen ophalen...")
MaakArray(arrayindex1, arrayindex2, c, sheet, LastRow, range, saRet, iRows, iCols, Cell_index, DataArray)
'Afdeling ophalen
ListBox1.Items.Add(DateAndTime.Now.ToString & ": afdelingen ophalen...")
ProgressBar1.Value = 20
c = c + 1 '1
arrayindex1 = arrayindex1 + 1
arrayindex2 = 0
MaakArray(arrayindex1, arrayindex2, c, sheet, LastRow, range, saRet, iRows, iCols, Cell_index, DataArray)
'Functie binnen de afdeling :
ListBox1.Items.Add(DateAndTime.Now.ToString & ": functies ophalen...")
ProgressBar1.Value = 30
c = c + 1 '2
arrayindex1 = arrayindex1 + 1
arrayindex2 = 0
MaakArray(arrayindex1, arrayindex2, c, sheet, LastRow, range, saRet, iRows, iCols, Cell_index, DataArray)
'Organisatie ophalen
ListBox1.Items.Add(DateAndTime.Now.ToString & ": organisaties ophalen...")
ProgressBar1.Value = 40
c = c + 1 '3
arrayindex1 = arrayindex1 + 1
arrayindex2 = 0
MaakArray(arrayindex1, arrayindex2, c, sheet, LastRow, range, saRet, iRows, iCols, Cell_index, DataArray)
'Vestiging ophalen
ListBox1.Items.Add(DateAndTime.Now.ToString & ": vestigingen ophalen...")
ProgressBar1.Value = 50
c = c + 1 '4
arrayindex1 = arrayindex1 + 1
arrayindex2 = 0
MaakArray(arrayindex1, arrayindex2, c, sheet, LastRow, range, saRet, iRows, iCols, Cell_index, DataArray)
'werkrooster ophalen
ListBox1.Items.Add(DateAndTime.Now.ToString & ": werkroosters ophalen...")
ProgressBar1.Value = 60
c = c + 1 '5
arrayindex1 = arrayindex1 + 1
arrayindex2 = 0
MaakArray(arrayindex1, arrayindex2, c, sheet, LastRow, range, saRet, iRows, iCols, Cell_index, DataArray)
'vastnummer ophalen
ListBox1.Items.Add(DateAndTime.Now.ToString & ": vaste telefoonnummers ophalen...")
ProgressBar1.Value = 70
c = c + 1 '6
arrayindex1 = arrayindex1 + 1
arrayindex2 = 0
MaakArray(arrayindex1, arrayindex2, c, sheet, LastRow, range, saRet, iRows, iCols, Cell_index, DataArray)
'draagpaar ophalen
ListBox1.Items.Add(DateAndTime.Now.ToString & ": draagbarenummers ophalen...")
ProgressBar1.Value = 75
c = c + 1 '7
arrayindex1 = arrayindex1 + 1
arrayindex2 = 0
MaakArray(arrayindex1, arrayindex2, c, sheet, LastRow, range, saRet, iRows, iCols, Cell_index, DataArray)
'GSM ophalen
ListBox1.Items.Add(DateAndTime.Now.ToString & ": GSM nummers ophalen...")
ProgressBar1.Value = 80
c = c + 1 '8
arrayindex1 = arrayindex1 + 1
arrayindex2 = 0
MaakArray(arrayindex1, arrayindex2, c, sheet, LastRow, range, saRet, iRows, iCols, Cell_index, DataArray)
'Afwezigheden ophalen
ListBox1.Items.Add(DateAndTime.Now.ToString & ": Afwezigheden nummer ophalen...")
ProgressBar1.Value = 85
c = c + 1 '9
arrayindex1 = arrayindex1 + 1
arrayindex2 = 0
MaakArray(arrayindex1, arrayindex2, c, sheet, LastRow, range, saRet, iRows, iCols, Cell_index, DataArray)
'Badgenummers ophalen
ListBox1.Items.Add(DateAndTime.Now.ToString & ": badgenummers ophalen...")
ProgressBar1.Value = 90
c = c + 1 '10
arrayindex1 = arrayindex1 + 1
arrayindex2 = 0
MaakArray(arrayindex1, arrayindex2, c, sheet, LastRow, range, saRet, iRows, iCols, Cell_index, DataArray)
'Normtijd ophalen
ListBox1.Items.Add(DateAndTime.Now.ToString & ": Normtijd ophalen...")
ProgressBar1.Value = 95
c = c + 1 '10
arrayindex1 = arrayindex1 + 1
arrayindex2 = 0
MaakArray(arrayindex1, arrayindex2, c, sheet, LastRow, range, saRet, iRows, iCols, Cell_index, DataArray)
' Save the changes and close the workbook.
workbook.Close(SaveChanges:=False)
' Close the Excel server.
excel_app.Quit()
ProgressBar1.Value = 100
ListBox1.Items.Add(DateAndTime.Now.ToString & ": Bestand opgeladen!")
ListBox1.Items.Add(DataArray(0, 501) & " " & DataArray(1, 501) & " " & DataArray(2, 501) & " " & DataArray(3, 501) & DataArray(4, 501) & DataArray(5, 501) & DataArray(6, 501) & DataArray(7, 501) & DataArray(8, 501))
UpdateButton.Enabled = True
MsgBox((DataArray(0, 501) & " " & DataArray(1, 501) & " " & DataArray(2, 501) & " " & DataArray(3, 501) & " " & DataArray(4, 501) & " " & DataArray(5, 501) & " " & DataArray(6, 501) & " " & DataArray(7, 501) & " " & DataArray(8, 501) & " " & DataArray(9, 501) & " " & DataArray(10, 501)))
End If
End Sub
Sub MaakArray(arrayindex1 As Integer, arrayindex2 As Integer, c As Integer, sheet As Excel.Worksheet, LastRow As Integer, range As Excel.Range, saret As Object, iRows As Long, iCols As Long, Cell_index() As String, ByRef DataArray(,) As String)
Dim LoopTime As Integer = 0
Do Until LoopTime = 11
'Get a range of data.
range = sheet.Range(Cell_index(c) & "2", Cell_index(c) & LastRow)
'Retrieve the data from the range.
saret = range.Value
'Determine the dimensions of the array.
iRows = saret.GetUpperBound(0)
iCols = saret.GetUpperBound(1)
'Build a string that contains the data of the array.
Dim valueString As String
'valueString = "" + vbCrLf
Dim rowCounter As Long
Dim colCounter As Long
Dim i As Integer
For rowCounter = 1 To iRows
For colCounter = 1 To iCols
'Write the next value into the string.
Try
valueString = saret(rowCounter, colCounter).ToString()
'If String.IsNullOrEmpty(valueString) Then
'valueString = ""
' End If
Catch
valueString = ""
End Try
DataArray(arrayindex1, arrayindex2) = valueString
If arrayindex2 = LastRow - 1 Then
arrayindex2 = arrayindex2
Else
arrayindex2 = arrayindex2 + 1
End If
ProgressBar1.Value = ProgressBar1.Value + 1 / 10
Next colCounter
Next rowCounter
LoopTime = LoopTime + 1
Loop
'#########################################################################
End Sub
End Class
答案 0 :(得分:0)
只需咧着嘴笑,请尝试以下操作,看看会得到什么。您可能必须将连接字符串中的HDR = YES调整为HDR = NO。将DataGridView添加到您的窗体,以便您可以查看结果。至少这将为您介绍Excel的数据提供程序。有了DataTable后,如果有主键,就可以更新数据库。
添加到文件顶部
导入System.Data.OleDb
表单代码中的
while