如果你直接读就会报错

IOException: Sharing violation on path *****

因为文件已经被占用

 

所以你需要用 FileShare.ReadWrite 打开它

FileStream stream = File.Open("myfile.xlsx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

注意要自行排除 ~$ 文件

然后将 FileStream转成bytes就可以用了

 

参考代码

/*----------------------读取被打开的Excel文件---------------------*/
FileStream fileStream = File.Open(info.readPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
fileStream.Seek(0, SeekOrigin.Begin);
return buffer;
/*-------------------------------------------------------------*/