C# NPOI 打开Excel报The process cannot access the file because it is being used by another process的解决办法

最近一个项目中需要操作 Excel,我选择使用 NPOI 使用如下方式读取
using var stream = new FileStream(@"test.xlsx", FileMode.Open);
但是 test.xlsx 被其他程序打开的情况下,执行这段代码会报如下错误:
The process cannot access the file 'test.xlsx' because it is being used by another process.
如下图所示:

解决办法
FileStream 有一个带有 FileShare 的构造函数,指定 FileShare 为 ReadWrite ,它的意思就是允许随后打开文件读取或写入,如果未指定此标识,则文件关闭前,任何打开该文件以进行读取或写入的请求(由此进程或另一进程发出)都将失败。
using var stream = new FileStream(@"test.xlsx", FileMode.Open,FileAccess.Read,FileShare.ReadWrite);