Initial commit of the ClosedXML library
Establishes the foundational structure, core Excel functionality, and tooling. - Implements base components for cells, ranges, columns, and rows. - Integrates a custom calculation engine with a wide range of Excel functions. - Adds comprehensive support for styling, conditional formatting, data validation, comments, pictures, and charts. - Configures build setup, project metadata (NuGet), and developer guidelines. - Includes editor and Git attributes for consistent code style and line endings.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
using System.IO;
|
||||
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.AllFormats);
|
||||
// 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 Methods
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Ranges
|
||||
{
|
||||
public class ClearingRanges : IXLExample
|
||||
{
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Clearing Ranges");
|
||||
foreach (var ro in Enumerable.Range(1, 10))
|
||||
{
|
||||
foreach (var co in Enumerable.Range(1, 10))
|
||||
{
|
||||
var cell = ws.Cell(ro, co);
|
||||
cell.Value = cell.Address.ToString();
|
||||
cell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
|
||||
cell.Style.Fill.BackgroundColor = XLColor.Turquoise;
|
||||
cell.Style.Font.Bold = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Clearing a range
|
||||
ws.Range("B1:C2").Clear();
|
||||
|
||||
// Clearing a row in a range
|
||||
ws.Range("B4:C5").Row(1).Clear();
|
||||
|
||||
// Clearing a column in a range
|
||||
ws.Range("E1:F4").Column(2).Clear();
|
||||
|
||||
// Clear an entire row
|
||||
ws.Row(7).Clear();
|
||||
|
||||
// Clear an entire column
|
||||
ws.Column("H").Clear();
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System.IO;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class CopyingRanges : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
var tempFile = ExampleHelper.GetTempFilePath(filePath);
|
||||
try
|
||||
{
|
||||
new BasicTable().Create(tempFile);
|
||||
var workbook = new XLWorkbook(tempFile);
|
||||
var ws = workbook.Worksheet(1);
|
||||
|
||||
// Define a range with the data
|
||||
var firstTableCell = ws.FirstCellUsed();
|
||||
var lastTableCell = ws.LastCellUsed();
|
||||
var rngData = ws.Range(firstTableCell.Address, lastTableCell.Address);
|
||||
|
||||
// Copy the table to another worksheet
|
||||
var wsCopy = workbook.Worksheets.Add("Contacts Copy");
|
||||
wsCopy.Cell(1, 1).Value = rngData;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(tempFile))
|
||||
{
|
||||
File.Delete(tempFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Ranges
|
||||
{
|
||||
public class CurrentRowColumn : IXLExample
|
||||
{
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
var ws = wb.Worksheets.Add("Current Row Column");
|
||||
|
||||
var cell = ws.Cell(5, 2);
|
||||
cell.Style.Fill.SetBackgroundColor(XLColor.Red);
|
||||
ws.Cell(1, 1)
|
||||
.SetValue("Red's Row:")
|
||||
.CellRight().SetValue(cell.WorksheetRow().RowNumber())
|
||||
.CellBelow().SetValue(cell.WorksheetColumn().ColumnLetter())
|
||||
.CellLeft().SetValue("Red's Column:");
|
||||
|
||||
var row = ws.Range("A6:C6").FirstRow();
|
||||
row.Style.Fill.SetBackgroundColor(XLColor.Blue);
|
||||
|
||||
var column = ws.Range("B7:B9").FirstColumn();
|
||||
column.Style.Fill.SetBackgroundColor(XLColor.Green);
|
||||
|
||||
ws.Cell(1, 4)
|
||||
.SetValue("Blue's Row:")
|
||||
.CellRight().SetValue(row.WorksheetRow().RowNumber())
|
||||
.CellBelow().SetValue(column.WorksheetColumn().ColumnLetter())
|
||||
.CellLeft().SetValue("Green's Column:");
|
||||
|
||||
ws.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Ranges
|
||||
{
|
||||
public class DefiningRanges : IXLExample
|
||||
{
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Defining a Range");
|
||||
|
||||
// With a string
|
||||
var range1 = ws.Range("A1:B1");
|
||||
range1.Cell(1, 1).Value = "ws.Range(\"A1:B1\").Merge()";
|
||||
range1.Merge();
|
||||
|
||||
// With two XLAddresses
|
||||
var range2 = ws.Range(ws.Cell(2, 1).Address, ws.Cell(2, 2).Address);
|
||||
range2.Cell(1, 1).Value = "ws.Range(ws.Cell(2, 1).Address, ws.Cell(2, 2).Address).Merge()";
|
||||
range2.Merge();
|
||||
|
||||
// With two strings
|
||||
var range4 = ws.Range("A3", "B3");
|
||||
range4.Cell(1, 1).Value = "ws.Range(\"A3\", \"B3\").Merge()";
|
||||
range4.Merge();
|
||||
|
||||
// With 4 points
|
||||
var range5 = ws.Range(4, 1, 4, 2);
|
||||
range5.Cell(1, 1).Value = "ws.Range(4, 1, 4, 2).Merge()";
|
||||
range5.Merge();
|
||||
|
||||
ws.Column("A").AdjustToContents();
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Ranges
|
||||
{
|
||||
public class DeletingRanges : IXLExample
|
||||
{
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Deleting Ranges");
|
||||
|
||||
// Deleting Columns
|
||||
// Setup test values
|
||||
ws.Columns("1-3, 5, 7").Style.Fill.BackgroundColor = XLColor.Gray;
|
||||
ws.Columns("4, 6").Style.Fill.BackgroundColor = XLColor.GreenPigment;
|
||||
ws.Row(1).Cells("1-3, 5, 7").Value = "FAIL";
|
||||
|
||||
ws.Column(7).Delete();
|
||||
ws.Column(1).Delete();
|
||||
ws.Columns(1,2).Delete();
|
||||
ws.Column(2).Delete();
|
||||
|
||||
// Deleting Rows
|
||||
ws.Rows("1,5,7").Style.Fill.BackgroundColor = XLColor.GreenPigment;
|
||||
ws.Rows("2-4,6, 8").Style.Fill.BackgroundColor = XLColor.Gray;
|
||||
ws.Column(1).Cells("2-4,6, 8").Value = "FAIL";
|
||||
|
||||
ws.Row(8).Delete();
|
||||
ws.Row(2).Delete();
|
||||
ws.Rows(2, 3).Delete();
|
||||
ws.Rows(3, 4).Delete();
|
||||
|
||||
// Deleting Ranges (Shifting Left)
|
||||
var rng1 = ws.Range(2, 2, 8, 8);
|
||||
rng1.Columns("1-3, 5, 7").Style.Fill.BackgroundColor = XLColor.Gray;
|
||||
rng1.Columns("4, 6").Style.Fill.BackgroundColor = XLColor.Orange;
|
||||
rng1.Row(1).Cells("1-3, 5, 7").Value = "FAIL";
|
||||
|
||||
rng1.Column(7).Delete();
|
||||
rng1.Column(1).Delete();
|
||||
rng1.Range(1, 1, rng1.RowCount(), 2).Delete(XLShiftDeletedCells.ShiftCellsLeft);
|
||||
rng1.Column(2).Delete();
|
||||
|
||||
// Deleting Ranges (Shifting Up)
|
||||
rng1.Rows("4, 6").Style.Fill.BackgroundColor = XLColor.Orange;
|
||||
rng1.Rows("1-3, 5, 7").Style.Fill.BackgroundColor = XLColor.Gray;
|
||||
rng1.Column(1).Cells("1-3, 5, 7").Value = "FAIL";
|
||||
|
||||
rng1.Row(7).Delete();
|
||||
rng1.Row(1).Delete();
|
||||
rng1.Range(1, 1, 2, rng1.ColumnCount()).Delete(XLShiftDeletedCells.ShiftCellsUp);
|
||||
rng1.Row(2).Delete();
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class InsertingDeletingColumns : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Inserting and Deleting Columns");
|
||||
|
||||
// Range starts with 2 columns
|
||||
var rng = ws.Range("B2:C3"); // Range starts on B2
|
||||
|
||||
// Insert a column before the range
|
||||
ws.Column(1).InsertColumnsAfter(1); // Range starts on C2
|
||||
|
||||
// Insert a column in between the range
|
||||
ws.Column(3).InsertColumnsAfter(1); // Range now has 3 columns
|
||||
|
||||
// Insert a column (from a range) before the range
|
||||
ws.Range("A2:A3").InsertColumnsAfter(1); // Range starts on D2
|
||||
|
||||
// Insert a column (from a range) in between the range
|
||||
ws.Range("D2:D3").InsertColumnsAfter(1); // Range now has 4 columns
|
||||
|
||||
// Inserting columns from a range not covering all columns
|
||||
// does not affect our defined range
|
||||
ws.Range("A1:A2").InsertColumnsAfter(1);
|
||||
ws.Range("E3:E4").InsertColumnsAfter(1);
|
||||
|
||||
// Delete a column before the range
|
||||
ws.Column(1).Delete(); // Range starts on C2
|
||||
|
||||
// Delete a column (from a range) before the range
|
||||
ws.Range("A2:A3").Delete(XLShiftDeletedCells.ShiftCellsLeft); // Range starts on B2
|
||||
|
||||
// Delete a column in between the range
|
||||
ws.Column(3).Delete(); // Range now has 3 columns
|
||||
|
||||
// Delete a column (from a range) in between the range
|
||||
ws.Range("C2:C3").Delete(XLShiftDeletedCells.ShiftCellsLeft); // Range now has 2 columns
|
||||
|
||||
// Deleting columns from a range not covering all columns
|
||||
// does not affect our defined range
|
||||
ws.Range("A1:A2").Delete(XLShiftDeletedCells.ShiftCellsLeft);
|
||||
ws.Range("D3:D4").Delete(XLShiftDeletedCells.ShiftCellsLeft);
|
||||
|
||||
rng.Style.Fill.BackgroundColor = XLColor.Orange;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class InsertingDeletingRows : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Inserting and Deleting Rows");
|
||||
|
||||
// Range starts with 2 rows
|
||||
var rng = ws.Range("B2:C3");
|
||||
|
||||
// Insert a row above the range
|
||||
ws.Row(1).InsertRowsBelow(1); // Range starts on B3
|
||||
|
||||
// Insert a row in between the range
|
||||
ws.Row(3).InsertRowsBelow(1); // Range now has 3 rows
|
||||
|
||||
// Insert a row (from a range) above the range
|
||||
ws.Range("B1:C1").InsertRowsBelow(1); // Range starts on B4
|
||||
|
||||
// Insert a row (from a range) in between the range
|
||||
ws.Range("B4:C4").InsertRowsBelow(1); // Range now has 4 rows
|
||||
|
||||
// Inserting rows from a range not covering all columns
|
||||
// does not affect our defined range
|
||||
ws.Range("A1:B1").InsertRowsBelow(1);
|
||||
ws.Range("C4:D4").InsertRowsBelow(1);
|
||||
|
||||
// Delete a row above the range
|
||||
ws.Row(1).Delete(); // Range starts on B3
|
||||
|
||||
// Delete a row (from a range) above the range
|
||||
ws.Range("B1:C1").Delete(XLShiftDeletedCells.ShiftCellsUp); // Range starts on B2
|
||||
|
||||
// Delete a row in between the range
|
||||
ws.Row(3).Delete(); // Range now has 3 rows
|
||||
|
||||
// Delete a row (from a range) in between the range
|
||||
ws.Range("B3:C3").Delete(XLShiftDeletedCells.ShiftCellsUp); // Range now has 2 rows
|
||||
|
||||
// Deleting rows from a range not covering all columns
|
||||
// does not affect our defined range
|
||||
ws.Range("A1:B1").Delete(XLShiftDeletedCells.ShiftCellsUp);
|
||||
ws.Range("C4:D4").Delete(XLShiftDeletedCells.ShiftCellsUp);
|
||||
|
||||
rng.Style.Fill.BackgroundColor = XLColor.Orange;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class MultipleRanges : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Multiple Ranges");
|
||||
|
||||
// using multiple string range definitions
|
||||
ws.Ranges("A1:B2,C3:D4,E5:F6").Style.Fill.BackgroundColor = XLColor.Red;
|
||||
|
||||
// using a single string separated by commas
|
||||
ws.Ranges("A5:B6,E1:F2").Style.Fill.BackgroundColor = XLColor.Orange;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class NamedRanges : IXLExample
|
||||
{
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
var wsPresentation = wb.Worksheets.Add("Presentation");
|
||||
var wsData = wb.Worksheets.Add("Data");
|
||||
|
||||
// Fill up some data
|
||||
wsData.Cell(1, 1).Value = "Name";
|
||||
wsData.Cell(1, 2).Value = "Age";
|
||||
wsData.Cell(2, 1).Value = "Tom";
|
||||
wsData.Cell(2, 2).Value = 30;
|
||||
wsData.Cell(3, 1).Value = "Dick";
|
||||
wsData.Cell(3, 2).Value = 25;
|
||||
wsData.Cell(4, 1).Value = "Harry";
|
||||
wsData.Cell(4, 2).Value = 29;
|
||||
|
||||
// Create a named range with the data:
|
||||
wsData.Range("A2:B4").AddToNamed("PeopleData"); // Default named range scope is Workbook
|
||||
|
||||
// Create a hidden named range
|
||||
wb.NamedRanges.Add("Headers", wsData.Range("A1:B1")).Visible = false;
|
||||
|
||||
// Create a hidden named range n worksheet scope
|
||||
wsData.NamedRanges.Add("HeadersAndData", wsData.Range("A1:B4")).Visible = false;
|
||||
|
||||
// Let's use the named range in a formula:
|
||||
wsPresentation.Cell(1, 1).Value = "People Count:";
|
||||
wsPresentation.Cell(1, 2).FormulaA1 = "COUNT(PeopleData)";
|
||||
|
||||
// Create a named range with worksheet scope:
|
||||
wsPresentation.Range("B1").AddToNamed("PeopleCount", XLScope.Worksheet);
|
||||
|
||||
// Let's use the named range:
|
||||
wsPresentation.Cell(2, 1).Value = "Total:";
|
||||
wsPresentation.Cell(2, 2).FormulaA1 = "PeopleCount";
|
||||
|
||||
// Copy the data in a named range:
|
||||
wsPresentation.Cell(4, 1).Value = "People Data:";
|
||||
wsPresentation.Cell(5, 1).Value = wb.Range("PeopleData");
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// For the Excel geeks out there who actually know about
|
||||
// named ranges with relative addresses, you can
|
||||
// create such a thing with the following methods:
|
||||
|
||||
// The following creates a relative named range pointing to the same row
|
||||
// and one column to the right. For example if the current cell is B4
|
||||
// relativeRange1 will point to C4.
|
||||
wsPresentation.NamedRanges.Add("relativeRange1", "Presentation!B1");
|
||||
|
||||
// The following creates a ralative named range pointing to the same row
|
||||
// and one column to the left. For example if the current cell is D2
|
||||
// relativeRange2 will point to C2.
|
||||
wb.NamedRanges.Add("relativeRange2", "Presentation!XFD1");
|
||||
|
||||
// Explanation: The address of a relative range always starts at A1
|
||||
// and moves from then on. To get the desired relative range just
|
||||
// add or subtract the required rows and/or columns from A1.
|
||||
// Column -1 = XFD, Column -2 = XFC, etc.
|
||||
// Row -1 = 1048576, Row -2 = 1048575, etc.
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
|
||||
wsData.Columns().AdjustToContents();
|
||||
wsPresentation.Columns().AdjustToContents();
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
#endregion Methods
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Ranges
|
||||
{
|
||||
public class SelectingRanges : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
var wsActiveCell = wb.AddWorksheet("Set Active Cell");
|
||||
wsActiveCell.Cell("B2").SetActive();
|
||||
|
||||
var wsSelectRowsColumns = wb.AddWorksheet("Select Rows and Columns");
|
||||
wsSelectRowsColumns.Rows("2, 4-5").Select();
|
||||
wsSelectRowsColumns.Columns("2, 4-5").Select();
|
||||
|
||||
var wsSelectMisc = wb.AddWorksheet("Select Misc");
|
||||
wsSelectMisc.Cell("B2").Select();
|
||||
wsSelectMisc.Range("D2:E2").Select();
|
||||
wsSelectMisc.Ranges("C3, D4:E5").Select();
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System.IO;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class ShiftingRanges : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
string tempFile = ExampleHelper.GetTempFilePath(filePath);
|
||||
try
|
||||
{
|
||||
new BasicTable().Create(tempFile);
|
||||
var workbook = new XLWorkbook(tempFile);
|
||||
var ws = workbook.Worksheet(1);
|
||||
|
||||
// Get a range object
|
||||
var rngHeaders = ws.Range("B3:F3");
|
||||
|
||||
// Insert some rows/columns before the range
|
||||
ws.Row(1).InsertRowsAbove(2);
|
||||
ws.Column(1).InsertColumnsBefore(2);
|
||||
|
||||
// Change the background color of the headers
|
||||
rngHeaders.Style.Fill.BackgroundColor = XLColor.LightSalmon;
|
||||
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(tempFile))
|
||||
{
|
||||
File.Delete(tempFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class SortExample : IXLExample
|
||||
{
|
||||
#region Variables
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
|
||||
#region Sort a table
|
||||
var wsTable = wb.Worksheets.Add("Table");
|
||||
AddTestTable(wsTable);
|
||||
var header = wsTable.Row(1).InsertRowsAbove(1).First();
|
||||
for(Int32 co = 1; co <= wsTable.LastColumnUsed().ColumnNumber(); co++)
|
||||
{
|
||||
header.Cell(co).Value = "Column" + co.ToString();
|
||||
}
|
||||
var rangeTable = wsTable.RangeUsed();
|
||||
var table = rangeTable.CopyTo(wsTable.Column(wsTable.LastColumnUsed().ColumnNumber() + 3)).CreateTable();
|
||||
|
||||
table.Sort("Column2, Column3 Desc, Column1 ASC");
|
||||
|
||||
wsTable.Row(1).InsertRowsAbove(2);
|
||||
wsTable.Cell(1, 1)
|
||||
.SetValue(".Sort(\"Column2, Column3 Desc, Column1 ASC\") = Sort table Top to Bottom, Col 2 Asc, Col 3 Desc, Col 1 Asc, Ignore Blanks, Ignore Case")
|
||||
.Style.Font.SetBold();
|
||||
#endregion
|
||||
|
||||
#region Sort a simple range left to right
|
||||
var wsLeftToRight = wb.Worksheets.Add("Sort Left to Right");
|
||||
AddTestTable(wsLeftToRight);
|
||||
wsLeftToRight.RangeUsed().Transpose(XLTransposeOptions.MoveCells);
|
||||
var rangeLeftToRight = wsLeftToRight.RangeUsed();
|
||||
var copyLeftToRight = rangeLeftToRight.CopyTo(wsLeftToRight.Row(wsLeftToRight.LastRowUsed().RowNumber() + 3));
|
||||
|
||||
copyLeftToRight.SortLeftToRight();
|
||||
|
||||
wsLeftToRight.Row(1).InsertRowsAbove(2);
|
||||
wsLeftToRight.Cell(1, 1)
|
||||
.SetValue(".SortLeftToRight() = Sort Range Left to Right, Ascendingly, Ignore Blanks, Ignore Case")
|
||||
.Style.Font.SetBold();
|
||||
#endregion
|
||||
|
||||
#region Sort a range
|
||||
var wsComplex2 = wb.Worksheets.Add("Complex 2");
|
||||
AddTestTable(wsComplex2);
|
||||
var rangeComplex2 = wsComplex2.RangeUsed();
|
||||
var copyComplex2 = rangeComplex2.CopyTo(wsComplex2.Column(wsComplex2.LastColumnUsed().ColumnNumber() + 3));
|
||||
|
||||
copyComplex2.SortColumns.Add(1, XLSortOrder.Ascending, false, true);
|
||||
copyComplex2.SortColumns.Add(3, XLSortOrder.Descending);
|
||||
copyComplex2.Sort();
|
||||
|
||||
wsComplex2.Row(1).InsertRowsAbove(4);
|
||||
wsComplex2.Cell(1, 1)
|
||||
.SetValue(".SortColumns.Add(1, XLSortOrder.Ascending, false, true) = Sort Col 1 Asc, Match Blanks, Match Case").Style.Font.SetBold();
|
||||
wsComplex2.Cell(2, 1)
|
||||
.SetValue(".SortColumns.Add(3, XLSortOrder.Descending) = Sort Col 3 Desc, Ignore Blanks, Ignore Case").Style.Font.SetBold();
|
||||
wsComplex2.Cell(3, 1)
|
||||
.SetValue(".Sort() = Sort range using the parameters defined in SortColumns").Style.Font.SetBold();
|
||||
#endregion
|
||||
|
||||
#region Sort a range
|
||||
var wsComplex1 = wb.Worksheets.Add("Complex 1");
|
||||
AddTestTable(wsComplex1);
|
||||
var rangeComplex1 = wsComplex1.RangeUsed();
|
||||
var copyComplex1 = rangeComplex1.CopyTo(wsComplex1.Column(wsComplex1.LastColumnUsed().ColumnNumber() + 3));
|
||||
|
||||
copyComplex1.Sort("2, 1 DESC", XLSortOrder.Ascending, true);
|
||||
|
||||
wsComplex1.Row(1).InsertRowsAbove(2);
|
||||
wsComplex1.Cell(1, 1)
|
||||
.SetValue(".Sort(\"2, 1 DESC\", XLSortOrder.Ascending, true) = Sort Range Top to Bottom, Col 2 Asc, Col 1 Desc, Ignore Blanks, Match Case").Style.Font.SetBold();
|
||||
#endregion
|
||||
|
||||
#region Sort a simple column
|
||||
var wsSimpleColumn = wb.Worksheets.Add("Simple Column");
|
||||
AddTestColumn(wsSimpleColumn);
|
||||
var rangeSimpleColumn = wsSimpleColumn.RangeUsed();
|
||||
var copySimpleColumn = rangeSimpleColumn.CopyTo(wsSimpleColumn.Column(wsSimpleColumn.LastColumnUsed().ColumnNumber() + 3));
|
||||
|
||||
copySimpleColumn.FirstColumn().Sort(XLSortOrder.Descending, true);
|
||||
|
||||
wsSimpleColumn.Row(1).InsertRowsAbove(2);
|
||||
wsSimpleColumn.Cell(1, 1)
|
||||
.SetValue(".Sort(XLSortOrder.Descending, true) = Sort Range Top to Bottom, Descendingly, Ignore Blanks, Match Case").Style.Font.SetBold();
|
||||
#endregion
|
||||
|
||||
#region Sort a simple range
|
||||
var wsSimple = wb.Worksheets.Add("Simple");
|
||||
AddTestTable(wsSimple);
|
||||
var rangeSimple = wsSimple.RangeUsed();
|
||||
var copySimple = rangeSimple.CopyTo(wsSimple.Column(wsSimple.LastColumnUsed().ColumnNumber() + 3));
|
||||
|
||||
copySimple.Sort();
|
||||
|
||||
wsSimple.Row(1).InsertRowsAbove(2);
|
||||
wsSimple.Cell(1, 1).SetValue(".Sort() = Sort Range Top to Bottom, Ascendingly, Ignore Blanks, Ignore Case").Style.Font.SetBold();
|
||||
#endregion
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
private void AddTestColumnMixed(IXLWorksheet ws)
|
||||
{
|
||||
ws.Cell("A1").SetValue(new DateTime(2011, 1, 30)).Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("A2").SetValue(1.15).Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("A3").SetValue(new TimeSpan(1, 1, 12, 30)).Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("A4").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("A5").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("A6").SetValue(9).Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("A7").SetValue(new TimeSpan(9, 4, 30)).Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("A8").SetValue(new DateTime(2011, 4, 15)).Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
}
|
||||
private void AddTestColumnNumbers(IXLWorksheet ws)
|
||||
{
|
||||
ws.Cell("A1").SetValue(1.30).Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("A2").SetValue(1.15).Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("A3").SetValue(1230).Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("A4").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("A5").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("A6").SetValue(9).Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("A7").SetValue(4.30).Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("A8").SetValue(4.15).Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
}
|
||||
private void AddTestColumnTimeSpans(IXLWorksheet ws)
|
||||
{
|
||||
ws.Cell("A1").SetValue(new TimeSpan(0, 12, 35, 21)).Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("A2").SetValue(new TimeSpan(45, 1, 15)).Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("A3").SetValue(new TimeSpan(1, 1, 12, 30)).Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("A4").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("A5").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("A6").SetValue(new TimeSpan(0, 12, 15)).Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("A7").SetValue(new TimeSpan(1, 4, 30)).Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("A8").SetValue(new TimeSpan(1, 4, 15)).Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
}
|
||||
private void AddTestColumnDates(IXLWorksheet ws)
|
||||
{
|
||||
ws.Cell("A1").SetValue(new DateTime(2011, 1, 30)).Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("A2").SetValue(new DateTime(2011, 1, 15)).Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("A3").SetValue(new DateTime(2011, 12, 30)).Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("A4").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("A5").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("A6").SetValue(new DateTime(2011, 12, 15)).Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("A7").SetValue(new DateTime(2011, 4, 30)).Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("A8").SetValue(new DateTime(2011, 4, 15)).Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
}
|
||||
private void AddTestColumn(IXLWorksheet ws)
|
||||
{
|
||||
ws.Cell("A1").SetValue("B").Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("A2").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("A3").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("A4").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("A5").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("A6").SetValue("b").Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("A7").SetValue("B").Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("A8").SetValue("c").Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
}
|
||||
private void AddTestTable(IXLWorksheet ws)
|
||||
{
|
||||
ws.Cell("A1").SetValue("B").Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("A2").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("A3").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("A4").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("A5").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("A6").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("A7").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("A8").SetValue("B").Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
|
||||
ws.Cell("B1").SetValue("").Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("B2").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("B3").SetValue("B").Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("B4").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("B5").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("B6").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("B7").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("B8").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
|
||||
ws.Cell("C1").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("C2").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("C3").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("C4").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("C5").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("C6").SetValue("b").Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("C7").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("C8").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
}
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class Sorting : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
|
||||
#region Sort Table
|
||||
|
||||
var wsTable = wb.Worksheets.Add("Table");
|
||||
AddTestTable(wsTable);
|
||||
|
||||
wsTable.Row(1).InsertRowsAbove(1);
|
||||
Int32 lastCo = wsTable.LastColumnUsed().ColumnNumber();
|
||||
for (Int32 co = 1; co <= lastCo; co++)
|
||||
wsTable.Cell(1, co).Value = "Column" + co.ToString();
|
||||
|
||||
var table = wsTable.RangeUsed().AsTable();
|
||||
table.Sort("Column2 Desc, 1, 3 Asc");
|
||||
|
||||
// Sort table another way
|
||||
wsTable = wb.Worksheets.Add("Table2");
|
||||
AddTestTable(wsTable);
|
||||
|
||||
wsTable.Row(1).InsertRowsAbove(1);
|
||||
lastCo = wsTable.LastColumnUsed().ColumnNumber();
|
||||
for (Int32 co = 1; co <= lastCo; co++)
|
||||
wsTable.Cell(1, co).Value = "Column" + co.ToString();
|
||||
|
||||
table = wsTable.RangeUsed().AsTable();
|
||||
table.Sort("Column2", XLSortOrder.Descending, false, true);
|
||||
|
||||
|
||||
#endregion Sort Table
|
||||
|
||||
#region Sort Rows
|
||||
|
||||
var wsRows = wb.Worksheets.Add("Rows");
|
||||
AddTestTable(wsRows);
|
||||
wsRows.Row(1).Sort();
|
||||
wsRows.RangeUsed().Row(2).Sort();
|
||||
wsRows.Rows(3, wsRows.LastRowUsed().RowNumber()).Delete();
|
||||
|
||||
#endregion Sort Rows
|
||||
|
||||
#region Sort Columns
|
||||
|
||||
var wsColumns = wb.Worksheets.Add("Columns");
|
||||
AddTestTable(wsColumns);
|
||||
wsColumns.LastColumnUsed().Delete();
|
||||
wsColumns.Column(1).Sort();
|
||||
wsColumns.RangeUsed().Column(2).Sort();
|
||||
|
||||
#endregion Sort Columns
|
||||
|
||||
#region Sort Mixed
|
||||
|
||||
var wsMixed = wb.Worksheets.Add("Mixed");
|
||||
AddTestColumnMixed(wsMixed);
|
||||
wsMixed.Sort();
|
||||
|
||||
#endregion Sort Mixed
|
||||
|
||||
#region Sort Numbers
|
||||
|
||||
var wsNumbers = wb.Worksheets.Add("Numbers");
|
||||
AddTestColumnNumbers(wsNumbers);
|
||||
wsNumbers.Sort();
|
||||
|
||||
#endregion Sort Numbers
|
||||
|
||||
#region Sort TimeSpans
|
||||
|
||||
var wsTimeSpans = wb.Worksheets.Add("TimeSpans");
|
||||
AddTestColumnTimeSpans(wsTimeSpans);
|
||||
wsTimeSpans.Sort();
|
||||
|
||||
#endregion Sort TimeSpans
|
||||
|
||||
#region Sort Dates
|
||||
|
||||
var wsDates = wb.Worksheets.Add("Dates");
|
||||
AddTestColumnDates(wsDates);
|
||||
wsDates.Sort();
|
||||
|
||||
#endregion Sort Dates
|
||||
|
||||
#region Do Not Ignore Blanks
|
||||
|
||||
var wsIncludeBlanks = wb.Worksheets.Add("Include Blanks");
|
||||
AddTestTable(wsIncludeBlanks);
|
||||
var rangeIncludeBlanks = wsIncludeBlanks;
|
||||
rangeIncludeBlanks.SortColumns.Add(1, XLSortOrder.Ascending, false, true);
|
||||
rangeIncludeBlanks.SortColumns.Add(2, XLSortOrder.Descending, false, true);
|
||||
rangeIncludeBlanks.Sort();
|
||||
|
||||
var wsIncludeBlanksColumn = wb.Worksheets.Add("Include Blanks Column");
|
||||
AddTestColumn(wsIncludeBlanksColumn);
|
||||
var rangeIncludeBlanksColumn = wsIncludeBlanksColumn;
|
||||
rangeIncludeBlanksColumn.SortColumns.Add(1, XLSortOrder.Ascending, false, true);
|
||||
rangeIncludeBlanksColumn.Sort();
|
||||
|
||||
var wsIncludeBlanksColumnDesc = wb.Worksheets.Add("Include Blanks Column Desc");
|
||||
AddTestColumn(wsIncludeBlanksColumnDesc);
|
||||
var rangeIncludeBlanksColumnDesc = wsIncludeBlanksColumnDesc;
|
||||
rangeIncludeBlanksColumnDesc.SortColumns.Add(1, XLSortOrder.Descending, false, true);
|
||||
rangeIncludeBlanksColumnDesc.Sort();
|
||||
|
||||
#endregion Do Not Ignore Blanks
|
||||
|
||||
#region Case Sensitive
|
||||
|
||||
var wsCaseSensitive = wb.Worksheets.Add("Case Sensitive");
|
||||
AddTestTable(wsCaseSensitive);
|
||||
var rangeCaseSensitive = wsCaseSensitive;
|
||||
rangeCaseSensitive.SortColumns.Add(1, XLSortOrder.Ascending, true, true);
|
||||
rangeCaseSensitive.SortColumns.Add(2, XLSortOrder.Descending, true, true);
|
||||
rangeCaseSensitive.Sort();
|
||||
|
||||
var wsCaseSensitiveColumn = wb.Worksheets.Add("Case Sensitive Column");
|
||||
AddTestColumn(wsCaseSensitiveColumn);
|
||||
var rangeCaseSensitiveColumn = wsCaseSensitiveColumn;
|
||||
rangeCaseSensitiveColumn.SortColumns.Add(1, XLSortOrder.Ascending, true, true);
|
||||
rangeCaseSensitiveColumn.Sort();
|
||||
|
||||
var wsCaseSensitiveColumnDesc = wb.Worksheets.Add("Case Sensitive Column Desc");
|
||||
AddTestColumn(wsCaseSensitiveColumnDesc);
|
||||
var rangeCaseSensitiveColumnDesc = wsCaseSensitiveColumnDesc;
|
||||
rangeCaseSensitiveColumnDesc.SortColumns.Add(1, XLSortOrder.Descending, true, true);
|
||||
rangeCaseSensitiveColumnDesc.Sort();
|
||||
|
||||
#endregion Case Sensitive
|
||||
|
||||
#region Simple Sorts
|
||||
|
||||
var wsSimple = wb.Worksheets.Add("Simple");
|
||||
AddTestTable(wsSimple);
|
||||
wsSimple.Sort();
|
||||
|
||||
var wsSimpleDesc = wb.Worksheets.Add("Simple Desc");
|
||||
AddTestTable(wsSimpleDesc);
|
||||
wsSimpleDesc.Sort("", XLSortOrder.Descending);
|
||||
|
||||
var wsSimpleColumns = wb.Worksheets.Add("Simple Columns");
|
||||
AddTestTable(wsSimpleColumns);
|
||||
wsSimpleColumns.Sort("2, A DESC, 3");
|
||||
|
||||
var wsSimpleColumn = wb.Worksheets.Add("Simple Column");
|
||||
AddTestColumn(wsSimpleColumn);
|
||||
wsSimpleColumn.Sort();
|
||||
|
||||
var wsSimpleColumnDesc = wb.Worksheets.Add("Simple Column Desc");
|
||||
AddTestColumn(wsSimpleColumnDesc);
|
||||
wsSimpleColumnDesc.Sort(1, XLSortOrder.Descending);
|
||||
|
||||
#endregion Simple Sorts
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddTestColumnMixed(IXLWorksheet ws)
|
||||
{
|
||||
ws.Cell("A1").SetValue(new DateTime(2011, 1, 30)).Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("A2").SetValue(1.15).Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("A3").SetValue(new TimeSpan(1, 1, 12, 30)).Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("A4").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("A5").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("A6").SetValue(9).Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("A7").SetValue(new TimeSpan(9, 4, 30)).Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("A8").SetValue(new DateTime(2011, 4, 15)).Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
}
|
||||
|
||||
private void AddTestColumnNumbers(IXLWorksheet ws)
|
||||
{
|
||||
ws.Cell("A1").SetValue(1.30).Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("A2").SetValue(1.15).Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("A3").SetValue(1230).Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("A4").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("A5").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("A6").SetValue(9).Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("A7").SetValue(4.30).Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("A8").SetValue(4.15).Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
}
|
||||
|
||||
private void AddTestColumnTimeSpans(IXLWorksheet ws)
|
||||
{
|
||||
ws.Cell("A1").SetValue(new TimeSpan(0, 12, 35, 21)).Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("A2").SetValue(new TimeSpan(45, 1, 15)).Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("A3").SetValue(new TimeSpan(1, 1, 12, 30)).Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("A4").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("A5").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("A6").SetValue(new TimeSpan(0, 12, 15)).Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("A7").SetValue(new TimeSpan(1, 4, 30)).Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("A8").SetValue(new TimeSpan(1, 4, 15)).Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
}
|
||||
|
||||
private void AddTestColumnDates(IXLWorksheet ws)
|
||||
{
|
||||
ws.Cell("A1").SetValue(new DateTime(2011, 1, 30)).Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("A2").SetValue(new DateTime(2011, 1, 15)).Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("A3").SetValue(new DateTime(2011, 12, 30)).Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("A4").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("A5").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("A6").SetValue(new DateTime(2011, 12, 15)).Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("A7").SetValue(new DateTime(2011, 4, 30)).Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("A8").SetValue(new DateTime(2011, 4, 15)).Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
}
|
||||
|
||||
private void AddTestColumn(IXLWorksheet ws)
|
||||
{
|
||||
ws.Cell("A1").SetValue("B").Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("A2").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("A3").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("A4").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("A5").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("A6").SetValue("b").Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("A7").SetValue("B").Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("A8").SetValue("c").Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
}
|
||||
|
||||
private void AddTestTable(IXLWorksheet ws)
|
||||
{
|
||||
ws.Cell("A1").SetValue("B").Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("A2").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("A3").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("A4").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("A5").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("A6").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("A7").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("A8").SetValue("B").Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
|
||||
ws.Cell("B1").SetValue("").Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("B2").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("B3").SetValue("B").Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("B4").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("B5").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("B6").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("B7").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("B8").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
|
||||
ws.Cell("C1").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.LightGreen);
|
||||
ws.Cell("C2").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DarkTurquoise);
|
||||
ws.Cell("C3").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.BurlyWood);
|
||||
ws.Cell("C4").SetValue("a").Style.Fill.SetBackgroundColor(XLColor.DarkGray);
|
||||
ws.Cell("C5").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.DarkSalmon);
|
||||
ws.Cell("C6").SetValue("b").Style.Fill.SetBackgroundColor(XLColor.DodgerBlue);
|
||||
ws.Cell("C7").SetValue("A").Style.Fill.SetBackgroundColor(XLColor.IndianRed);
|
||||
ws.Cell("C8").SetValue("").Style.Fill.SetBackgroundColor(XLColor.DeepPink);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System.IO;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class TransposeRanges:IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
string tempFile = ExampleHelper.GetTempFilePath(filePath);
|
||||
try
|
||||
{
|
||||
new BasicTable().Create(tempFile);
|
||||
var workbook = new XLWorkbook(tempFile);
|
||||
|
||||
var ws = workbook.Worksheet(1);
|
||||
|
||||
var rngTable = ws.Range("B2:F6");
|
||||
|
||||
rngTable.Transpose(XLTransposeOptions.MoveCells);
|
||||
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(tempFile))
|
||||
{
|
||||
File.Delete(tempFile);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.IO;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class TransposeRangesPlus : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
string tempFile = ExampleHelper.GetTempFilePath(filePath);
|
||||
try
|
||||
{
|
||||
new BasicTable().Create(tempFile);
|
||||
var workbook = new XLWorkbook(tempFile);
|
||||
|
||||
var ws = workbook.Worksheet(1);
|
||||
|
||||
var rngTable = ws.Range("B2:F6");
|
||||
|
||||
rngTable.Row(rngTable.RowCount() - 1).Delete(XLShiftDeletedCells.ShiftCellsUp);
|
||||
|
||||
// Place some markers
|
||||
var cellNextRow = ws.Cell(rngTable.RangeAddress.LastAddress.RowNumber + 1, rngTable.RangeAddress.LastAddress.ColumnNumber);
|
||||
cellNextRow.Value = "ColumnRight Row";
|
||||
var cellNextColumn = ws.Cell(rngTable.RangeAddress.LastAddress.RowNumber, rngTable.RangeAddress.LastAddress.ColumnNumber + 1);
|
||||
cellNextColumn.Value = "ColumnRight Column";
|
||||
|
||||
rngTable.Transpose(XLTransposeOptions.MoveCells);
|
||||
rngTable.Transpose(XLTransposeOptions.MoveCells);
|
||||
rngTable.Transpose(XLTransposeOptions.ReplaceCells);
|
||||
rngTable.Transpose(XLTransposeOptions.ReplaceCells);
|
||||
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(tempFile))
|
||||
{
|
||||
File.Delete(tempFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Ranges
|
||||
{
|
||||
public class WalkingRanges : IXLExample
|
||||
{
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
var ws = wb.Worksheets.Add("Walking Cells");
|
||||
|
||||
var cell = ws.Cell(5, 5).SetValue("(5,5)");
|
||||
|
||||
cell.CellAbove().SetValue("(4,5)").Style.Fill.SetBackgroundColor(XLColor.LightSalmon);
|
||||
cell.CellAbove(2).SetValue("(3,5)").Style.Fill.SetBackgroundColor(XLColor.LightSalmon);
|
||||
cell.CellBelow().SetValue("(6,5)").Style.Fill.SetBackgroundColor(XLColor.Salmon);
|
||||
cell.CellBelow(2).SetValue("(7,5)").Style.Fill.SetBackgroundColor(XLColor.Salmon);
|
||||
|
||||
cell.CellLeft().SetValue("(5,4)").Style.Fill.SetBackgroundColor(XLColor.LightBlue);
|
||||
cell.CellLeft(2).SetValue("(5,3)").Style.Fill.SetBackgroundColor(XLColor.LightBlue);
|
||||
cell.CellRight().SetValue("(5,6)").Style.Fill.SetBackgroundColor(XLColor.BlueBell);
|
||||
cell.CellRight(2).SetValue("(5,7)").Style.Fill.SetBackgroundColor(XLColor.BlueBell);
|
||||
|
||||
var wsWalkRows = wb.Worksheets.Add("Walking rows");
|
||||
|
||||
var row = wsWalkRows.Row(3);
|
||||
row.RowAbove().Style.Fill.SetBackgroundColor(XLColor.Salmon);
|
||||
row.RowAbove(2).Style.Fill.SetBackgroundColor(XLColor.LightSalmon);
|
||||
row.RowBelow().Style.Fill.SetBackgroundColor(XLColor.Blue);
|
||||
row.RowBelow(2).Style.Fill.SetBackgroundColor(XLColor.BlueBell);
|
||||
|
||||
var rangeRow = wsWalkRows.Range("B8:D12").Row(3);
|
||||
rangeRow.RowAbove().Style.Fill.SetBackgroundColor(XLColor.Salmon);
|
||||
rangeRow.RowAbove(2).Style.Fill.SetBackgroundColor(XLColor.LightSalmon);
|
||||
rangeRow.RowBelow().Style.Fill.SetBackgroundColor(XLColor.Blue);
|
||||
rangeRow.RowBelow(2).Style.Fill.SetBackgroundColor(XLColor.BlueBell);
|
||||
|
||||
var wsWalkColumns = wb.Worksheets.Add("Walking columns");
|
||||
|
||||
var column = wsWalkColumns.Column(3);
|
||||
column.ColumnLeft().Style.Fill.SetBackgroundColor(XLColor.Salmon);
|
||||
column.ColumnLeft(2).Style.Fill.SetBackgroundColor(XLColor.LightSalmon);
|
||||
column.ColumnRight().Style.Fill.SetBackgroundColor(XLColor.Blue);
|
||||
column.ColumnRight(2).Style.Fill.SetBackgroundColor(XLColor.BlueBell);
|
||||
|
||||
var rangeColumn = wsWalkColumns.Range("H2:L4").Column(3);
|
||||
rangeColumn.ColumnLeft().Style.Fill.SetBackgroundColor(XLColor.Salmon);
|
||||
rangeColumn.ColumnLeft(2).Style.Fill.SetBackgroundColor(XLColor.LightSalmon);
|
||||
rangeColumn.ColumnRight().Style.Fill.SetBackgroundColor(XLColor.Blue);
|
||||
rangeColumn.ColumnRight(2).Style.Fill.SetBackgroundColor(XLColor.BlueBell);
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user