C# 如何获取值而不是公式

C# 如何获取值而不是公式,c#,excel,C#,Excel,我试图从工作表中获得最小值,但当我生成最小值并将其放入单元格中,然后提取该值时,我得到的是整个公式,而不是单元格中的双精度值。。。。我做错了什么?下面是我的生成图方法 还有一件事,我还想在保存图形后删除它,我已经尝试过了。delete(),但这只是抛出了一个错误,我该怎么做呢 private void GenerateGraph(Worksheet worksheet, int lastRow, int lastColumn, string filename) {

我试图从工作表中获得最小值,但当我生成最小值并将其放入单元格中,然后提取该值时,我得到的是整个公式,而不是单元格中的双精度值。。。。我做错了什么?下面是我的生成图方法

还有一件事,我还想在保存图形后删除它,我已经尝试过了。delete(),但这只是抛出了一个错误,我该怎么做呢

private void GenerateGraph(Worksheet worksheet, int lastRow, int lastColumn, string filename)
        {
            string topLeft = ToCell(0, 0);
            string bottomRight = ToCell(lastRow - 1, lastColumn - 1);



        worksheet.get_Range(ToCell(0, 0), missing).Formula = "Max(B2:" + bottomRight + ")";
        worksheet.get_Range(ToCell(0, 0), missing).FormulaHidden = true;
        worksheet.get_Range(ToCell(0, 0), missing).Calculate();
        Range range = (Range)worksheet.Cells[1,1];

        //
        //here is where my problem is, small is being given the formula from above
        //

        string small = (string)range.Value2;
        double min = Convert.ToDouble(small);
        worksheet.get_Range(ToCell(0,0),missing).Formula = "";

        //Generates the graph
        range = worksheet.get_Range(topLeft, bottomRight);
        ChartObjects Xlchart = (ChartObjects)worksheet.ChartObjects(missing);
        ChartObject chart = (ChartObject)Xlchart.Add(20, 160, 600, 500);
        Excel.Chart myChart = chart.Chart;
        myChart.SetSourceData(range, missing);

        //sets the y axis
        Axis axis = (Axis)myChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
        axis.MinimumScaleIsAuto = true;
        axis.MaximumScaleIsAuto = true;
        axis.HasTitle = true;
        axis.AxisTitle.Text = "Measure (m)";
        axis.CrossesAt = (int)(min-1);

        //sets the x axis
        Axis xAxis = (Axis)myChart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary);
        xAxis.HasTitle = true;
        xAxis.AxisTitle.Text = "Position (m)";

        //makes the graph a line graph
        myChart.ChartType = XlChartType.xlXYScatterLinesNoMarkers;

        //titles the graph
        myChart.HasTitle = true;
        myChart.ChartTitle.Text = "Profiles";

        //saves the graph
        myChart.Export(filename, "JPG", missing);

        //
        //here is where I would like to delete the graph
        //
    }
你需要:

Formula = "=Max(B2:" + bottomRight + ")"
公式中缺少等号。

您需要:

Formula = "=Max(B2:" + bottomRight + ")"

你在公式中遗漏了等号。

我是,这解决了我问题的前半部分,现在我如何从实际的excel文件中删除图形?@Bob一问两个问题就是这个问题。我知道第一个问题的答案,但不知道第二个问题的答案。你最好编辑这个问题来删除删除部分,然后问一个新的问题就可以了!我是,这解决了我问题的前半部分,现在我如何从实际的excel文件中删除图表?@Bob这就是一问两个问题的问题。我知道第一个问题的答案,但不知道第二个问题的答案。你最好编辑这个问题来删除删除部分,然后问一个新的问题就可以了!