2014-01-15 23 views
0

我有一個客戶端應用程序(excel addIn)從WCF服務器檢索數據。從自定義對象寫入excel工作表

這些類:

public class myWorksheet 
    { 
     public int Id { get; set; } 
     public string WorksheetName { get; set; } 
     public List<myData> MyDataChildren { get; set; } 
     public List<string> Dependencies { get; set; } 
    } 

    public class myData 
    { 
     public int Id { get; set; } 
     public int WorksheetId { get; set; } 
     public int RowId { get; set; } 
     public string ColumnId { get; set; } 
     public string Value { get; set; } 
     public string Formula { get; set; } 
     public string Comment { get; set; } 
    } 

依賴 - 工作表名稱列表,當前工作表中有一個參考(公式)

我檢索WCF(實際上IEnumerable的)的列表和在客戶端上有這些數據。 問題是,當我將數據寫入excel時,需要花費很長時間。

我在做什麼是:

  1. 工作簿中創建的工作表的空
  2. 激活一個工作表(在一個用戶currenty看)
  3. 在 「Application_SheetActivate」 事件

    • 首先將所有數據加載到依存關係中的工作表中
    • 加載數據成片用戶在看

像這樣:

public List<myWorksheet> WorksheetList { get; set; } 

    private void Rebind(myWorksheet currWorksheet, out Excel.Workbook workbook) 
    { 

     foreach (string connectedWorksheet in currWorksheet.Dependencies) 
     { 
     myWorksheet ws = DataHelper.FindWorksheetFromList(connectedWorksheet, this.WorksheetList); 
     Excel.Worksheet sheet = DataHelper.FindWorksheetFromWorkbook(connectedWorksheet,workbook); 

     this.LoadData(sheet,ws); 
     } 


     Excel.Worksheet sheetMain = DataHelper.FindWorksheetFromWorkbook(currWorksheet.WorksheetName,workbook); 
     this.LoadData(sheetMain,currWorksheet); 
    } 

    private void LoadData(Excel.Worksheet worksheet, myWorksheet worksheetEnt) 
    { 
     foreach (myData cell in worksheetEnt.MyDataChildren) 
     { 
      Excel.Range excelCell = worksheet.Range(cell.ColumnId.ToString() + cell.RowId.ToString()); 
      excelCell.Value2 = cell.Value; 
      excelCell.Formula = cell.Formula; 
      excelCell.AddComment(cell.Comment); 
     } 
    } 

的問題是:

  • 有在名單
  • 大約400工作表中的每個工作表中有大約2000個單元格(myData)。
  • 某些工作表有大約200個依賴關係。 (並且加載該依賴關係需要15分鐘)。

  • 這次迭代我做錯了什麼?有沒有更好/更快的方式,我不知道?

回答

0

管理解決這個問題。

使用數組

  • 設置Application.ScreenUpdating爲False
  • 試圖

    • 佈線的數據和幫助一點。但真正的伎倆是

      • Application.Calculation = Excel.XlCalculation.xlCalculationManual

      所以我的代碼現在看起來像這樣:

      private void Rebind(myWorksheet currWorksheet, out Excel.Workbook workbook) 
      { 
      
          currWorksheet.Application.Calculation = Excel.XlCalculation.xlCalculationManual 
          currWorksheet.Application.ScreenUpdating=False 
      
          foreach (string connectedWorksheet in currWorksheet.Dependencies) 
          { 
          myWorksheet ws = DataHelper.FindWorksheetFromList(connectedWorksheet, this.WorksheetList); 
          Excel.Worksheet sheet = DataHelper.FindWorksheetFromWorkbook(connectedWorksheet,workbook); 
      
          this.LoadData(sheet,ws); 
          } 
      
          Excel.Worksheet sheetMain = DataHelper.FindWorksheetFromWorkbook(currWorksheet.WorksheetName,workbook); 
          this.LoadData(sheetMain,currWorksheet); 
      
          currWorksheet.Application.Calculation = Excel.XlCalculation.xlCalculationAutomatic 
          currWorksheet.Application.ScreenUpdating=True 
      }