Skip to content

Commit

Permalink
Excel Table creation sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohan2401 committed Apr 23, 2024
1 parent e6f0180 commit 7423990
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Create and Edit Table/ExcelTable/ExcelTable.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34414.90
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExcelTable", "ExcelTable\ExcelTable.csproj", "{3AC8CBBD-68FB-485B-A202-35ABE6681BE6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3AC8CBBD-68FB-485B-A202-35ABE6681BE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3AC8CBBD-68FB-485B-A202-35ABE6681BE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3AC8CBBD-68FB-485B-A202-35ABE6681BE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3AC8CBBD-68FB-485B-A202-35ABE6681BE6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {ED8871A2-860B-4E8F-9EFD-F92673F1AACB}
EndGlobalSection
EndGlobal
Binary file not shown.
18 changes: 18 additions & 0 deletions Create and Edit Table/ExcelTable/ExcelTable/ExcelTable.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="25.1.40" />
</ItemGroup>

<ItemGroup>
<Folder Include="Output\" />
</ItemGroup>

</Project>
Binary file not shown.
53 changes: 53 additions & 0 deletions Create and Edit Table/ExcelTable/ExcelTable/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Syncfusion.XlsIO;

namespace ExcelTable
{
class Program
{
public static void Main()
{
Program program = new Program();
program.CreateTable();
}

/// <summary>
/// Creates a Excel table in the existing Excel document.
/// </summary>
public void CreateTable()
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
FileStream fileStream = new FileStream("../../../Data/SalesReport.xlsx", FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic);
IWorksheet worksheet = workbook.Worksheets[0];

AddExcelTable("Table1", worksheet.UsedRange);

string fileName = "../../../Output/SalesReport.xlsx";

//Saving the workbook as stream
FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(stream);
stream.Dispose();
}
}

/// <summary>
/// Adds a table to the worksheet with the given name and range
/// </summary>
/// <param name="tableName">Table name</param>
/// <param name="tableRange">Table range</param>
public void AddExcelTable(string tableName, IRange tableRange)
{
IWorksheet worksheet = tableRange.Worksheet;

//Create table with the data in given range
IListObject table = worksheet.ListObjects.Create(tableName, tableRange);

//Set table style
table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium14;
}
}
}

0 comments on commit 7423990

Please sign in to comment.