我一直在使用一个简单的C#控制台
我能够按预期打开并修改输入范围,但我无法设法“刷新”(或重新计算,如果您愿意)工作簿,以便公式返回一个带有新输入的结果。
以下示例应该让我的问题非常明确。
所以我的问题是;如何从C#中刷新一个excel应用程序,以便刷新包含公式的单元格?
提前致谢,
艾库特
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel; // reference to excel 12 object library!
namespace deneme
{
class Program
{
static void Main(string[] args)
{
/*
I have a complex formula which was very easy to develop on EXCEL with excel built-in formulas
but would have been too complex if I tried to replicate this formula via C# codes.
So, I designed this formula on Excel, and designed the C# code to go and get this formula from excel when needed.
*/
// Note 2: his example used the 'Microsoft Excel 15.0 Object Library' but may be compatible with earlier versions of Interop and other libraries.
string path = "C:\\FNN\\XLA\\fnnComTemplate.xlsx";
Excel.Application xlApp = new Excel.Application();
Excel.Workbook wb = xlApp.Workbooks.Open(path);
Excel.Worksheet wSheet = wb.Worksheets[1];
string HisseKod = "GARAN";
string bilancoDonem = "2014/09K";
wSheet.Cells[5, 6].Value = HisseKod;
wSheet.Cells[5, 7].Value = bilancoDonem;
xlApp.Calculate();
result = wSheet.Cells[5, 11].Value.ToString(); // <<---- THIS IS WHERE THE PROBLEM IS! WHATEVER THE CODE RETURN IS NOT THE REFRESHED VALUE!!
Console.WriteLine( HisseKod + " - " + bilancoDonem + ": " + result);
wb.Close(true, "fnnComTemplate.xlsx");
xlApp.Quit();
Console.ReadLine();
}
}
答案 0 :(得分:0)
汉斯。感谢您的评论。问题是我打开并聚集的excel文件有某些使用其他插件的公式。并且,我意识到我需要打开这些插件(主要是xla&#39; s)和主excel文件以返回正确的结果。所以,
是的,这应该是我要检查的第一件事,但是,案件已经解决了。
最佳