我正在使用C#framework 4.5,netoffice 1.6和sharpdevelop 4.4.1来操作位于网络共享中的excel工作簿,从Outlook中.
在某些时候,我需要更改工作簿对象(ewb)的文件访问权限,以便像这样读写:
ewb.ChangeFileAccess(Excel.Enums.XlFileAccess.xlReadWrite, System.Reflection.Missing.Value, true);
Run Code Online (Sandbox Code Playgroud)
在更改文件访问权限之前,我检查文件是否在服务器上被锁定.如果文件被锁定,我将通知用户稍后重试该操作.
现在,我想在通知中包含锁定excel文件的用户名.我搜索了msdn,netoffice论坛等等,但还没有找到解决方案.我知道,如果你打开excel文件readwrite,它会将用户的名字存储在xlsx文件中.如何通过c#访问该特定信息?
编辑:我最终这样做:
public string GetExcelFileOwner(string path, NetOffice.ExcelApi.Enums.XlFileFormat ffmt) {
string tempmark = "~$";
if(ffmt==NetOffice.ExcelApi.Enums.XlFileFormat.xlExcel8) {
tempmark = "";
}
string uspath = Path.Combine(Path.GetDirectoryName(path), tempmark + Path.GetFileName(path));
if (!File.Exists(uspath)) return "";
var sharing = FileShare.ReadWrite | FileShare.Delete;
using (var fs = new FileStream(uspath, FileMode.Open, FileAccess.Read, sharing))
using (var br = new BinaryReader(fs, Encoding.Default)) {
if(ffmt==NetOffice.ExcelApi.Enums.XlFileFormat.xlExcel8) {
byte[] ByteBuffer = new byte[500];
br.BaseStream.Seek(150, SeekOrigin.Begin);
br.Read(ByteBuffer, 0, 500);
return matchRegex(System.Text.Encoding.UTF8.GetString(ByteBuffer), @"(?=\w\w\w)([\w, ]+)").Trim();
}
else …Run Code Online (Sandbox Code Playgroud)