How is Excel culture defined using XlsIO?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can also define the culture.
Excel culture is defined based on the machine by default. In addition, XlsIO provides an option to control the culture by changing the current and UI culture settings of an application.
For example, the value 12.43452 in German culture is returned as 12,43452 and in US culture it is returned as 12.43452 when accessed using DisplayText property. The following steps show how to control Excel document to a specific culture.
Steps to define Excel culture programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application
Step 2: Install Syncfusion.XlsIO.WinForms NuGet package as a reference to your .NET Framework applications from the NuGet.org.
Install NuGet package
Step 3: Include the following namespaces in the Program.cs file.
C#
using Syncfusion.XlsIO; using System.Threading; using System.Globalization;
VB.NET
Imports Syncfusion.XlsIO Imports System.Threading Imports System.Globalization
Step 4: Add the following code snippet to define Excel culture using C# and VB.NET. It should be noted that the thread culture must be set before accessing the value. Hence, it is recommended to set the culture before the instantiation of the ExcelEngine object.
Please refer the below code snippet to set culture using XlsIO in C#/VB.NET.
C#
//Set the Excel document to German culture Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE"); //Create an instance of ExcelEngine using (ExcelEngine excelEngine = new ExcelEngine()) { //Instantiate the Excel application object IApplication application = excelEngine.Excel; //Set the default application version application.DefaultVersion = ExcelVersion.Excel2016; //Create a workbook IWorkbook workbook = application.Workbooks.Create(1); //Access the first worksheet object in the worksheets collection IWorksheet worksheet = workbook.Worksheets[0]; //Set a value to A1 worksheet.Range["A1"].Number = 12.43452; //Retrieve DisplayText from A1 string displaytext = worksheet.Range["A1"].DisplayText; //Save the workbook workbook.SaveAs("ExcelCulture.xlsx"); }
VB.NET
'Set the Excel document to German culture Thread.CurrentThread.CurrentCulture = New CultureInfo("de-DE") Thread.CurrentThread.CurrentUICulture = New CultureInfo("de-DE") 'Create an instance of ExcelEngine Using excelEngine As ExcelEngine = New ExcelEngine() 'Instantiate the Excel application object Dim application As IApplication = excelEngine.Excel 'Set the default application version application.DefaultVersion = ExcelVersion.Excel2016 'Create a workbook Dim workbook As IWorkbook = application.Workbooks.Create(1) 'Access the first worksheet object in the worksheets collection Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Set a value to A1 worksheet.Range("A1").Number = 12.43452 'Retrieve DisplayText from A1 Dim displaytext As String = worksheet.Range("A1").DisplayText 'Save the workbook workbook.SaveAs("ExcelCulture.xlsx") End Using
A complete working example to define Excel culture can be downloaded from DefineExcelCulture.zip.
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheet or workbook, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to worksheet using Template Markers, and most importantly PDF and Image conversions with code examples.
Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.
An online sample link to generate Excel file.
See Also:
Read Excel from Code in C#, VB.NET
Create Excel file in Azure platform
Create Excel from DataTable in C#, VB.NET
How to check if a column exists in Excel table using XlsIO?
Create Pivot Table in C#, VB.NET
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering Syncfusion license key in your application to use the components without trial message.