Working with spreadsheets is one of the key tasks of developing business applications. In the C# programming language and platform .NET there are many libraries for automating work with Excel. One of the most popular is SautinSoft Excel .NET library is a powerful tool that allows you to create, edit, and manage Excel files without having to install MS Excel on your computer.
One of the important functions when working with tables is managing the Worksheet View. In this context, we are talking about properties that allow you to customize the display of the sheet for the user, improve interaction and increase the convenience of working with data.
Viewing a worksheet in Excel is a mode that allows the user to view the contents of a spreadsheet without editing, as well as perform actions such as zooming, scrolling, and viewing various elements of the worksheet for ease of analysis and navigation.
Advantages:
Step-by-step guide:
Complete code
using System;
using System.Collections.Generic;
using System.IO;
using SautinSoft.Excel;
namespace Example
{
class Program
{
static void Main(string[] args)
{
// Get your free key here:
// https://sautinsoft.com/start-for-free/
CreateXlsxOptions();
}
/// <summary>
/// Create a XLSX document and change Option Properties.
/// </summary>
/// <remarks>
/// Details: https://www.sautinsoft.com/products/excel/help/net/developer-guide/create-xlsx-options-properties-net-csharp-vb.php
/// </remarks>
static void CreateXlsxOptions()
{
string outFile = @"..\..\..\example.xlsx";
// The file format is detected automatically from the file extension: ".xlsx".
ExcelDocument excel = new ExcelDocument();
// Add an empty worksheet to the file
excel.Worksheets.Add("Page 1");
var worksheet = excel.Worksheets["Page 1"];
worksheet.ViewOptions.FirstVisibleRow = 3;
worksheet.ViewOptions.FirstVisibleColumn = 3;
worksheet.ViewOptions.ShowFormulas = false;
worksheet.ViewOptions.ShowSheetHeaders = true;
worksheet.ViewOptions.Zoom = 50;
worksheet.ViewOptions.ShowGridLines = true;
worksheet.ViewOptions.ShowOutlineSymbols = true;
worksheet.ViewOptions.OutlineColumnButtonsRight = true;
worksheet.ViewOptions.ShowZeroValues = false;
worksheet.ViewOptions.OutlineRowButtonsBelow = true;
//worksheet.ViewOptions.ShowColumnsFromRightToLeft = true ;
worksheet.ViewOptions.PageBreakViewZoom = 400;
//worksheet.Protected = false;
worksheet.Visibility = false;
worksheet.HeadersFooters.Header = "Header";
worksheet.HeadersFooters.DifferentFirst = true;
worksheet.Cells["J15"].Formula = "=D2*E2";
// Set the settings for the first or even headers and footers
worksheet.HeadersFooters.DifferentFirst = false;
worksheet.HeadersFooters.DifferentOddEven = false;
// Sample data
List<List<object>> data = new List<List<object>>() {
new List<object> { "Date", "Product", "Category", "Quantity", "Unit Price", "Total Cost" },
new List<object> { new DateOnly(2024, 12, 1).ToString(), "Apples", "Fruits", 15, 1.2, "=D2*E2" },
new List<object> { new DateOnly(2024, 12, 1).ToString(), "Bread", "Bakery", 10, 0.8, "=D3*E3" },
new List<object> { new DateOnly(2024, 12, 2).ToString(), "Milk", "Dairy", 20, 1.5, "=D4*E4" },
new List<object> { new DateOnly(2024, 12, 2).ToString(), "Oranges", "Fruits", 10, 1.8, "=D5*E5" },
new List<object> { new DateOnly(2024, 12, 3).ToString(), "Chocolates", "Sweets", 5, 2.5, "=D6*E6" },
new List<object> { new DateOnly(2024, 12, 3).ToString(), "Potatoes", "Vegetables", 25, 0.5, "=D7*E7" },
};
// Inserting data
int i = 1;
foreach (var row in data)
{
int j = 0;
foreach (var item in row)
{
worksheet.Cells["ABCDEFGHIJKLMNOPQRSTUVWXYZ"[j] + i.ToString()].Value = item;
j++;
}
i++;
}
// Saving the excel document
excel.Save(outFile);
// Important for Linux: Install MS Fonts
// sudo apt install ttf-mscorefonts-installer -y
// Open the result for demonstration purposes.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
}
}
}
Option Infer On
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports SautinSoft.Excel
Namespace Example
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Get your free key here:
' https://sautinsoft.com/start-for-free/
CreateXlsxOptions()
End Sub
''' <summary>
''' Create a XLSX document and change Option Properties.
''' </summary>
''' <remarks>
''' Details: https://www.sautinsoft.com/products/excel/help/net/developer-guide/create-xlsx-options-properties-net-csharp-vb.php
''' </remarks>
Private Shared Sub CreateXlsxOptions()
Dim outFile As String = "..\..\..\example.xlsx"
' The file format is detected automatically from the file extension: ".xlsx".
Dim excel As New ExcelDocument()
' Add an empty worksheet to the file
excel.Worksheets.Add("Page 1")
Dim worksheet = excel.Worksheets("Page 1")
worksheet.ViewOptions.FirstVisibleRow = 3
worksheet.ViewOptions.FirstVisibleColumn = 3
worksheet.ViewOptions.ShowFormulas = False
worksheet.ViewOptions.ShowSheetHeaders = True
worksheet.ViewOptions.Zoom = 50
worksheet.ViewOptions.ShowGridLines = True
worksheet.ViewOptions.ShowOutlineSymbols = True
worksheet.ViewOptions.OutlineColumnButtonsRight = True
worksheet.ViewOptions.ShowZeroValues = False
worksheet.ViewOptions.OutlineRowButtonsBelow = True
'worksheet.ViewOptions.ShowColumnsFromRightToLeft = true ;
worksheet.ViewOptions.PageBreakViewZoom = 400
'worksheet.Protected = false;
worksheet.Visibility = False
worksheet.HeadersFooters.Header = "Header"
worksheet.HeadersFooters.DifferentFirst = True
worksheet.Cells("J15").Formula = "=D2*E2"
' Set the settings for the first or even headers and footers
worksheet.HeadersFooters.DifferentFirst = False
worksheet.HeadersFooters.DifferentOddEven = False
' Sample data
Dim data As New List(Of List(Of Object))() _
From {
New List(Of Object) From {"Date", "Product", "Category", "Quantity", "Unit Price", "Total Cost"},
New List(Of Object) From {(New DateOnly(2024, 12, 1)).ToString(), "Apples", "Fruits", 15, 1.2, "=D2*E2"},
New List(Of Object) From {(New DateOnly(2024, 12, 1)).ToString(), "Bread", "Bakery", 10, 0.8, "=D3*E3"},
New List(Of Object) From {(New DateOnly(2024, 12, 2)).ToString(), "Milk", "Dairy", 20, 1.5, "=D4*E4"},
New List(Of Object) From {(New DateOnly(2024, 12, 2)).ToString(), "Oranges", "Fruits", 10, 1.8, "=D5*E5"},
New List(Of Object) From {(New DateOnly(2024, 12, 3)).ToString(), "Chocolates", "Sweets", 5, 2.5, "=D6*E6"},
New List(Of Object) From {(New DateOnly(2024, 12, 3)).ToString(), "Potatoes", "Vegetables", 25, 0.5, "=D7*E7"}
}
' Inserting data
Dim i As Integer = 1
For Each row In data
Dim j As Integer = 0
For Each item In row
worksheet.Cells("ABCDEFGHIJKLMNOPQRSTUVWXYZ".Chars(j) + i.ToString()).Value = item
j += 1
Next item
i += 1
Next row
' Saving the excel document
excel.Save(outFile)
' Important for Linux: Install MS Fonts
' sudo apt install ttf-mscorefonts-installer -y
' Open the result for demonstration purposes.
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
End Sub
End Class
End Namespace
If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: