使用 aspose.cells 动态设置求和公式中的列

发布于 2024-12-09 14:06:10 字数 566 浏览 0 评论 0 原文

我正在使用 aspose excel 导出。我正在.net中设置公式。

我想要第三个单元格中两个单元格值的总和。像

excelbook.Worksheets[0].Cells["A2"].R1C1Formula = "=SUM(R1C1,R1C2)";

但我想动态设置该列。与上面的公式一样,C1、C2 列是固定的。

现在我想动态选择列名称,我想构建如下所示的公式

excelbook.Worksheets[0].Cells["A2"].R1C1Formula = "=SUM(R1C(单元格 B1 的值),R1C(单元格的值) B2))”;

因此,如果我在 B1 单元格中输入 1,在 B2 单元格中输入 3,那么它应该对 A1 和 C1 求和。我可以在 B1 和 B2 单元格中输入任何值,Excel 应该根据输入的值计算总和。

请建议如何构建此类公式。

直接在Excel中,我们可以通过这种方式做到:-

=INDIRECT(B1&1)+INDIRECT(B2&1)

所以请建议我可以在r1c1公式中使用INDIRECT或建议其他方式。

I am use aspose excel export. I am setting formulla in .net.

I want sum of two cells value in third cell. like

excelbook.Worksheets[0].Cells["A2"].R1C1Formula = "=SUM(R1C1,R1C2)";

but i want to set the column dynamically. like in above formulla column C1,C2 are fixed.

now i want pick the column name dynamically , i want to build my formulla like below

excelbook.Worksheets[0].Cells["A2"].R1C1Formula = "=SUM(R1C(Value of Cell B1),R1C(Value of Cell B2))";

so if i enter 1 in B1 cell and 3 in B2 cell then it should sum A1 and C1.i can enter any value in B1 and B2 cell and excel should calculate the sum based upon entered values.

Please suggest how to build this type of formulla.

directly in excel we can do by this way:-

=INDIRECT(B1&1)+INDIRECT(B2&1)

so please suggest can i use INDIRECT in r1c1 formulla or suggest some other way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╄→承喏 2024-12-16 14:06:10

如果您想在 Excel 中添加 D1 和 E1,但您想根据特定单元格的值选择行;例如,在本例中,您可能使用 A1 和 A2 来放置要选择的行的值,然后您将使用如下公式:

=SUM(INDIRECT("D"&$A$1),INDIRECT(" E"&$A$2))

您可以使用 Aspose.Cells for .NET 也是如此。请参阅以下代码:

        //create XLS File
        //Instantiate a Workbook object that represents Excel file.
        //create a workbook
        Workbook CellsDocument = new Workbook();
        //Note when you create a new workbook, a default worksheet
        //"Sheet1" is added (by default) to the workbook.
        //Access the first worksheet "Sheet1" in the book.
        Worksheet sheet = CellsDocument.Worksheets[0];
        //Access the cells and put values
        Aspose.Cells.Cell cellA1 = sheet.Cells["A1"];
        cellA1.PutValue(1);

        Aspose.Cells.Cell cellA2 = sheet.Cells["A2"];
        cellA2.PutValue(1);

        Aspose.Cells.Cell cellD1 = sheet.Cells["D1"];
        cellD1.PutValue(4);

        Aspose.Cells.Cell cellE1 = sheet.Cells["E1"];
        cellE1.PutValue(5);

        Aspose.Cells.Cell cellE14 = sheet.Cells["E14"];
        cellE14.R1C1Formula = "=SUM(INDIRECT(\"D\"&$A$1),INDIRECT(\"E\"&$A$2))";


        //Save the Excel file.
        CellsDocument.Save("output.xls", Aspose.Cells.SaveFormat.Excel97To2003);

请在最后使用 最新版本的 Aspose.Cells for .NET。如果您认为这没有帮助或者我无法清楚地理解您的问题,请分享您进一步的想法。

披露:我在 Aspose 担任开发人员传播者。

If you want to add D1 and E1 in Excel but you want to select the row based on the value of the particular cell; for example, in this case, you might use A1 and A2 to place the value of the row to select, then you will use the formula like this:

=SUM(INDIRECT("D"&$A$1),INDIRECT("E"&$A$2))

You can assign the same formula using Aspose.Cells for .NET as well. Please see the following code:

        //create XLS File
        //Instantiate a Workbook object that represents Excel file.
        //create a workbook
        Workbook CellsDocument = new Workbook();
        //Note when you create a new workbook, a default worksheet
        //"Sheet1" is added (by default) to the workbook.
        //Access the first worksheet "Sheet1" in the book.
        Worksheet sheet = CellsDocument.Worksheets[0];
        //Access the cells and put values
        Aspose.Cells.Cell cellA1 = sheet.Cells["A1"];
        cellA1.PutValue(1);

        Aspose.Cells.Cell cellA2 = sheet.Cells["A2"];
        cellA2.PutValue(1);

        Aspose.Cells.Cell cellD1 = sheet.Cells["D1"];
        cellD1.PutValue(4);

        Aspose.Cells.Cell cellE1 = sheet.Cells["E1"];
        cellE1.PutValue(5);

        Aspose.Cells.Cell cellE14 = sheet.Cells["E14"];
        cellE14.R1C1Formula = "=SUM(INDIRECT(\"D\"&$A$1),INDIRECT(\"E\"&$A$2))";


        //Save the Excel file.
        CellsDocument.Save("output.xls", Aspose.Cells.SaveFormat.Excel97To2003);

Please try it at your end using the latest version of the Aspose.Cells for .NET. If you think it doesn't help or I'm unable to understand your question clearly then please share your further thoughts.

Disclosure: I work as a developer evangelist at Aspose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文