Samples: Forumula

   Minimize
  Formula Sample for C# / VB.NET


Download the sample here
 
[C#]
XlsDocumentBuilder builder = new XlsDocumentBuilder();
//create a workbook
IXlsWorkbook book = builder.Create(1);
//Add worksheet
IXlsWorksheet sheet = book.Worksheets[0];
//Some data 
int row = 2;
int col = 2;
sheet.Range[row++, col].Value2 = 3.5;   //cell B2
sheet.Range[row++, col].Value2 = 2;     //cell B3
sheet.Range[row++, col].Value2 = 5.34;  //cell B4
sheet.Range[row++, col].Value2 = 25;    //cell B5
sheet.Range[row++, col].Value2 = 11;    //cell B6
sheet.Range[row++, col].Value2 = 2.44;  //cell B7
row = 2;
col = 4;
sheet.Range[row++, col].Value2 = 3.5; //cell D2
sheet.Range[row++, col].Value2 = 2;   //cell D3
sheet.Range[row++, col].Value2 = 100; //cell D4
sheet.Range[row++, col].Value2 = 255; //cell D5
sheet.Range[row++, col].Value2 = 20;  //cell D6
sheet.Range[row++, col].Value2 = 21;  //cell D7
row = 10;
col = 2;
sheet.Range[++row, col].Value2 = "Formula";
sheet.Columns[col].ColumnWidth = 20;
sheet.Range[row, col + 1].Value2 = "Calculated Value";
string formula;
IXlsRange Cell;
//Simple operators
formula = "=(100-3)/(2+5/(2+1))";
row++;
Cell = sheet.Range[row, col];
Cell.Formula = formula;
sheet.Range[row, col + 1].Value2 = "'" + formula;
////Sum function
formula = "=Sum(B2:B4,D2:D4)";
row++;
Cell = sheet.Range[row, col];
Cell.Formula = formula;
sheet.Range[row, col + 1].Value2 = "'" + formula;
////IF,SUM,AVERAGE functions
formula = "=IF(SUM(B2:B4)>SUM(D2:D4);AVERAGE(B2:B4);AVERAGE(D2:D4))";
row++;
Cell = sheet.Range[row, col];
Cell.Formula = formula;
sheet.Range[row, col + 1].Value = "'" + formula;
//////COUNT function
formula = "=COUNT(B2:D4)";
row++;
Cell = sheet.Range[row, col];
Cell.Formula = formula;
sheet.Range[row, col + 1].Value = "'" + formula;
////Power operation
formula = "=8^2";
row++;
Cell = sheet.Range[row, col];
Cell.Formula = formula;
sheet.Range[row, col + 1].Value = "'" + formula;
////Expression with float constants
formula = "=2.33-.22";
row++;
Cell = sheet.Range[row, col];
Cell.Formula = formula;
sheet.Range[row, col + 1].Value = "'" + formula;
////formula refers to cells with formula
formula = "=SUM(B12:B15)";
row++;
Cell = sheet.Range[row, col];
Cell.Formula = formula;
sheet.Range[row, col + 1].Value = "'" + formula;
book.Save(FileName);