//传入Excel公式,获取公式计算结果
private string GetValue(string formula)
{
string result = "";
try
{
Object missing = System.Reflection.Missing.Value;
Excel.Application excelApp = new Excel.Application();
Excel.Workbooks wbooks = (Excel.Workbooks)excelApp.Workbooks;
string path1 = AppDomain.CurrentDomain.BaseDirectory + ("UpFiles\") + "635236681202245225.xls";
Excel.Workbook wbook = wbooks.Open(path1, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
excelApp.Visible = false;
excelApp.DisplayAlerts = false;
//s写入sheet1
Excel.Worksheet wsheet = (Excel.Worksheet)wbook.Worksheets.get_Item(1);
Excel.Range range = (Excel.Range)wsheet.Cells[1, 1];
range.Formula = formula;
//range = (Excel.Range)wsheet.Cells[1, 2];
//range.Value2 = 5;
//range = (Excel.Range)wsheet.Cells[1, 3];
//range.Value2 = 123;
wbook.Save();
range = (Excel.Range)wsheet.Cells[1, 1];
result = range.Value2.ToString();
wbook.Close();
wbooks.Close();
}
catch
{ }
finally
{
Process[] myProcesses = Process.GetProcessesByName("Excel");
foreach (Process myProcess in myProcesses)
{ myProcess.Kill(); }
}
return result;
}