我目前正在为我的公司制定一个程序,用于在用户之间传递各种信息,以及其他目前不重要的信息。将要传达的一件事是每日任务清单。基本上,目的是建立一个在所有用户之间同步的检查列表,因此如果一个用户检查任务,其他人都可以看到该任务已经完成。
到目前为止,我所做的是创建了一个DataGrid对象来保存复选框。该框开始为空,但是当程序加载时,运行以下代码,填充列表。
Private Sub chkListPop()
If System.IO.File.Exists(chkListPath) Then
Dim newChk As CheckBox
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBooks As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim rng As Microsoft.Office.Interop.Excel.Range
Dim r As Byte
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBooks = xlApp.Workbooks.Open(chkListPath)
xlWorkSheet = xlWorkBooks.Worksheets("Tasks")
rng = xlWorkSheet.Range("A2")
r = 0
Do While rng.Offset(r).Value <> ""
chkListContainer.Items.Add(New Controls.CheckBox)
newChk = chkListContainer.Items.Item(r)
newChk.Content = rng.Offset(r).Value
Try
If rng.Offset(r, 1).Value = True Then
newChk.IsChecked = True
End If
Catch
End Try
r = r + 1
Loop
xlApp.DisplayAlerts = False
xlWorkBooks.Save()
xlWorkBooks.Close()
xlApp.DisplayAlerts = True
xlApp.Quit()
End If
End Sub
基本上,sub读取excel文件,并且对于文件中的每一行,在第二列中创建一个带有true false值的复选框。我想必须有一个更优雅的解决方案来做到这一点。我不是训练的程序员,所以我对这个“复杂”的主题知之甚少。除了使用excel文件之外,更好的方法是做什么?我应该使用Access数据库还是SQL数据库?这段代码有效,但我觉得它错了。
此外,当有人检查任务时,我无法弄清楚如何更新文件。现在我只运行一个计时器,每隔X分钟,程序就会更新excel文件。以下是其工作原理的代码:
Public Sub chkListSendData()
If System.IO.File.Exists(chkListPath) Then
Dim newChk As CheckBox
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBooks As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim rng As Microsoft.Office.Interop.Excel.Range
Dim r As Byte
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBooks = xlApp.Workbooks.Open(chkListPath)
xlWorkSheet = xlWorkBooks.Worksheets("Tasks")
rng = xlWorkSheet.Range("A2")
r = 0
Do While rng.Offset(r).Value <> ""
Try
newChk = chkListContainer.Items.Item(r)
Catch
chkListContainer.Items.Add(New Controls.CheckBox)
newChk = chkListContainer.Items.Item(r)
newChk.Content = rng.Offset(r).Value
End Try
If newChk.IsChecked Then
rng.Offset(r, 1).Value = True
End If
r = r + 1
Loop
xlApp.DisplayAlerts = False
xlWorkBooks.Save()
xlWorkBooks.Close()
xlApp.DisplayAlerts = True
xlApp.Quit()
End If
End Sub
Public Sub chkListGetData()
If System.IO.File.Exists(chkListPath) Then
Dim newChk As CheckBox
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBooks As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim rng As Microsoft.Office.Interop.Excel.Range
Dim r As Byte
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBooks = xlApp.Workbooks.Open(chkListPath)
xlWorkSheet = xlWorkBooks.Worksheets("Tasks")
rng = xlWorkSheet.Range("A2")
r = 0
Do While rng.Offset(r).Value <> ""
Try
newChk = chkListContainer.Items.Item(r)
Catch
chkListContainer.Items.Add(New Controls.CheckBox)
newChk = chkListContainer.Items.Item(r)
newChk.Content = rng.Offset(r).Value
End Try
Try
If rng.Offset(r, 1).Value = True Then
newChk.IsChecked = True
End If
Catch
End Try
r = r + 1
Loop
xlApp.DisplayAlerts = False
xlWorkBooks.Close()
xlApp.DisplayAlerts = True
xlApp.Quit()
End If
End Sub
Private Sub chkTimer()
Dim dispatcherTimer As DispatcherTimer = New DispatcherTimer()
AddHandler dispatcherTimer.Tick, AddressOf OnTimedEvent
dispatcherTimer.Interval = New TimeSpan(0, 1, 0)
dispatcherTimer.Start()
End Sub
Private Sub OnTimedEvent(ByVal sender As Object, ByVal e As EventArgs)
chkListGetData()
chkListSendData()
End Sub
在我的测试中,这个工作正常,但我觉得当很多人使用该程序时,当多个人同时尝试更新文件时,这将无法正常工作。如果我错了,这段代码很完美,那很好,但我怀疑是这样的。如果有更好的方法来处理这类事情,我很想学习如何在这个程序中实现它。
提前感谢您的帮助。