diff --git a/ClosedXML_Examples/ClosedXML_Examples.csproj b/ClosedXML_Examples/ClosedXML_Examples.csproj index 13237ec..ac1caed 100644 --- a/ClosedXML_Examples/ClosedXML_Examples.csproj +++ b/ClosedXML_Examples/ClosedXML_Examples.csproj @@ -113,13 +113,11 @@ - - @@ -170,6 +168,8 @@ + + diff --git a/ClosedXML_Examples/Creating/CreateFiles.cs b/ClosedXML_Examples/Creating/CreateFiles.cs index 2afcf33..8f1978f 100644 --- a/ClosedXML_Examples/Creating/CreateFiles.cs +++ b/ClosedXML_Examples/Creating/CreateFiles.cs @@ -4,6 +4,7 @@ using ClosedXML_Examples.Ranges; using ClosedXML_Examples.Rows; using ClosedXML_Examples.Styles; +using ClosedXML_Examples.Tables; using System.IO; namespace ClosedXML_Examples diff --git a/ClosedXML_Examples/Misc/CopyingWorksheets.cs b/ClosedXML_Examples/Misc/CopyingWorksheets.cs index b0843eb..e06cd66 100644 --- a/ClosedXML_Examples/Misc/CopyingWorksheets.cs +++ b/ClosedXML_Examples/Misc/CopyingWorksheets.cs @@ -1,6 +1,6 @@ -using System.IO; using ClosedXML.Excel; -using ClosedXML_Examples.Ranges; +using ClosedXML_Examples.Tables; +using System.IO; namespace ClosedXML_Examples.Misc { @@ -18,7 +18,7 @@ var wsSource = wb.Worksheet(1); // Copy the worksheet to a new sheet in this workbook wsSource.CopyTo("Copy"); - + // We're going to open another workbook to show that you can // copy a sheet from one workbook to another: new BasicTable().Create(tempFile2); @@ -40,6 +40,5 @@ } } } - } } diff --git a/ClosedXML_Examples/Ranges/ResizingTables.cs b/ClosedXML_Examples/Ranges/ResizingTables.cs deleted file mode 100644 index 38b7db0..0000000 --- a/ClosedXML_Examples/Ranges/ResizingTables.cs +++ /dev/null @@ -1,41 +0,0 @@ -using ClosedXML.Excel; -using System.IO; -using System.Linq; - -namespace ClosedXML_Examples.Ranges -{ - public class ResizingTables : IXLExample - { - public void Create(string filePath) - { - string tempFile = ExampleHelper.GetTempFilePath(filePath); - try - { - new UsingTables().Create(tempFile); - using (var wb = new XLWorkbook(tempFile)) - { - var ws1 = wb.Worksheets.First(); - - var ws2 = ws1.CopyTo("Contacts 2"); - ws2.Cell("A2").Value = "Index"; - ws2.Cell("A3").Value = Enumerable.Range(1, 3).ToArray(); - var table2 = ws2.Tables.First().SetShowTotalsRow(false); - table2.Resize(ws2.Range(ws2.Cell("A2"), table2.DataRange.LastCell())); - - var ws3 = ws1.CopyTo("Contacts 3"); - var table3 = ws3.Tables.First().SetShowTotalsRow(false); - table3.Resize(ws3.Range(table3.AsRange().FirstCell(), table3.DataRange.LastCell().CellLeft())); - - wb.SaveAs(filePath); - } - } - finally - { - if (File.Exists(tempFile)) - { - File.Delete(tempFile); - } - } - } - } -} diff --git a/ClosedXML_Examples/Ranges/UsingTables.cs b/ClosedXML_Examples/Ranges/UsingTables.cs deleted file mode 100644 index e5c24b2..0000000 --- a/ClosedXML_Examples/Ranges/UsingTables.cs +++ /dev/null @@ -1,106 +0,0 @@ -using System; -using System.IO; -using ClosedXML.Excel; -using System.Linq; - - -namespace ClosedXML_Examples.Ranges -{ - public class UsingTables : 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.Worksheet(1); - ws.Name = "Contacts Table"; - 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 - // manipulate the range as a table but don't want - // to create the table in the worksheet. - - // Let's activate the Totals row and add the sum of Income - table.ShowTotalsRow = true; - table.Field("Income").TotalsRowFunction = XLTotalsRowFunction.Sum; - // Just for fun let's add the text "Sum Of Income" to the totals row - table.Field(0).TotalsRowLabel = "Sum Of Income"; - - // Copy all the headers - Int32 columnWithHeaders = lastCell.Address.ColumnNumber + 2; - Int32 currentRow = table.RangeAddress.FirstAddress.RowNumber; - ws.Cell(currentRow, columnWithHeaders).Value = "Table Headers"; - foreach (var cell in table.HeadersRow().Cells()) - { - currentRow++; - ws.Cell(currentRow, columnWithHeaders).Value = cell.Value; - } - - // Format the headers as a table with a different style and no autofilters - var htFirstCell = ws.Cell(table.RangeAddress.FirstAddress.RowNumber, columnWithHeaders); - var htLastCell = ws.Cell(currentRow, columnWithHeaders); - var headersTable = ws.Range(htFirstCell, htLastCell).CreateTable("Headers"); - headersTable.Theme = XLTableTheme.TableStyleLight10; - headersTable.ShowAutoFilter = false; - - // Add a custom formula to the headersTable - headersTable.ShowTotalsRow = true; - headersTable.Field(0).TotalsRowFormulaA1 = "CONCATENATE(\"Count: \", CountA(Headers[Table Headers]))"; - - // Copy the names - Int32 columnWithNames = columnWithHeaders + 2; - currentRow = table.RangeAddress.FirstAddress.RowNumber; // reset the currentRow - ws.Cell(currentRow, columnWithNames).Value = "Names"; - foreach (var row in table.DataRange.Rows()) - { - currentRow++; - var fName = row.Field("FName").GetString(); // Notice how we're calling the cell by field name - var lName = row.Field("LName").GetString(); // Notice how we're calling the cell by field name - var name = String.Format("{0} {1}", fName, lName); - ws.Cell(currentRow, columnWithNames).Value = name; - } - - // Format the names as a table with a different style and no autofilters - var ntFirstCell = ws.Cell(table.RangeAddress.FirstAddress.RowNumber, columnWithNames); - var ntLastCell = ws.Cell(currentRow, columnWithNames); - var namesTable = ws.Range(ntFirstCell, ntLastCell).CreateTable(); - namesTable.Theme = XLTableTheme.TableStyleLight12; - namesTable.ShowAutoFilter = false; - - ws.Columns().AdjustToContents(); - ws.Columns("A,G,I").Width = 3; - - wb.SaveAs(filePath); - } - finally - { - if (File.Exists(tempFile)) - { - File.Delete(tempFile); - } - } - } - - // Private - - // Override - - - #endregion - } -} diff --git a/ClosedXML_Examples/Tables/ResizingTables.cs b/ClosedXML_Examples/Tables/ResizingTables.cs new file mode 100644 index 0000000..14dcea1 --- /dev/null +++ b/ClosedXML_Examples/Tables/ResizingTables.cs @@ -0,0 +1,41 @@ +using ClosedXML.Excel; +using System.IO; +using System.Linq; + +namespace ClosedXML_Examples.Tables +{ + public class ResizingTables : IXLExample + { + public void Create(string filePath) + { + string tempFile = ExampleHelper.GetTempFilePath(filePath); + try + { + new UsingTables().Create(tempFile); + using (var wb = new XLWorkbook(tempFile)) + { + var ws1 = wb.Worksheets.First(); + + var ws2 = ws1.CopyTo("Contacts 2"); + ws2.Cell("A2").Value = "Index"; + ws2.Cell("A3").Value = Enumerable.Range(1, 3).ToArray(); + var table2 = ws2.Tables.First().SetShowTotalsRow(false); + table2.Resize(ws2.Range(ws2.Cell("A2"), table2.DataRange.LastCell())); + + var ws3 = ws1.CopyTo("Contacts 3"); + var table3 = ws3.Tables.First().SetShowTotalsRow(false); + table3.Resize(ws3.Range(table3.AsRange().FirstCell(), table3.DataRange.LastCell().CellLeft())); + + wb.SaveAs(filePath); + } + } + finally + { + if (File.Exists(tempFile)) + { + File.Delete(tempFile); + } + } + } + } +} diff --git a/ClosedXML_Examples/Tables/UsingTables.cs b/ClosedXML_Examples/Tables/UsingTables.cs new file mode 100644 index 0000000..b05e5b5 --- /dev/null +++ b/ClosedXML_Examples/Tables/UsingTables.cs @@ -0,0 +1,103 @@ +using ClosedXML.Excel; +using System; +using System.IO; + +namespace ClosedXML_Examples.Tables +{ + public class UsingTables : 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.Worksheet(1); + ws.Name = "Contacts Table"; + 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 + // manipulate the range as a table but don't want + // to create the table in the worksheet. + + // Let's activate the Totals row and add the sum of Income + table.ShowTotalsRow = true; + table.Field("Income").TotalsRowFunction = XLTotalsRowFunction.Sum; + // Just for fun let's add the text "Sum Of Income" to the totals row + table.Field(0).TotalsRowLabel = "Sum Of Income"; + + // Copy all the headers + Int32 columnWithHeaders = lastCell.Address.ColumnNumber + 2; + Int32 currentRow = table.RangeAddress.FirstAddress.RowNumber; + ws.Cell(currentRow, columnWithHeaders).Value = "Table Headers"; + foreach (var cell in table.HeadersRow().Cells()) + { + currentRow++; + ws.Cell(currentRow, columnWithHeaders).Value = cell.Value; + } + + // Format the headers as a table with a different style and no autofilters + var htFirstCell = ws.Cell(table.RangeAddress.FirstAddress.RowNumber, columnWithHeaders); + var htLastCell = ws.Cell(currentRow, columnWithHeaders); + var headersTable = ws.Range(htFirstCell, htLastCell).CreateTable("Headers"); + headersTable.Theme = XLTableTheme.TableStyleLight10; + headersTable.ShowAutoFilter = false; + + // Add a custom formula to the headersTable + headersTable.ShowTotalsRow = true; + headersTable.Field(0).TotalsRowFormulaA1 = "CONCATENATE(\"Count: \", CountA(Headers[Table Headers]))"; + + // Copy the names + Int32 columnWithNames = columnWithHeaders + 2; + currentRow = table.RangeAddress.FirstAddress.RowNumber; // reset the currentRow + ws.Cell(currentRow, columnWithNames).Value = "Names"; + foreach (var row in table.DataRange.Rows()) + { + currentRow++; + var fName = row.Field("FName").GetString(); // Notice how we're calling the cell by field name + var lName = row.Field("LName").GetString(); // Notice how we're calling the cell by field name + var name = String.Format("{0} {1}", fName, lName); + ws.Cell(currentRow, columnWithNames).Value = name; + } + + // Format the names as a table with a different style and no autofilters + var ntFirstCell = ws.Cell(table.RangeAddress.FirstAddress.RowNumber, columnWithNames); + var ntLastCell = ws.Cell(currentRow, columnWithNames); + var namesTable = ws.Range(ntFirstCell, ntLastCell).CreateTable(); + namesTable.Theme = XLTableTheme.TableStyleLight12; + namesTable.ShowAutoFilter = false; + + ws.Columns().AdjustToContents(); + ws.Columns("A,G,I").Width = 3; + + wb.SaveAs(filePath); + } + finally + { + if (File.Exists(tempFile)) + { + File.Delete(tempFile); + } + } + } + + // Private + + // Override + + #endregion Methods + } +} diff --git a/ClosedXML_Tests/ClosedXML_Tests.csproj b/ClosedXML_Tests/ClosedXML_Tests.csproj index 0531fa7..842079f 100644 --- a/ClosedXML_Tests/ClosedXML_Tests.csproj +++ b/ClosedXML_Tests/ClosedXML_Tests.csproj @@ -76,6 +76,7 @@ + @@ -222,7 +223,6 @@ - @@ -291,7 +291,8 @@ - + + diff --git a/ClosedXML_Tests/Examples/RangesTests.cs b/ClosedXML_Tests/Examples/RangesTests.cs index 5d78654..59cd62b 100644 --- a/ClosedXML_Tests/Examples/RangesTests.cs +++ b/ClosedXML_Tests/Examples/RangesTests.cs @@ -99,18 +99,6 @@ } [Test] - public void UsingTables() - { - TestHelper.RunTestExample(@"Ranges\UsingTables.xlsx"); - } - - [Test] - public void ResizingTables() - { - TestHelper.RunTestExample(@"Ranges\ResizingTables.xlsx"); - } - - [Test] public void AddingRowToTables() { TestHelper.RunTestExample(@"Ranges\AddingRowToTables.xlsx"); diff --git a/ClosedXML_Tests/Resource/Examples/Ranges/ResizingTables.xlsx b/ClosedXML_Tests/Resource/Examples/Ranges/ResizingTables.xlsx deleted file mode 100644 index 9259bba..0000000 --- a/ClosedXML_Tests/Resource/Examples/Ranges/ResizingTables.xlsx +++ /dev/null Binary files differ diff --git a/ClosedXML_Tests/Resource/Examples/Ranges/UsingTables.xlsx b/ClosedXML_Tests/Resource/Examples/Ranges/UsingTables.xlsx deleted file mode 100644 index 288d1fe..0000000 --- a/ClosedXML_Tests/Resource/Examples/Ranges/UsingTables.xlsx +++ /dev/null Binary files differ diff --git a/ClosedXML_Tests/Resource/Examples/Tables/ResizingTables.xlsx b/ClosedXML_Tests/Resource/Examples/Tables/ResizingTables.xlsx new file mode 100644 index 0000000..9259bba --- /dev/null +++ b/ClosedXML_Tests/Resource/Examples/Tables/ResizingTables.xlsx Binary files differ diff --git a/ClosedXML_Tests/Resource/Examples/Tables/UsingTables.xlsx b/ClosedXML_Tests/Resource/Examples/Tables/UsingTables.xlsx new file mode 100644 index 0000000..288d1fe --- /dev/null +++ b/ClosedXML_Tests/Resource/Examples/Tables/UsingTables.xlsx Binary files differ