Я не особенно доволен этим решением, но я нашел обходной путь для избежания модальностей входа в MS (и многих других модалов) до открытия документа Excel.
Перед открытием документа Excelв MS Excel Interop я провожу «zip-тест», где я заархивирую файл во временный каталог, попытаюсь извлечь его, а затем немедленно удалить этот каталог.
private void ZipTest(string xlsxFilePath, string tempDirectoryPath)
{
try
{
Directory.CreateDirectory(tempDirectoryPath);
string zipPath = ZipFile(xlsxFilePath, tempDirectoryPath);
using (ZipArchive zip = System.IO.Compression.ZipFile.Open(zipPath, ZipArchiveMode.Update))
{
zip.ExtractToDirectory(tempDirectoryPath);
}
Directory.Delete(tempDirectoryPath, true);
}
catch (Exception ex)
{
if (Directory.Exists(tempDirectoryPath))
{
Directory.Delete(tempDirectoryPath, true);
}
throw ex; // Do whatever you want
}
}
private string ZipFile(string filePath, string destinationDirectory)
{
string fileName = Path.GetFileName(filePath);
string zipFileName = Path.ChangeExtension(fileName, ".zip");
string destinationPath = $"{destinationDirectory}\\{zipFileName}";
File.Copy(filePath, destinationPath, false);
return destinationPath;
}
* 1005ошибка, если рабочая книга имеет ограниченный доступ, защищена паролем или не может быть записана иным образом.Это предотвращает модальный вход MS, поскольку документ Excel никогда не открывается.
Обратите внимание, что это работает только для файлов xlsx.Любые файлы xls должны быть сначала преобразованы в xlsx.