从自定义对象写入Excel工作表

时间:2014-01-15 15:08:35

标签: c# excel loading cell worksheet

我有一个从WCF服务器检索数据的客户端应用程序(excel addIn)。

这些是类:

    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检索List(实际上是IEnumerable)并在客户端上拥有该数据。 问题在于,当我将数据写入excel时,它需要很长时间。

我正在做的是:

  1. 在工作簿中创建空工作表
  2. 激活一个工作表(一个用户当前正在查看)
  3. 在“Application_SheetActivate”事件

    • 首先将所有数据加载到Dependencies中的工作表
    • 将数据加载到工作表用户正在查看
  4. 像这样:

        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分钟)。

    • 这次迭代是我做错了吗?是否有更好/更快的方式,我不知道?

1 个答案:

答案 0 :(得分: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
 }