首页 > 解决方案 > 更新已打开的 Excel 工作簿 C#

问题描述

我有经常从传感器和外部数据源更新的 excel 文档。

在 C# 中,我打开文件并处理数据。我需要做什么来更新我的工作表变量而不尝试再次重新打开文件。

void test(){
    Application xlApp = new Application();
    excelBook = xlApp.Workbooks.Open(filePath);
    Worksheet sheet = excelBook.Worksheets[sheetName];

    if (sheet != null){
       var r = sheet.UsedRange.Value2;

       while(true){
            string json = JsonConvert.SerializeObject(r, Formatting.Indented);

            //do work with the json data here
            //........

            //make sure we have latest updated sheet
            //these are not working.

            excelBook.RefreshAll();
            excelBook.AutoUpdateSaveChanges = true;
       }
    }
}

标签: c#exceloffice-interop

解决方案


推荐阅读