Я поражен странной проблемой здесь.Я использую списочные объекты в Excel и привязываю их к данным, как показано в коде ниже.Это похоже на код здесь .
Отлично работает с новой книгой, а также книгой меньшего размера.Но одна из рабочих книг объемом 33 МБ висит в строке, где вызывается listObject.SetDataBinding (..).
private bool SaveDataTableToExcel()
{
var restoreCalculation = Globals.ThisAddIn.Application.Calculation;
var restoreDisplayAlerts = Globals.ThisAddIn.Application.DisplayAlerts;
var restoreEnableEvents = Globals.ThisAddIn.Application.EnableEvents;
var restoreScreenUpdating = Globals.ThisAddIn.Application.ScreenUpdating;
var restoreAutumationSecurity = Globals.ThisAddIn.Application.AutomationSecurity;
var restoreDisplayStatusBar = Globals.ThisAddIn.Application.DisplayStatusBar;
Globals.ThisAddIn.Application.Calculation = Microsoft.Office.Interop.Excel.XlCalculation.xlCalculationManual;
Globals.ThisAddIn.Application.DisplayAlerts = false;
Globals.ThisAddIn.Application.EnableEvents = false;
Globals.ThisAddIn.Application.ScreenUpdating = false;
Globals.ThisAddIn.Application.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;
Globals.ThisAddIn.Application.DisplayStatusBar = false;
try
{
var settingsDatatSet = new DataSet("DATASET");
//Retreives datatable
DataTable gridData = dataTable.PopulateTable();
settingsDatatSet.Tables.Add(gridData);
Microsoft.Office.Interop.Excel.Worksheet ws = Globals.ThisAddIn.Application.Worksheets["SETTINGS"];
Microsoft.Office.Tools.Excel.Worksheet vstoWorkSheet = Globals.Factory.GetVstoObject(ws);
Microsoft.Office.Tools.Excel.ListObject listObject = null;
//Check if list objects exists and use that
foreach (dynamic lo in vstoWorkSheet.ListObjects)
{
if (lo.Name == gridData.TableName)
{
listObject = Globals.Factory.GetVstoObject(lo);
}
}
//if not exists create new list objects
if (listObject == null)
{
listObject = vstoWorkSheet.Controls.AddListObject(ws.Range[cellRange], gridData.TableName);
}
listObject.AutoSetDataBoundColumnHeaders = true;
//ISSUE HERE: Works on a new workbook
//But hangs on a existing workbook of size 36MB
listObject.SetDataBinding(bindingSource, gridData.TableName);
}
catch (Exception ex)
{
logger.Error(ex);
return false;
}
finally
{
Globals.ThisAddIn.Application.Calculation = restoreCalculation;
Globals.ThisAddIn.Application.DisplayAlerts = restoreDisplayAlerts;
Globals.ThisAddIn.Application.EnableEvents = restoreEnableEvents;
Globals.ThisAddIn.Application.ScreenUpdating = restoreScreenUpdating;
Globals.ThisAddIn.Application.AutomationSecurity = restoreAutumationSecurity;
Globals.ThisAddIn.Application.DisplayStatusBar = restoreDisplayStatusBar;
}
return true;
}