写入excel和只读属性vb.net

时间:2016-11-23 06:52:30

标签: .net excel vb.net visual-studio-2013

我在Visual Studio 2013中创建了一个表单,用于同时由多个用户在Excel中输入数据。它工作得非常好,除非两个用户同时打开表单或者如果一个用户单击提交并且表单仍处理,则会发生错误。

  

您的应用程序中发生了未处理的异常。来自HRESULT的异常:0x800A03EC

将数据写入文件时,我想将其设为只读,以便其他用户可以等待。我该怎么做?

Imports excel = Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices

Public Class Form1
Dim xlapp As New excel.Application
Dim workbook As excel.Workbook
Dim worksheet As excel.Worksheet

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
workbook = xlapp.Workbooks.Open("\2.xlsx")
xlapp.Visible = False
worksheet = workbook.Sheets("sheet1")
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

If workbook.MultiUserEditing Then
    workbook.AcceptAllChanges()
    workbook.Save()
End If

Dim row As Long
Dim alpha As Long = 0
row = 5
With worksheet
    Do While .Cells(row, 4).value IsNot Nothing
        row = row + 1
    Loop

    .Cells(row, 2).value = Me.fname.Text
    .Cells(row, 3).value = Me.lname.Text
    Me.fullname.Text = Me.fname.Text + Me.lname.Text
    .Cells(row, 4).value = Me.fullname.Text

End With
xlapp.DisplayAlerts = False

workbook.SaveAs("\2.xlsx", AccessMode:=excel.XlSaveAsAccessMode.xlShared)

End Sub
End Class

1 个答案:

答案 0 :(得分:0)

你想要的是,文件应该是互相排斥的,即如果某人打开它进行写作,其他人就无法打开它进行编辑。 解决此问题的一个简单方法是,每当用户打开文件时都会跟踪它。这可以通过在DB或文件系统中跟踪表来实现。

  1. 对于数据库方法: - 检查表中是否存在filename的记录。如果是,请将标签的文本设置为"只读"并且不允许保存。如果未找到记录,请在DB中插入记录并允许保存。保存/关闭表单时从表中删除此记录。即使你可以通过跟踪哪个用户打开/关闭它以及在什么时间跟踪它来扩展功能。在这种情况下,您不需要删除记录,但添加更多列IsOpened,User,StartDateTime,EndDateTime。
  2. 对于文件系统方法: - 检查filename文件夹中是否存在Lock的空文件。如果文件存在,请将标签的文本设置为" Read only"并且不允许保存。如果文件不存在,请在filename文件夹中创建一个Lock的空文件,并允许保存。保存/关闭表单时从文件夹中删除此文件。
  3. 更新了文件系统方法: - 以下是向您展示的方法。我没有编译代码。请原谅我。

    1. 创建一个文件夹" Lock"在将保存Excel文件的位置。
    2. 在表单上添加指标(例如标签lblAccess)。
    3. 更新Form1_Load()以检查锁定文件夹中是否存在文件,并指明用户有关访问权限。
    4. 更新Button1_Click功能以执行或不执行另存为
    5. ---代码低于---

      Dim lockFile As String ' form/class level variable
      
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
      
          Dim filename As String = "2.xlsx"
          Dim sharedFolder As String = "\\ComputerABC\XL_FILE_FOLDER\LOCK\"
      
          lockFile = sharedFolder + filename
          If (System.IO.File.Exists(lockFile)) Then
              Me.lblAccess.Visible = True
              Me.lblAccess.Text = "Read-Only Access. Data cannot be saved."
          Else
              IO.File.Create(lockFile).Dispose() 'create empty file
              Me.lblAccess.Visible = False
          End If
          Me.Button1.Enabled = Not Me.lblAccess.Visible
      
          workbook = xlapp.Workbooks.Open("\" + filename)
          xlapp.Visible = False
          worksheet = workbook.Sheets("sheet1")
      End Sub
      
      
      
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
          :
          :
          :
          If Not lblAccess.Visible Then
              workbook.SaveAs("\2.xlsx", AccessMode:=excel.XlSaveAsAccessMode.xlShared)
              System.IO.File.Delete(lockFile)
              lockFile = ""
          End If
      
      End Sub