如何在两个单独的 Excel 应用程序中将一个 Excel 工作表的更改复制到另一个 Excel 工作表?
这一切都在 C# .NET Excel Interop Automation for Office 2007 中进行。
假设您创建两个 Excel 应用程序并为每个应用程序打开同一个工作簿:
app = new Excel.ApplicationClass();
app2 = new Excel.ApplicationClass();
string fileLocation = "myBook.xslx";
workbook = app.Workbooks.Open(fileLocation,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workbook2 = app2.Workbooks.Open(fileLocation,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
现在,我想将 workbook2 中发生的任何更改复制到工作簿中。我发现我可以连接 SheetChanged 事件来捕获单元格更改:
app.SheetChange += new Microsoft.Office.Interop.Excel.AppEvents_SheetChangeEventHandler(app_SheetChange);
void app_SheetChange(object Sh, Microsoft.Office.Interop.Excel.Range Target)
{
Excel.Worksheet sheetReadOnly = (Excel.Worksheet)Sh;
string changedRange = Target.get_Address(missing, missing,
Excel.XlReferenceStyle.xlA1, missing, missing);
Console.WriteLine("The value of " + sheetReadOnly.Name + ":" +
changedRange + " was changed to = " + Target.Value2);
Excel.Worksheet sheet = workbook.Worksheets[sheetReadOnly.Index] as Excel.Worksheet;
Excel.Range range = sheet.get_Range(changedRange, missing);
range.Value2 = Target.Value2;
}
如何捕获计算更改?我可以挂载计算事件,但唯一传递的是工作表,而不是更新的单元格。我尝试强制 app.Calculate() 或 app.CalculateFullRebuild() 但其他应用程序中没有任何更新。当公式更改时,更改事件不会被触发(即滑块控件导致 SheetCalculate 事件而不是 SheetChange 事件)
有没有办法查看更新了哪些公式?或者是否有更简单的方法以编程方式实时同步两个工作簿?
This is all in C# .NET Excel Interop Automation for Office 2007.
Say you create two excel apps and open the same workbook for each application:
app = new Excel.ApplicationClass();
app2 = new Excel.ApplicationClass();
string fileLocation = "myBook.xslx";
workbook = app.Workbooks.Open(fileLocation,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workbook2 = app2.Workbooks.Open(fileLocation,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Now, I want to replicate any changes that occur in workbook2, into workbook. I figured out I can hook up the SheetChanged event to capture cell changes:
app.SheetChange += new Microsoft.Office.Interop.Excel.AppEvents_SheetChangeEventHandler(app_SheetChange);
void app_SheetChange(object Sh, Microsoft.Office.Interop.Excel.Range Target)
{
Excel.Worksheet sheetReadOnly = (Excel.Worksheet)Sh;
string changedRange = Target.get_Address(missing, missing,
Excel.XlReferenceStyle.xlA1, missing, missing);
Console.WriteLine("The value of " + sheetReadOnly.Name + ":" +
changedRange + " was changed to = " + Target.Value2);
Excel.Worksheet sheet = workbook.Worksheets[sheetReadOnly.Index] as Excel.Worksheet;
Excel.Range range = sheet.get_Range(changedRange, missing);
range.Value2 = Target.Value2;
}
How do you capture calculate changes? I can hook onto the calculate event but the only thing that is passed is the sheet, not the cells that were updated. I tried forcing an app.Calculate() or app.CalculateFullRebuild() but nothing updates in the other application. The change event does not get fired when formulas change (i.e. a slider control causes a SheetCalculate event and not a SheetChange event)
Is there a way to see what formulas were updated? Or is there an easier way to sync two workbooks programmatically in real time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为没有任何直接事件可以告诉您正在计算哪些单元格,因为事件传播默认为 false,否则计算函数将陷入无限循环。
另请注意,工作表更改会触发自动计算。计算发生在整个工作表上,而不仅仅是更改,以确保公式地址中有任何修改。希望这有帮助。
I don't think there is any direct event which will tell you which cells are being calculated, as the events propogation is defaulted to false otherwise the calculate function will end into an infinite loop.
Also note the sheet change triggers the automatic calculate. And calculate happens on the whole sheet not just the changes, to make sure there is any modification in the formula addresses. Hope this helps.
如果两个用户编辑 Excel 文件中的相同单元格,保存工作簿的第二个用户会看到一个弹出问题。这通知用户自上次保存以来她已对另一个用户更改的单元格进行了更改。她可以选择保留自己的更改,从而删除其他用户的更改,或删除她最近的更改。这是共享 Excel 电子表格时的常见默认做法。但是,可以在“共享工作簿”窗口的“高级”选项卡中关闭此行为。
阅读更多内容:如何在一个 Excel 电子表格上控制多个用户 | eHow.com http://www.ehow.com/how_5913825_control -users-one-excel-spreadsheet.html#ixzz0xcXAZvP1
In the event that two users edit the same cells in the Excel file, the second user who saves the workbook is presented with a pop-up question. This notifies the user that she has made changes to a cell altered by another user since her last save. She is given the option of keeping her own changes, and thus deleting the other user's, or deleting her recent changes. This is the common default practice when sharing Excel spreadsheets. However, this behavior may be turned off in the "Advanced" tab of the "Share Workbook" window.
Read more: How to Control Multiple Users on One Excel Spreadsheet | eHow.com http://www.ehow.com/how_5913825_control-users-one-excel-spreadsheet.html#ixzz0xcXAZvP1