Excel Subtotal in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to set Excel subtotal formula via Spire.XLS.

using System.Drawing;

using Spire.Xls;

namespace Subtotal
{
    class Program
    {
        static void Main(string[] args)
        {
            //create a workbook
            Workbook workbook = new Workbook();
            
            //load the workbook
            workbook.LoadFromFile(@"..\..\..\..\Data\order.xls",ExcelVersion.Version97to2003);
            Worksheet sheet = workbook.Worksheets[0];

            //subtotal the count of order
            sheet.Range["B18"].Text = "the count of order";
            sheet.Range["B19"].Formula = "SUBTOTAL(2,order!B2:B17)";

            //subtotal the minimum of Profits
            sheet.Range["C18"].Text = "the minimum of Profits";
            sheet.Range["C19"].Formula = "SUBTOTAL(5,order!C2:C17)";

            //subtotal the sum of cost
            sheet.Range["D18"].Text = "the sum of cost";
            sheet.Range["D19"].Formula = "SUBTOTAL(9,order!D2:D17)";

            sheet.AllocatedRange.AutoFitColumns();
            sheet.AllocatedRange.HorizontalAlignment = HorizontalAlignType.Center;

            //set the style of new rows
            sheet.Range["A18:D19"].Borders.LineStyle = LineStyleType.Thin;
            sheet.Range["A18:D19"].Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
            sheet.Range["A18:D19"].Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;
            sheet.Range["A18:D19"].Style.Color = Color.Bisque;

            workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
            System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}

Imports System.Drawing

Imports Spire.Xls

Namespace Subtotal
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			'create a workbook
			Dim workbook As New Workbook()

			'load the workbook
			workbook.LoadFromFile("..\..\..\..\Data\order.xls",ExcelVersion.Version97to2003)
			Dim sheet As Worksheet = workbook.Worksheets(0)

			'subtotal the count of order
			sheet.Range("B18").Text = "the count of order"
			sheet.Range("B19").Formula = "SUBTOTAL(2,order!B2:B17)"

			'subtotal the minimum of Profits
			sheet.Range("C18").Text = "the minimum of Profits"
			sheet.Range("C19").Formula = "SUBTOTAL(5,order!C2:C17)"

			'subtotal the sum of cost
			sheet.Range("D18").Text = "the sum of cost"
			sheet.Range("D19").Formula = "SUBTOTAL(9,order!D2:D17)"

			sheet.AllocatedRange.AutoFitColumns()
			sheet.AllocatedRange.HorizontalAlignment = HorizontalAlignType.Center

			'set the style of new rows
			sheet.Range("A18:D19").Borders.LineStyle = LineStyleType.Thin
			sheet.Range("A18:D19").Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.None
			sheet.Range("A18:D19").Borders(BordersLineType.DiagonalUp).LineStyle = LineStyleType.None
			sheet.Range("A18:D19").Style.Color = Color.Bisque

			workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003)
			System.Diagnostics.Process.Start(workbook.FileName)
		End Sub
	End Class
End Namespace