我有一個excel應用程序,它使用電源查詢從SharePoint獲取整合列表。我已經設置屬性來刷新Excel的應用程序,刷新數據集。當我手動打開excel時,它會刷新並獲取更新的數據。但是,我在我的winform應用程序中使用這個excel來獲取數據。 因此,爲了刷新數據,我試圖打開和關閉代碼的Excel。在Excel中刷新電源查詢
但是數據沒有被刷新,並且它鎖定了電源查詢插件。當我再次手動打開Excel表格時,運行代碼後,出現錯誤「查詢未運行,或數據庫表無法打開」。
功能區上不再顯示Power Query。 下面是我用它來打開和關閉Excel應用程序的代碼:
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
string workbookPath = @"C:\Test\Test.xlsx";
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A2");
excelWorkbook.Close(true, "Test.xlxs", null);
這是刷新數據的最佳方式是什麼?
不知道如何以編程方式執行此操作,但可以使用網關自動刷新工作簿@ http://blogs.msdn.com/b/powerbi/archive/2014/08/19/scheduled-data-refresh-update-new-data -sources.aspx。 –