gpt4 book ai didi

c# - 并发读取和写入 Excel 文件

转载 作者:太空狗 更新时间:2023-10-29 23:04:58 27 4
gpt4 key购买 nike

是否有可能只有一个进程对 excel 文件执行 WRITE 而多个进程执行 READ 操作?我为此使用 ExcelPackage(EPPlus)。

为了演示,我编写了两个控制台应用程序,一个用于迭代编写,另一个用于读取。同时运行它们将导致任何一方都失败。

写入

// simply write to a column
var fileLocation = "D:\\Book.xlsx";
FileInfo fi = new FileInfo(fileLocation);

int i = 1;
while (1 == 1) //ALERT: an infinite loop!
{
using (ExcelPackage excelPackage = new ExcelPackage(fi))
{
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["Sheet1"];
var row = worksheet.Row(2);

worksheet.Cells[$"A{i}"].Value = "Test " + i.ToString();
excelPackage.Save();
i++;
}
}

阅读

//simply populate a list reading excel
var fileLocation = "D:\\Book.xlsx";
FileInfo fi = new FileInfo(fileLocation);
List<string> list = new List<string>();
ExcelWorksheet worksheet = null;
int i = 1;
while (1 == 1) //ALERT: an infinite loop!
{
using (ExcelPackage excelPackage = new ExcelPackage(fi))
{
worksheet = excelPackage.Workbook.Worksheets["Sheet1"];
if (worksheet.Cells[i, 1].Value != null)
{
list.Add(worksheet.Cells[i, 1].Value.ToString());
}
}

list.Clear();
}

最佳答案

我稍微更改了我的代码,让 WRITE 程序在写入前锁定文件,并在发生故障时让 READ 程序保持弹性:

写入:使用 FileStream 并在尝试写入之前锁定它。这将防止 WRITE 失败

阅读:添加了一个实现 try/catch block 的重试机制

修改后的代码:

写入

// simply write to a column
var fileLocation = "D:\\Book.xlsx";
FileInfo fi = new FileInfo(fileLocation);

int i = 1;
while (1 == 1) //ALERT: an infinite loop!
{

using (ExcelPackage excelPackage = new ExcelPackage(fi))
{
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["Sheet1"];
worksheet.Cells[$"A{i}"].Value = "Test " + i.ToString();

using (var fs = new FileStream(fileLocation, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
{
fs.Lock(0, fs.Length);
excelPackage.SaveAs(fs);
try
{
fs.Unlock(0, fs.Length); // this raises an exception if fs unlocked already by itself
}
catch (IOException ex) when (ex.Message.ToLower().StartsWith("the segment is already unlocked.",
StringComparison.InvariantCultureIgnoreCase))
{
// NOP; just ignore if already unlocked
}
}
i++;
}
}

阅读

//simply populate a list reading excel
var fileLocation = "D:\\Book.xlsx";
FileInfo fi = new FileInfo(fileLocation);
List<string> list = new List<string>();
ExcelWorksheet worksheet = null;
int i = 1;
while (1 == 1) //ALERT: an infinite loop!
{
try
{
using (ExcelPackage excelPackage = new ExcelPackage(fi))
{
worksheet = excelPackage.Workbook.Worksheets["Sheet1"];
if (worksheet.Cells[i, 1].Value != null)
{
list.Add(worksheet.Cells[i, 1].Value.ToString());
}

Console.WriteLine(worksheet.Dimension.Rows.ToString()); // just prove that it read
}
}

catch (Exception ex) when (
ex is IOException &&
ex.Message.StartsWith("The process cannot access the file because another process has locked a portion of the file.", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine($"Attempt: {i}");
}

list.Clear();
}

在实际应用程序的代码中,我将 READ's WHILE 的限制设置为 3,以便在第一次读取尝试失败时重试两次。事实证明,这对我来说绰绰有余(因为 WRITE 很短;一次添加一行),并且该应用程序在一个月内运行良好。

关于c# - 并发读取和写入 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54231635/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com