【发布时间】:2014-09-15 08:04:56
【问题描述】:
我正在尝试创建一个脚本来刷新工作表中的所有数据,然后刷新数据透视表(因为数据透视表中的数据通常在数据库中的数据之前刷新,默认情况下结果不正确)。
为此,我使用此脚本(因为我每天 9 点自动启动此脚本)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Get fully qualified path for xlsx file
var spreadsheetLocation = "C:\\update_rapport\\Salgsrapport.xlsx";
var exApp = new Microsoft.Office.Interop.Excel.Application();
var exWbk = exApp.Workbooks.Open(spreadsheetLocation);
//var exWks = (Microsoft.Office.Interop.Excel.Worksheet)exWbk.Sheets["responses(7)"];
exWbk.RefreshAll();
exApp.DisplayAlerts = false;
// This part is not correct. Need to find all pivot tables and update them
Object PivotTables(
Object Index
);
string save_file_name = "C:\\temp\\updated\\Salgsrapport.xlsx";
exWbk.SaveAs(save_file_name);
exWbk.Close(true);
exApp.Quit();
}
}
}
我发现循环遍历所有数据透视表的最接近的事情是: http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.pivottables.aspx
但这不是一个完整的示例,而且我之前从未真正编写过 C#,所以我有点迷路了。这个问题可能有一个更简单的解决方案,任何提示都表示赞赏。
【问题讨论】:
标签: c# excel pivot-table