So, I have this method which exports some data into an Excel file:
` public void exportToExcel(string filePath, string sheetName)
{
Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
Workbook workBook = excelApplication.Workbooks.Open(filePath, ReadOnly : false);
Worksheet workSheet = (Worksheet)workBook.Worksheets[sheetName];
int x = 1;
foreach (string plotName in plotNames)
{
x += 2;
foreach (string property in plotProperties[plotName])
{
x += 1;
foreach (Area area in AreasOrganizer[plotName][property])
{
try
{
Range cellRangeString = workSheet.Range[$"A{x}", $"B{x}"];
Range cellRangeDouble = workSheet.Range[$"C{x}", $"X{x}"];
/* configuring data to be written */
string[] areaStringData = new[] { areaNumber, areaName };
double[] areasDoubleData = new[] { areaArea, areaSubjected, ACGA, ACOR, ACLE, ACLO, ACHE, ACRO, ACSP, ACST, ACZO, ACCO, C1C2, areaCommonPercent, areaCommonArea, areaTotalArea, areaPermitPercent, areaRLPPercentage, areaRLP };
cellRangeString.set_Value(XlRangeValueDataType.xlRangeValueDefault, areaStringData);
cellRangeDouble.set_Value(XlRangeValueDataType.xlRangeValueDefault, areasDoubleData);
}
catch
{
Range cellRangeString = workSheet.Range[$"A{x}", $"B{x}"];
string[] cellsStrings = new[] { "X", "Y" };
cellRangeString.set_Value(XlRangeValueDataType.xlRangeValueDefault, cellsStrings);
}
x += 1;
}
}
}
workBook.Save();
workBook.Close();
}`
The method works just fine, but once I try to open the file manually afterwards, it asks to be opened in Safe Mode:
enter image description here
It’s not a big deal, but it’s just a bit annoying, so if anyone knows how to fix this, that would be awesome! It’s the first time I’m using Microsoft.Office.Interop.Excel. And I don’t think the problem comes from internal Excel configuration, as it only happens when I edit the file from my plug in and never when doing it manually 🙂 thanks in advance!
Danail Momchilov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.