【问题标题】:Writing to excel worksheet from custom object从自定义对象写入 excel 工作表
【发布时间】:2014-01-15 15:08:35
【问题描述】:

我有一个从 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. 激活一个工作表(一个用户当前正在查看)
  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);
        }
    }

问题是:

  • List 中有大约 400 个工作表
  • 每个工作表都有大约 2000 个单元格 (myData)。
  • 一些工作表有大约 200 个依赖项。 (并且加载该依赖项需要 15 分钟)。

  • 这个迭代是我做错了吗?有没有我不知道的更好/更快的方法?

【问题讨论】:

    标签: c# excel loading cell worksheet


    【解决方案1】:

    设法解决了问题。

    试过了

    • 使用数组连接数据
    • 将 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
     }
    

    【讨论】:

      猜你喜欢
      • 2018-08-30
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      • 2019-06-26
      相关资源
      最近更新 更多