How to Copy Formula from one cell to another

Hi

I have a formula in cell C1=B1-A1 i need to copy this formula into the cell C2 as C2=B2-A2 how to copy this.Is this possible using Aspose.cells

Hi,

Thanks for your posting and considering Aspose.Cells for .NET.

Please look into the following code, it copies your desired formula in first 100 rows of column C.

I have also attached the output.xls file for your reference generated by this code.

Please also see the screenshot.

C#


//Create a workbook

Workbook workbook = new Workbook();


//Acess the first worksheet

Worksheet worksheet = workbook.Worksheets[0];


//Template formula

string templateFormula = “B{r}-A{r}”;



//Copy your desired formula in first 100 cells of column C

for (int i = 0; i < 100; i++)

{

string formula = templateFormula.Replace("{r}", (i + 1 + “”));


worksheet.Cells[“C” + (i + 1 + “”)].Formula = formula;

}


//Save your workbook in xlsx format

workbook.Save(@“F:\Shak-Data-RW\Downloads\output.xlsx”);

Screenshot: