C# excel中的链接不会自动更新# 到目前为止我所尝试的
我正在开发基于.NETFramework 4.0的基于窗口的应用程序。该应用程序执行两项任务-C# excel中的链接不会自动更新# 到目前为止我所尝试的,c#,.net,excel,C#,.net,Excel,我正在开发基于.NETFramework 4.0的基于窗口的应用程序。该应用程序执行两项任务- 打开excel文件,以便使用Microsoft.Office.Interop.excel从DDE服务器(MCX)获取更新 使用RSS总线Ado.Net提供程序将excel文件中的数据读入DataGridView 正常情景 我已经将Excel文件共享,以便可以同时编辑和读取 现在,当我运行DDE服务器时,它会将一些数据发送到Excel。Excel文件显示更新的数据,中断时间为2秒(默认值)。那很好 编程
xlApp = new Application { AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityLow };
_xlApp.CalculateFull();
_xlBook = _xlApp.Workbooks.Open(ExcelFilePath, 0, false, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false);
//Add an event handler for the WorkbookBeforeClose Event of the Application object.
_eventBeforeBookClose = BeforeBookClose;
_xlApp.WorkbookBeforeClose += _eventBeforeBookClose;
//Add an event handler for the Change event of both worksheet objects.
_eventCellsChange = CellsChange;
//Add an event handler for the Calculate event of both worksheet objects.
_eventCellsCalculate = CellsCalculated;
_xlSheet1 = (Worksheet)_xlBook.Worksheets.Item[1];_xlSheet1.Change += _eventCellsChange;
((DocEvents_Event)_xlSheet1).Calculate += _eventCellsCalculate;
//Make Excel visible and give the user control.
_xlApp.Visible = true;
_xlApp.UserControl = true;
问题
C#中是否有任何设置允许自动更新excel文件(从程序打开)中的链接?要自动更新excel中的链接,我添加了以下需要由计时器滴答事件调用的函数 代码片段-
public static void UpdateLinkValues()
{
var oleLinks = (object)_xlBook.LinkSources(XlLink.xlOLELinks);
var oleLinkArray = (Array)oleLinks;
for (var i = 1; i <= oleLinkArray.Length; i++)
{
_xlBook.UpdateLink(oleLinkArray.GetValue(i),XlLinkType.xlLinkTypeOLELinks);
}
}
publicstaticvoidupdatelinkvalues()
{
var oleLinks=(对象)xlBook.LinkSources(XlLink.xlOLELinks);
var oleLinkArray=(数组)oleLinks;
对于(var i=1;i有一个更简单的解决方案,Workbooks Open方法为此提供了一个参数:
_xlBook = _xlApp.Workbooks.Open(ExcelFilePath, UpdateLinks: 3);
有关UpdateLink参数的确切说明,请参阅。值“3”将始终更新所有链接。是否保存Excel文件?@ThorstenDittmar否!我没有保存Excel文件。我测试了保存,但由于文件已经存在,所以出现异常。我们只是打开它。