使用 Excel 互操作扩展公式范围
我正在使用 Excel Interop 库来获取工作表并替换工作表中的值。我的工作表结构如下:
AE 列包含计算 FL 列中数据的公式 当我更新工作表时,我清除 FL 中的内容并将所有公式保留在 AE 中。然后我用新数据填充 FL 列。
我需要 AE 列(任何行)中的公式扩展到我输入的新数据的全部范围。
例如,如果我将数据从 8 行扩展到 20 行,=sum(G1,G8)
应变为 =sum(G1,G20)
。 有没有办法获得工作表中所有现有的公式?如何扩大公式的范围?
I'm using the Excel Interop libraries to take a sheet and replace values in the sheet. My sheet structure is as follows:
Columns A-E contain formula which work off the data in Columns F-L
When I update the sheet, I clear the contents from F-L and leave any formula in A-E. I then fill columns F-L with new data.
I need my formula in Columns A-E(any rows) to extend to the full range of the new data I entered.
For example =sum(G1,G8)
should become =sum(G1,G20)
if I extend the data to 20 rows from 8.
Is there a way to get to all present formula in the sheet? How do I extend the range of the formula?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在向单元格输入数据时,跟踪每个新单元格,然后将公式应用于该范围:
While inputting data to the cells, keep track of each new cell and then apply the formula to that range:
有关操作 Excel 对象模式的信息,我建议首先查找 VBA 解决方案,然后将其转换为 C#(这很简单)。网络上有更多的 VBA 资源,有时您甚至可以通过录制 VBA 宏来帮助自己。
根据您的公式所在的具体位置,您也许可以通过插入行来实现您想要的效果。
例如,如果您在 A3 中有一个引用范围 G1:G8 的公式,并且您现在想要插入 20 行数据而不是 8 行,则可以在写入之前将第 8 行向下移动 20-8 = 12 次,如下所示数据。
当您执行此操作时,A3 中的公式将自动修改以包含插入的行。
请注意,通过 Interop 执行此操作对于大量行来说不会特别有效(对于插入的每行,从应用程序到 Excel 进程的往返)。提高性能的一种方法是一次插入多行。
For information on manipulating the Excel object mode, I'd recommend looking first for a VBA solution, then converting this to C# (which is trivial). There are far more VBA resources available on the web, and you can sometimes even help yourself by recording a VBA macro.
Depending on exactly where your formulae are, you may be able to achieve what you want by inserting rows.
E.g. if you have a formula in, say, A3 that references the range G1:G8, and you now want to insert twenty rows of data instead of 8, you can move row 8 down 20-8 = 12 times as follows before writing your data.
When you do this, the formula in A3 will be modified automatically to include the inserted rows.
Note that doing this via Interop will not be particularly efficient for a large number of rows (a round trip from your app to the Excel process for each row inserted). One way to improve performance is to insert multiple rows at a time.
我也遇到过类似的问题,不过是用 IF 公式。将其设置为 Formula 属性不起作用。但是,如果设置为 FormulaLocal,则同样有效...
感谢 Microsoft :)
顺便说一句:Formula 和 FormulaLocal 仅适用于 A1 参考样式。对于 R1C1,您应该使用 FormulaR1C1 和 FormulaR1C1Local。
I had a similar problem, but with IF formula. Setting it to Formula property didn't work. However the same works if set to FormulaLocal...
thanks Microsoft :)
btw: Formula and FormulaLocal will work only for A1 reference style. For R1C1 you should use FormulaR1C1 and FormulaR1C1Local.
您可能必须将对象解析为 Range...
You may have to parse the object to Range...