Skip to Content
TExcelQuick start

Quick start

On this page

Use a .NET 10 console application to write a three-column, two-record XLSX and read it back with the same column mapping. Microsoft Excel is not required.

Create a project

dotnet new console -n ExcelDemo -f net10.0 cd ExcelDemo dotnet add package TExcel

Write and read a workbook

Place this in Program.cs. Top-level statements precede the model definition. Running the program creates devices.xlsx and prints both devices.

using TExcel; var devices = new[] { new DeviceRow { Code = "P-001", Name = "Inlet pump", Enabled = true }, new DeviceRow { Code = "P-002", Name = "Outlet pump", Enabled = false } }; Excel.SaveAs("devices.xlsx", devices, sheetName: "Devices"); foreach (var device in Excel.Query<DeviceRow>("devices.xlsx", sheetName: "Devices")) { Console.WriteLine($"{device.Code}: {device.Name}"); } /// <summary>设备工作表的导入导出模型。</summary> public sealed class DeviceRow { /// <summary>用文本保留设备编码及前导零。</summary> [ExcelColumn("Device code", Order = 0)] public string Code { get; set; } = ""; /// <summary>业务设备名称。</summary> [ExcelColumn("Name", Order = 1)] public string Name { get; set; } = ""; /// <summary>设备是否参与当前配置。</summary> [ExcelColumn("Enabled", Order = 2)] public bool Enabled { get; set; } }
dotnet run

Expected output:

P-001: Inlet pump P-002: Outlet pump

Open devices.xlsx in the run directory. The Devices sheet has Device code, Name and Enabled headers followed by two records. Running again recreates the same file; choose distinct paths when retaining earlier exports.

Query<T> is lazy: enumeration performs file access and conversion. Calling ToList() materializes every row in memory.

List worksheets

foreach (var sheet in Excel.GetSheets("devices.xlsx")) { Console.WriteLine($"{sheet.Index}: {sheet.Name}"); }

Place the worksheet-listing fragment before the type declaration in Program.cs. Omitting sheetName selects the first worksheet. Specify the name when importing external templates so a changed worksheet order does not select different data.

Choose your next task

RequirementContinue with
Multiple sheets, batches or streamsWriter lifetime
Different headers, dates or conversion failuresColumn mapping
Large files or raw cell factsReading models
Preview uploaded data before saving itComplete import preview

If the file cannot open, check that writing finished and no other process holds it. If properties read back empty, compare actual headers with ExcelColumn names. Validate required fields, duplicate codes and database constraints during import preview.