diff --git a/Create and Edit Table/ExcelTable/ExcelTable.sln b/Create and Edit Table/ExcelTable/ExcelTable.sln new file mode 100644 index 00000000..51354e09 --- /dev/null +++ b/Create and Edit Table/ExcelTable/ExcelTable.sln @@ -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 diff --git a/Create and Edit Table/ExcelTable/ExcelTable/Data/SalesReport.xlsx b/Create and Edit Table/ExcelTable/ExcelTable/Data/SalesReport.xlsx new file mode 100644 index 00000000..8304c092 Binary files /dev/null and b/Create and Edit Table/ExcelTable/ExcelTable/Data/SalesReport.xlsx differ diff --git a/Create and Edit Table/ExcelTable/ExcelTable/ExcelTable.csproj b/Create and Edit Table/ExcelTable/ExcelTable/ExcelTable.csproj new file mode 100644 index 00000000..f0cfd2ba --- /dev/null +++ b/Create and Edit Table/ExcelTable/ExcelTable/ExcelTable.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/Create and Edit Table/ExcelTable/ExcelTable/Output/SalesReport.xlsx b/Create and Edit Table/ExcelTable/ExcelTable/Output/SalesReport.xlsx new file mode 100644 index 00000000..a5a17e9e Binary files /dev/null and b/Create and Edit Table/ExcelTable/ExcelTable/Output/SalesReport.xlsx differ diff --git a/Create and Edit Table/ExcelTable/ExcelTable/Program.cs b/Create and Edit Table/ExcelTable/ExcelTable/Program.cs new file mode 100644 index 00000000..2ea7365f --- /dev/null +++ b/Create and Edit Table/ExcelTable/ExcelTable/Program.cs @@ -0,0 +1,53 @@ +using Syncfusion.XlsIO; + +namespace ExcelTable +{ + class Program + { + public static void Main() + { + Program program = new Program(); + program.CreateTable(); + } + + /// + /// Creates a Excel table in the existing Excel document. + /// + 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(); + } + } + + /// + /// Adds a table to the worksheet with the given name and range + /// + /// Table name + /// Table range + 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; + } + } +}