当 2 个程序尝试读取和写入 csv 文件时,运行时错误 70 Permission Denied

Runtime error 70 Permission Denied when 2 programs try to read and write the csv file

我在 excel 中有 VBA 脚本,当 Worksheet Calculate 的状态在给定单元格中发生时会触发该脚本。该脚本将数据写入 csv 文件。只需将 20 个单元格写入 csv 文件。 Worksheet Calculate 被触发的速度 = 100ms 到 200ms,因为数据几乎是实时变化的。

另一方面,我有 asp.net MVC 项目,当发送 api get 请求时,它试图读取同一个 CSV 文件。

VBA脚本单独执行不会导致任何问题。但是当我尝试读取相同的 csv 文件时,我看到我的 VBA 脚本暂停并给出以下错误:Runtime error 70 Permission Denied

我认为这可能是因为 2 个程序正试图为 read/write 同时访问同一个 csv 文件。

VBA代码

Private Sub Worksheet_Calculate()
Dim Target As Range

Dim myFile As String
Dim rng As Range
Dim cellValue As Variant
Dim i As Integer
Dim j As Integer
Dim count As Integer
Dim xval As Integer
Dim yval As Integer
xval = 6
yval = 8
count = 0

myFile = "C:\Users\test\Desktop\saved.csv"
Set rng = Range("D15:I15")

    If Not Intersect(rng, Range("D15:I15")) Is Nothing Then

        Open myFile For Output As #1
            For i = 1 To yval
                For j = 1 To xval
                    cellValue = rng.Cells(i, j).Value
                    If j = xval Then
                        Write #1, cellValue
                    Else
                        Write #1, cellValue,
                    End If
                Next j
            Next i
        Close #1
    End If
End Sub

我找到了这篇文章:https://varunvns.wordpress.com/2012/05/05/reading-and-writing-in-text-files-with-multiple-programs-accessing-it-simultaneously/

这可能是我的问题的解决方案。 文件共享。但我不确定如何在 VBA Excel 中设置这样的标志,当写入 ReadWrite 和 asp.net 代码时,如何在访问 csv 文件时将标志设置为 Read 。

如果您的 asp.net MVC 项目 只需要读取文件然后以 只读模式 打开文件,这样它不影响 VBA.

的写入

一个文件在写模式下只能同时打开一次,但在只读模式下可以多次打开。

尽管如此,我认为读取每 200 毫秒更改一次的文件没有多大意义。如果您阅读数据,它已经很旧了。