Excel 使用数据库在所有用户之间通信数据

Excel 使用数据库在所有用户之间通信数据,excel,vba,Excel,Vba,我目前正在为我的公司开发一个程序,用于在用户之间交流各种信息,以及目前不重要的其他信息。将要传达的内容之一是日常任务清单。基本上,目的是要有一个在所有用户之间同步的检查列表,这样,如果一个用户检查了一个任务,其他所有人都可以看到该任务已经完成 到目前为止,我所做的是创建了一个DataGrid对象来保存复选框。该框开始为空,但当程序加载时,将运行以下代码,从而填充列表 Private Sub chkListPop() If System.IO.File.Exists(chkListPath

我目前正在为我的公司开发一个程序,用于在用户之间交流各种信息,以及目前不重要的其他信息。将要传达的内容之一是日常任务清单。基本上,目的是要有一个在所有用户之间同步的检查列表,这样,如果一个用户检查了一个任务,其他所有人都可以看到该任务已经完成

到目前为止,我所做的是创建了一个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
Private Sub chkListPop()
如果System.IO.File.Exists(chkListPath)存在,则
Dim newChk As复选框
Dim xlApp作为Microsoft.Office.Interop.Excel.Application
将工作簿设置为Microsoft.Office.Interop.Excel.Workbook
将工作表改为Microsoft.Office.Interop.Excel.Worksheet
将rng设置为Microsoft.Office.Interop.Excel.Range
作为字节的Dim r
xlApp=新的Microsoft.Office.Interop.Excel.Application
xlWorkBooks=xlApp.Workbooks.Open(chkListPath)
xlWorkSheet=xlWorkBooks.worksheet(“任务”)
rng=xl工作表范围(“A2”)
r=0
当rng.Offset(r)值为“”时执行此操作
chkListContainer.Items.Add(新控件.复选框)
newChk=chkListContainer.Items.Item(r)
newChk.Content=rng.Offset(r).Value
尝试
如果rng.Offset(r,1).Value=True,则
newChk.IsChecked=True
如果结束
抓住
结束尝试
r=r+1
环
xlApp.DisplayAlerts=False
xlWorkBooks.Save()
xlWorkBooks.Close()
xlApp.DisplayAlerts=True
xlApp.Quit()
如果结束
端接头
基本上,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
公共子目录chkListSendData()
如果System.IO.File.Exists(chkListPath)存在,则
Dim newChk As复选框
Dim xlApp作为Microsoft.Office.Interop.Excel.Application
将工作簿设置为Microsoft.Office.Interop.Excel.Workbook
将工作表改为Microsoft.Office.Interop.Excel.Worksheet
将rng设置为Microsoft.Office.Interop.Excel.Range
作为字节的Dim r
xlApp=新的Microsoft.Office.Interop.Excel.Application
xlWorkBooks=xlApp.Workbooks.Open(chkListPath)
xlWorkSheet=xlWorkBooks.worksheet(“任务”)
rng=xl工作表范围(“A2”)
r=0
当rng.Offset(r)值为“”时执行此操作
尝试
newChk=chkListContainer.Items.Item(r)
抓住
chkListContainer.Items.Add(新控件.复选框)
newChk=chkListContainer.Items.Item(r)
newChk.Content=rng.Offset(r).Value
结束尝试
如果选中了newChk,那么
rng偏移量(r,1)。值=真
如果结束
r=r+1
环
xlApp.DisplayAlerts=False
xlWorkBooks.Save()
xlWorkBooks.Close()
xlApp.DisplayAlerts=True
xlApp.Quit()
如果结束
端接头
公共子目录chkListGetData()
如果System.IO.File.Exists(chkListPath)存在,则
Dim newChk As复选框
Dim xlApp作为Microsoft.Office.Interop.Excel.Application
将工作簿设置为Microsoft.Office.Interop.Excel.Workbook
将工作表改为Microsoft.Office.Interop.Excel.Worksheet
将rng设置为Microsoft.Office.Interop.Excel.Range
作为字节的Dim r
xlApp=新的Microsoft.Office.Interop.Excel.Application
xlWorkBooks=xlApp.Workbooks.Open(chkListPath)
xlWorkSheet=xlWorkBooks.worksheet(“任务”)
rng=xl工作表范围(“A2”)
r=0
当rng.Offset(r)值为“”时执行此操作
尝试
newChk=chkListContainer.Items.Item(r)
抓住
chkListContainer.Items.Add(新控件.复选框)
newChk=chkListContainer.Items.Item(r)
newChk.Content=rng.Offset(r).Value
结束尝试
尝试
如果rng.Offset(r,1).Value=True,则
newChk.IsChecked=True
如果结束
抓住
结束尝试
r=r+1
环
xlApp.DisplayAlerts=False
xlWorkBooks.Close()
xlApp.DisplayAlerts=True
xlApp.Quit()
如果结束
端接头
私有子chkTimer()
Dim Dispatchermer As Dispatchermer=新Dispatchermer()
AddHandler dispatcherTimer.Tick,AddressOf OnTimedEvent
Dispatchermer.Interval=新的时间跨度(0,1,0)
dispatchermer.Start()
端接头
私有子OnTimedEvent(ByVal发送方作为对象,ByVal e作为事件参数)
chkListGetData()
chkListSendData()
端接头
在我的测试中,这很好,但我有一种感觉,当很多人在使用该程序时,当多人同时尝试更新该文件时,这将无法很好地工作。如果我错了,而这个代码是完美的,那就太好了,但我怀疑情况是否如此。如果有更好的方法来处理这类事情,我很想学习如何在这个程序中实现它


提前感谢您的帮助。

由于excel似乎是其中的一个主要元素,您可能需要添加它