Excel ListObject数据绑定没有响应

时间:2019-02-05 00:08:25

标签: vsto office-interop excel-addins

我在这里感到奇怪。我一直在Excel中使用listobjects并绑定到数据表,如下面的代码所示。 这类似于代码here

它在新工作簿和较小尺寸的工作簿上都可以正常工作。但是其中一本33 MB的工作簿挂在调用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;

}

0 个答案:

没有答案