How to copy formula from one column in first row to same column in next row

Hi

I have Excel Template and am filling the each row in excel by using PUTVALUE method.I have a formula in one of the column in first row.If i have more than one row from database, the formula in the column in the first row should copy to next rows.Is this possible

Hi,


I think you may try to use Shared Formula feature provided by Aspose.Cells for your requirement. See the topic for your reference:
http://docs.aspose.com/display/cellsnet/Setting+Shared+Formula


Thank you.

hi

Thanks for your reply.But this is like assigning formula in the code,but reqiurement is like i have a formula in the cell in my template that formula i need to take a and assign that formula to the next rows

Hi,

Please use the following code, it will copy cell a1 formula to cell b1.

e.g


Cell al=…;

Cell b1=…;

b1.Formula = a1.Formula;



hi

In this example your coping A1 formula to B1 but i need to copy A1 formula to A2 like
A1=B1+C1 need to copy to A2 as A2=B2+C2

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: