Articles in this section
Category / Section

How to Dynamically Use Formulas to Compute Grades in C#, VB.NET?

3 mins read

Syncfusion® Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.

While working with Excel worksheets, you can dynamically apply formulas to compute grades based on score thresholds. This example demonstrates how to assign grades such as A, B, or C to students based on their total score using C# and VB.NET.

Include the following namespace in the Program.cs file.

C#

using Syncfusion.XlsIO;

VB.NET

Imports Syncfusion.XlsIO

Use the following code snippet for dynamically apply formulas to compute grades based on score thresholds.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.xlsx"), FileMode.Open, FileAccess.Read);
    IWorkbook workbook = application.Workbooks.Open(inputStream);
    IWorksheet worksheet = workbook.Worksheets[0];

    worksheet["E1"].Text = "Rank";
    worksheet["E1"].CellStyle.Font.Bold = true;

    //Apply grading logic using nested IF formulas
    worksheet["E2"].Formula = "=IF(D2>=270,\"A\", IF(D2>=250,\"B\", IF(D2>=230,\"C\",\"D\")))";
    worksheet["E3"].Formula = "=IF(D3>=270,\"A\", IF(D3>=250,\"B\", IF(D3>=230,\"C\",\"D\")))";
    worksheet["E4"].Formula = "=IF(D4>=270,\"A\", IF(D4>=250,\"B\", IF(D4>=230,\"C\",\"D\")))";
    worksheet["E5"].Formula = "=IF(D5>=270,\"A\", IF(D5>=250,\"B\", IF(D5>=230,\"C\",\"D\")))";
    worksheet["E6"].Formula = "=IF(D6>=270,\"A\", IF(D6>=250,\"B\", IF(D6>=230,\"C\",\"D\")))";
    worksheet["E7"].Formula = "=IF(D7>=270,\"A\", IF(D7>=250,\"B\", IF(D7>=230,\"C\",\"D\")))";

    //Align given range to the right
    worksheet.Range["E2:E7"].CellStyle.HorizontalAlignment = ExcelHAlign.HAlignRight;

    //Saving the workbook 
    FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.ReadWrite);
    workbook.SaveAs(outputStream);

    //Dispose streams
    outputStream.Dispose();
    inputStream.Dispose();
}

VB.NET

Using excelEngine As New ExcelEngine()
    Dim application As IApplication = excelEngine.Excel
    application.DefaultVersion = ExcelVersion.Xlsx
    Dim workbook As IWorkbook = application.Workbooks.Open("Input.xlsx")
    Dim worksheet As IWorksheet = workbook.Worksheets(0)

    worksheet("E1").Text = "Rank"
    worksheet("E1").CellStyle.Font.Bold = True

    'Apply grading logic using nested IF formulas
    worksheet("E2").Formula = "=IF(D2>=270,""A"", IF(D2>=250,""B"", IF(D2>=230,""C"",""D"")))"
    worksheet("E3").Formula = "=IF(D3>=270,""A"", IF(D3>=250,""B"", IF(D3>=230,""C"",""D"")))"
    worksheet("E4").Formula = "=IF(D4>=270,""A"", IF(D4>=250,""B"", IF(D4>=230,""C"",""D"")))"
    worksheet("E5").Formula = "=IF(D5>=270,""A"", IF(D5>=250,""B"", IF(D5>=230,""C"",""D"")))"
    worksheet("E6").Formula = "=IF(D6>=270,""A"", IF(D6>=250,""B"", IF(D6>=230,""C"",""D"")))"
    worksheet("E7").Formula = "=IF(D7>=270,""A"", IF(D7>=250,""B"", IF(D7>=230,""C"",""D"")))"

    'Align given range to the right
    worksheet.Range("E2:E7").CellStyle.HorizontalAlignment = ExcelHAlign.HAlignRight

    'Saving the workbook
    workbook.SaveAs("Output.xlsx")
End Using 

You can get the complete sample to dynamically apply formulas to compute grades based on score thresholds from here.

I hope you enjoyed learning about how to dynamically apply formulas to compute grades based on score thresholds using C# and VB.NET in XlsIO.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, support tickets, or feedback portal. We are always happy to assist you!

Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.

StudentRank.zip
Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied