NativeExcel for .NET Developer's Reference

Ten Times Table sample

Example

This example creates worksheet with the Ten Times Table

Output file: console-tentimestable.xls

[C#]
using System;
using NativeExcel;

namespace Console_TenTimesTable
{
    class Program
    {
        static void Main(string[] args)
        {
            string FileName = "console-tentimestable.xls";
            CreateWorkbook(FileName);
            OpenWorkbookWithExcel(FileName);
        }

        static void CreateWorkbook(string FileName) {
            //Create workbook
            IWorkbook book = NativeExcel.Factory.CreateWorkbook();
            //Add sheet
            IWorksheet sheet = book.Worksheets.Add();
            sheet.Name = "Ten times table";

            //General formatting
            IRange range =  sheet.Range["B2:M14"];

            range.Font.Size = 12;
            range.Font.Bold = true;
            range.ColumnWidth = 5.71;
            range.Interior.ColorIndex = 45;
            //borders
            range.Borders[XlBordersIndex.xlAround].ColorIndex = XlColorIndex.xlColorIndexAutomatic;
            range.Borders[XlBordersIndex.xlAround].Weight = XlBorderWeight.xlThick;
            range.Borders[XlBordersIndex.xlInsideAll].ColorIndex = XlColorIndex.xlColorIndexAutomatic;

            range.HorizontalAlignment = XlHAlign.xlHAlignCenter;

            //Title
            IRange rg = range.Rows[1]; 
            rg.Merge();
            rg.Font.Size = 14;
            rg.Value = "Ten Times Table";

            //Columns header  
            rg = range.Rows[2];
            rg.Interior.ColorIndex = 53;
            rg.Font.Color = System.Drawing.Color.White;  
            
            //rows header
            rg = sheet.Range["B3:B14"]; 
            rg.Interior.ColorIndex = 53;
            rg.Font.Color = System.Drawing.Color.White;     

            for (int i = 0; i <= 10; i++) {
                //column header
                range.Columns[1][i + 3].Value = i;
                //row header
                range.Rows[2][i + 2].Value = i;
            } 

            for (int r = 0; r <= 10; r++) {
                for (int c = 0; c <= 10; c++) {
                    range[3 + r, 2 + c].Value = r * c;
                } 
            }

            book.SaveAs(FileName);
        }

        static void OpenWorkbookWithExcel(string FileName){
            try {
                System.Diagnostics.Process.Start(FileName);
            } catch {
                Console.WriteLine(FileName + " created in application folder");
            }
        }
    }
}
[Visual Basic]
imports System
imports NativeExcel

Module Console_TenTimesTable

    Sub Main()
        Dim FileName As String = "console-tentimestable.xls"
        CreateWorkbook(FileName)
        OpenWorkbookWithExcel(FileName)
    End Sub

    Sub CreateWorkbook(FileName As String)
        'Create workbook
        Dim book As IWorkbook = NativeExcel.Factory.CreateWorkbook()
        'Add sheet
        Dim sheet As IWorksheet = book.Worksheets.Add()
        sheet.Name = "Ten times table"

        'General formatting
        Dim range As IRange =  sheet.Range("B2:M14")

        range.Font.Size = 12
        range.Font.Bold = true
        range.ColumnWidth = 5.71
        range.Interior.ColorIndex = 45

        'borders
        range.Borders(XlBordersIndex.xlAround).ColorIndex = XlColorIndex.xlColorIndexAutomatic
        range.Borders(XlBordersIndex.xlAround).Weight = XlBorderWeight.xlThick
        range.Borders(XlBordersIndex.xlInsideAll).ColorIndex = XlColorIndex.xlColorIndexAutomatic

        range.HorizontalAlignment = XlHAlign.xlHAlignCenter

        'Title
        Dim rg As IRange = range.Rows(1) 
        rg.Merge()
        rg.Font.Size = 14
        rg.Value = "Ten Times Table"

        'Columns header formatting
        rg = range.Rows(2)
        rg.Interior.ColorIndex = 53
        rg.Font.Color = System.Drawing.Color.White
            
        'rows header formatting
        rg = sheet.Range("B3:B14") 
        rg.Interior.ColorIndex = 53
        rg.Font.Color = System.Drawing.Color.White

        'Columns and rows header values
        For i As Integer = 0 To 10
           'column header
           range.Columns(1)(i + 3).Value = i
           'row header
           range.Rows(2)(i + 2).Value = i
        Next i   

        'Table values
        For r As Integer = 0 To 10
            For c As Integer = 0 To 10
                range(3 + r, 2 + c).Value = r * c
            Next c 
        Next r

        'Save workbook
        book.SaveAs(FileName)

    End Sub

    Sub OpenWorkbookWithExcel(FileName As String)
        Try
           System.Diagnostics.Process.Start(FileName)
        Catch
           Console.WriteLine(FileName + " created in application folder")
        End Try  
    End Sub

End Module