从C#中拖出excel中的公式

时间:2011-08-08 13:00:15

标签: c# excel excel-formula

我觉得这个问题很简单,但我找不到答案。

我想根据col“A”和“B”中的信息在“C”列中应用一列公式。我希望公式在编写公式时能够像excel一样工作,然后拖动,一直创建行相关公式。

以下方法有效,但速度非常慢,因为它会分别编写每个公式。我确信那里有一种更有效的方法。

由于

        using Excel = Microsoft.Office.Interop.Excel;

        ...


        object oOpt = System.Reflection.Missing.Value; //for optional arguments
        Excel.Application oXL = null;
        Excel.Workbook oWB = null;
        Excel.Worksheet oSheet = null;
        Excel.Range oRng = null;

        try
        {
            //Start Excel and get Application object.
            oXL = new Excel.Application();
            oXL.Visible = true;

            //Get a new workbook.
            oWB = (Excel.Workbook)(oXL.Workbooks.Add(Missing.Value));
            oSheet = (Excel.Worksheet)oWB.ActiveSheet;

            ...
            //Set numberOfRows
            //Load information to column A and B
            ...


            //Write the column of formulas
            for (int r = 2; r < numberOfRows + 2; r++)
            {
                oRng = oSheet.get_Range("C" + r, "C" + r);
                oRng.Formula = "= IF(AND(A" + r + "<> 0,B" + r + "<>2),\"YES\",\"NO\")";
            }


        }
        catch (Exception theException)
        {
            String errorMessage;
            errorMessage = "Error: ";
            errorMessage = String.Concat(errorMessage, theException.Message);
            errorMessage = String.Concat(errorMessage, " Line: ");
            errorMessage = String.Concat(errorMessage, theException.Source);

            MessageBox.Show(errorMessage, "Error");
        }
        finally
        {

            // Cleanup
            GC.Collect();
            GC.WaitForPendingFinalizers();

            Marshal.FinalReleaseComObject(oRng);
            Marshal.FinalReleaseComObject(oSheet);

            oWB.Close(Type.Missing, Type.Missing, Type.Missing);
            Marshal.FinalReleaseComObject(oWB);

            oXL.Quit();
            Marshal.FinalReleaseComObject(oXL);
       }

2 个答案:

答案 0 :(得分:3)

使用R1C1公式,并替换:

    for (int r = 2; r < numberOfRows + 2; r++)
    {
        oRng = oSheet.get_Range("C" + r, "C" + r);
        oRng.Formula = "= IF(AND(A" + r + "<> 0,B" + r + "<>2),\"YES\",\"NO\")";
    }

    oRng = oSheet.get_Range("C2").get_Resize(100, 1);
    oRng.FormulaR1C1 = "=IF(AND(RC[-2]<> 0,RC[-1]<>2),\"YES\",\"NO\")";

答案 1 :(得分:0)

我正在使用Microsoft.Office.Interop.Excel库从C#中将excel中的拖动公式的代码发布

class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();
        p.Excel();
    }

    public void Excel()
    {
        Application xlApp = new Application();
        Workbook xlWorkBook;
        Worksheet xlWorkSheet;
        object misValue = Missing.Value;

        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);

        for (int r = 1; r < 5; r++) //r stands for ExcelRow and c for ExcelColumn
        {
            // Its a my sample example: Excel row and column start positions for writing Row=1 and Col=1
            for (int c = 1; c < 3; c++)
            {
                if (c == 2)
                {
                    if (r == 1)
                    {
                        xlWorkSheet.Cells[r, c].Formula = "=SUM(A1+200)";
                    }
                    continue;
                }
                xlWorkSheet.Cells[r, c] = r;
            }
        }
        Range rng = xlWorkSheet.get_Range("B1");

        //  This is the main code we can Drag our excel sheet formulas in range
        rng.AutoFill(xlWorkSheet.get_Range("B1", "B4"), XlAutoFillType.xlLinearTrend);

        xlWorkBook.Worksheets[1].Name = "MySheetData";//Renaming the Sheet1 to MySheet

        xlWorkBook.SaveAs(@"E:\test.xlsx");

        xlWorkBook.Close();

        Marshal.ReleaseComObject(xlWorkSheet);
        Marshal.ReleaseComObject(xlWorkBook);
        Marshal.ReleaseComObject(xlApp);

    }
}