diff --git a/ClosedXML_Examples/ClosedXML_Examples.csproj b/ClosedXML_Examples/ClosedXML_Examples.csproj index 2e02ca0..9cd5941 100644 --- a/ClosedXML_Examples/ClosedXML_Examples.csproj +++ b/ClosedXML_Examples/ClosedXML_Examples.csproj @@ -95,6 +95,7 @@ + diff --git a/ClosedXML_Examples/Creating/CreateFiles.cs b/ClosedXML_Examples/Creating/CreateFiles.cs index 24cd221..1660039 100644 --- a/ClosedXML_Examples/Creating/CreateFiles.cs +++ b/ClosedXML_Examples/Creating/CreateFiles.cs @@ -65,6 +65,7 @@ new RowCells().Create(path + @"\RowCells.xlsx"); new FreezePanes().Create(path + @"\FreezePanes.xlsx"); new UsingTables().Create(path + @"\UsingTables.xlsx"); + new AddingRowToTables().Create(path + @"\AddingRowToTables.xlsx"); new ShowCase().Create(path + @"\ShowCase.xlsx"); new CopyingWorksheets().Create(path + @"\CopyingWorksheets.xlsx"); new InsertingTables().Create(path + @"\InsertingTables.xlsx"); diff --git a/ClosedXML_Examples/Ranges/AddingRowToTables.cs b/ClosedXML_Examples/Ranges/AddingRowToTables.cs new file mode 100644 index 0000000..8320397 --- /dev/null +++ b/ClosedXML_Examples/Ranges/AddingRowToTables.cs @@ -0,0 +1,58 @@ +using System; +using System.IO; +using ClosedXML.Excel; +using System.Linq; + + +namespace ClosedXML_Examples.Ranges +{ + public class AddingRowToTables : IXLExample + { + #region Methods + + // Public + public void Create(String filePath) + { + string tempFile = ExampleHelper.GetTempFilePath(filePath); + try + { + new BasicTable().Create(tempFile); + var wb = new XLWorkbook(tempFile); + var ws = wb.Worksheets.First(); + + var firstCell = ws.FirstCellUsed(); + var lastCell = ws.LastCellUsed(); + var range = ws.Range(firstCell.Address, lastCell.Address); + range.FirstRow().Delete(); // Deleting the "Contacts" header (we don't need it for our purposes) + + // We want to use a theme for table, not the hard coded format of the BasicTable + range.Clear(XLClearOptions.Formats); + // Put back the date and number formats + range.Column(4).Style.NumberFormat.NumberFormatId = 15; + range.Column(5).Style.NumberFormat.Format = "$ #,##0"; + + var table = range.CreateTable(); // You can also use range.AsTable() if you want to + + ws.Cell("Q6000").Value = "dummy value"; + + var row = table.DataRange.InsertRowsBelow(1).First(); + + wb.SaveAs(filePath); + } + finally + { + if (File.Exists(tempFile)) + { + File.Delete(tempFile); + } + } + } + + // Private + + // Override + + + #endregion + } +} diff --git a/ClosedXML_Tests/ClosedXML_Tests.csproj b/ClosedXML_Tests/ClosedXML_Tests.csproj index 80cf986..e822189 100644 --- a/ClosedXML_Tests/ClosedXML_Tests.csproj +++ b/ClosedXML_Tests/ClosedXML_Tests.csproj @@ -241,7 +241,9 @@ - + + + diff --git a/ClosedXML_Tests/Examples/RangesTests.cs b/ClosedXML_Tests/Examples/RangesTests.cs index 07dae9f..8595872 100644 --- a/ClosedXML_Tests/Examples/RangesTests.cs +++ b/ClosedXML_Tests/Examples/RangesTests.cs @@ -105,6 +105,12 @@ } [Test] + public void AddingRowToTables() + { + TestHelper.RunTestExample(@"Ranges\AddingRowToTables.xlsx"); + } + + [Test] public void WalkingRanges() { TestHelper.RunTestExample(@"Ranges\WalkingRanges.xlsx"); diff --git a/ClosedXML_Tests/Resource/Examples/Ranges/AddingRowToTables.xlsx b/ClosedXML_Tests/Resource/Examples/Ranges/AddingRowToTables.xlsx new file mode 100644 index 0000000..02e82ea --- /dev/null +++ b/ClosedXML_Tests/Resource/Examples/Ranges/AddingRowToTables.xlsx Binary files differ