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,139 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class CustomAutoFilter : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
IXLWorksheet ws;
|
||||
|
||||
#region Single Column Numbers
|
||||
String singleColumnNumbers = "Single Column Numbers";
|
||||
ws = wb.Worksheets.Add(singleColumnNumbers);
|
||||
|
||||
// Add a bunch of numbers to filter
|
||||
ws.Cell("A1").SetValue("Numbers")
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(5)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(4);
|
||||
|
||||
// Add filters
|
||||
ws.RangeUsed().SetAutoFilter().Column(1).EqualTo(3).Or.GreaterThan(4);
|
||||
|
||||
// Sort the filtered list
|
||||
ws.AutoFilter.Sort(1);
|
||||
#endregion
|
||||
|
||||
#region Single Column Strings
|
||||
String singleColumnStrings = "Single Column Strings";
|
||||
ws = wb.Worksheets.Add(singleColumnStrings);
|
||||
|
||||
// Add a bunch of strings to filter
|
||||
ws.Cell("A1").SetValue("Strings")
|
||||
.CellBelow().SetValue("B")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("E")
|
||||
.CellBelow().SetValue("A")
|
||||
.CellBelow().SetValue("D");
|
||||
|
||||
// Add filters
|
||||
ws.RangeUsed().SetAutoFilter().Column(1).Between("B", "D");
|
||||
|
||||
// Sort the filtered list
|
||||
ws.AutoFilter.Sort(1);
|
||||
#endregion
|
||||
|
||||
#region Single Column Mixed
|
||||
String singleColumnMixed = "Single Column Mixed";
|
||||
ws = wb.Worksheets.Add(singleColumnMixed);
|
||||
|
||||
// Add a bunch of items to filter
|
||||
ws.Cell("A1").SetValue("Mixed")
|
||||
.CellBelow().SetValue("B")
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("E")
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(4);
|
||||
|
||||
// Add filters
|
||||
ws.RangeUsed().SetAutoFilter().Column(1).EqualTo(3).Or.EqualTo("C");
|
||||
|
||||
// Sort the filtered list
|
||||
ws.AutoFilter.Sort(1);
|
||||
#endregion
|
||||
|
||||
#region Multi Column
|
||||
String multiColumn = "Multi Column";
|
||||
ws = wb.Worksheets.Add(multiColumn);
|
||||
|
||||
ws.Cell("A1").SetValue("First")
|
||||
.CellBelow().SetValue("B")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("E")
|
||||
.CellBelow().SetValue("A")
|
||||
.CellBelow().SetValue("D");
|
||||
|
||||
ws.Cell("B1").SetValue("Numbers")
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(5)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(4);
|
||||
|
||||
ws.Cell("C1").SetValue("Strings")
|
||||
.CellBelow().SetValue("B")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("E")
|
||||
.CellBelow().SetValue("A")
|
||||
.CellBelow().SetValue("D");
|
||||
|
||||
// Add filters
|
||||
ws.RangeUsed().SetAutoFilter().Column(2).EqualTo(3).Or.GreaterThan(4);
|
||||
ws.RangeUsed().SetAutoFilter().Column(3).Between("B", "D");
|
||||
|
||||
// Sort the filtered list
|
||||
ws.AutoFilter.Sort(3);
|
||||
#endregion
|
||||
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
wb.SaveAs(ms);
|
||||
|
||||
var workbook = new XLWorkbook(ms);
|
||||
|
||||
#region Single Column Numbers
|
||||
workbook.Worksheet(singleColumnNumbers).AutoFilter.Sort(1, XLSortOrder.Descending);
|
||||
#endregion
|
||||
|
||||
#region Single Column Strings
|
||||
workbook.Worksheet(singleColumnStrings).AutoFilter.Sort(1, XLSortOrder.Descending);
|
||||
#endregion
|
||||
|
||||
#region Single Column Mixed
|
||||
workbook.Worksheet(singleColumnMixed).AutoFilter.Column(1).EqualOrGreaterThan("D");
|
||||
workbook.Worksheet(singleColumnMixed).AutoFilter.Sort(1, XLSortOrder.Descending);
|
||||
#endregion
|
||||
|
||||
#region Multi Column
|
||||
workbook.Worksheet(multiColumn).AutoFilter.Column(3).EqualTo("E");
|
||||
workbook.Worksheet(multiColumn).AutoFilter.Sort(3, XLSortOrder.Descending);
|
||||
#endregion
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
ms.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class DateTimeGroupAutoFilter : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
IXLWorksheet ws;
|
||||
|
||||
#region Single Column Dates
|
||||
|
||||
String singleColumnDates = "Single Column Dates";
|
||||
ws = wb.Worksheets.Add(singleColumnDates);
|
||||
|
||||
// Add a bunch of numbers to filter
|
||||
ws.Cell("A1").SetValue("Dates")
|
||||
.CellBelow().SetValue(new DateTime(2018, 1, 1).AddDays(2))
|
||||
.CellBelow().SetValue(new DateTime(2018, 1, 1).AddDays(3))
|
||||
.CellBelow().SetValue(new DateTime(2018, 1, 1).AddDays(3))
|
||||
.CellBelow().SetValue(new DateTime(2018, 1, 1).AddDays(5))
|
||||
.CellBelow().SetValue(new DateTime(2018, 1, 1).AddDays(1))
|
||||
.CellBelow().SetValue(new DateTime(2018, 1, 1).AddDays(4));
|
||||
|
||||
ws.Column(1).Style.NumberFormat.Format = "d MMMM yyyy";
|
||||
|
||||
// Add filters
|
||||
ws.RangeUsed().SetAutoFilter().Column(1).AddDateGroupFilter(new DateTime(2018, 1, 1).AddDays(3), XLDateTimeGrouping.Day);
|
||||
|
||||
// Sort the filtered list
|
||||
ws.AutoFilter.Sort(1);
|
||||
|
||||
#endregion Single Column Dates
|
||||
|
||||
ws.Columns().AdjustToContents();
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class DynamicAutoFilter : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
IXLWorksheet ws;
|
||||
|
||||
#region Single Column Numbers
|
||||
String singleColumnNumbers = "Single Column Numbers";
|
||||
ws = wb.Worksheets.Add(singleColumnNumbers);
|
||||
|
||||
// Add a bunch of numbers to filter
|
||||
ws.Cell("A1").SetValue("Numbers")
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(5)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(4);
|
||||
|
||||
// Add filters
|
||||
ws.RangeUsed().SetAutoFilter().Column(1).AboveAverage();
|
||||
|
||||
// Sort the filtered list
|
||||
//ws.AutoFilter.Sort(1);
|
||||
#endregion
|
||||
|
||||
#region Multi Column
|
||||
String multiColumn = "Multi Column";
|
||||
ws = wb.Worksheets.Add(multiColumn);
|
||||
|
||||
ws.Cell("A1").SetValue("First")
|
||||
.CellBelow().SetValue("B")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("E")
|
||||
.CellBelow().SetValue("A")
|
||||
.CellBelow().SetValue("D");
|
||||
|
||||
ws.Cell("B1").SetValue("Numbers")
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(5)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(4);
|
||||
|
||||
ws.Cell("C1").SetValue("Strings")
|
||||
.CellBelow().SetValue("B")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("E")
|
||||
.CellBelow().SetValue("A")
|
||||
.CellBelow().SetValue("D");
|
||||
|
||||
// Add filters
|
||||
ws.RangeUsed().SetAutoFilter().Column(2).BelowAverage();
|
||||
|
||||
// Sort the filtered list
|
||||
//ws.AutoFilter.Sort(3);
|
||||
#endregion
|
||||
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
wb.SaveAs(ms);
|
||||
|
||||
var workbook = new XLWorkbook(ms);
|
||||
|
||||
#region Single Column Numbers
|
||||
//workbook.Worksheet(singleColumnNumbers).AutoFilter.Sort(1, XLSortOrder.Descending);
|
||||
#endregion
|
||||
|
||||
#region Multi Column
|
||||
//workbook.Worksheet(multiColumn).AutoFilter.Sort(3, XLSortOrder.Descending);
|
||||
#endregion
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
ms.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class RegularAutoFilter : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
IXLWorksheet ws;
|
||||
|
||||
#region Single Column Numbers
|
||||
|
||||
String singleColumnNumbers = "Single Column Numbers";
|
||||
ws = wb.Worksheets.Add(singleColumnNumbers);
|
||||
|
||||
// Add a bunch of numbers to filter
|
||||
ws.Cell("A1").SetValue("Numbers")
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(5)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(4)
|
||||
.CellBelow().SetValue(5);
|
||||
|
||||
ws.Cell("B1").SetValue("Names")
|
||||
.CellBelow().SetValue("John")
|
||||
.CellBelow().SetValue("Jack")
|
||||
.CellBelow().SetValue("Neil")
|
||||
.CellBelow().SetValue("Alex")
|
||||
.CellBelow().SetValue("Jason")
|
||||
.CellBelow().SetValue("Patrick")
|
||||
.CellBelow().SetValue("Jacques");
|
||||
|
||||
// Add filters
|
||||
var autoFilter = ws.RangeUsed().SetAutoFilter();
|
||||
autoFilter.Column(1).AddFilter(3)
|
||||
.AddFilter(1);
|
||||
|
||||
autoFilter.Column(2).BeginsWith("J");
|
||||
|
||||
// Sort the filtered list
|
||||
ws.AutoFilter.Sort(1);
|
||||
|
||||
#endregion Single Column Numbers
|
||||
|
||||
#region Single Column Strings
|
||||
|
||||
String singleColumnStrings = "Single Column Strings";
|
||||
ws = wb.Worksheets.Add(singleColumnStrings);
|
||||
|
||||
// Add a bunch of strings to filter
|
||||
ws.Cell("A1").SetValue("Strings")
|
||||
.CellBelow().SetValue("B")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("E")
|
||||
.CellBelow().SetValue("A")
|
||||
.CellBelow().SetValue("D");
|
||||
|
||||
// Add filters
|
||||
ws.RangeUsed().SetAutoFilter().Column(1).AddFilter("C")
|
||||
.AddFilter("A");
|
||||
|
||||
// Sort the filtered list
|
||||
ws.AutoFilter.Sort(1);
|
||||
|
||||
#endregion Single Column Strings
|
||||
|
||||
#region Single Column Mixed
|
||||
|
||||
String singleColumnMixed = "Single Column Mixed";
|
||||
ws = wb.Worksheets.Add(singleColumnMixed);
|
||||
|
||||
// Add a bunch of items to filter
|
||||
ws.Cell("A1").SetValue("Mixed")
|
||||
.CellBelow().SetValue("B")
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("E")
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(4);
|
||||
|
||||
// Add filters
|
||||
ws.RangeUsed().SetAutoFilter().Column(1).AddFilter("C")
|
||||
.AddFilter(1);
|
||||
|
||||
// Sort the filtered list
|
||||
ws.AutoFilter.Sort(1);
|
||||
|
||||
#endregion Single Column Mixed
|
||||
|
||||
#region Multi Column
|
||||
|
||||
String multiColumn = "Multi Column";
|
||||
ws = wb.Worksheets.Add(multiColumn);
|
||||
|
||||
ws.Cell("A1").SetValue("First")
|
||||
.CellBelow().SetValue("B")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("E")
|
||||
.CellBelow().SetValue("A")
|
||||
.CellBelow().SetValue("D");
|
||||
|
||||
ws.Cell("B1").SetValue("Numbers")
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(5)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(4);
|
||||
|
||||
ws.Cell("C1").SetValue("Strings")
|
||||
.CellBelow().SetValue("B")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("E")
|
||||
.CellBelow().SetValue("A")
|
||||
.CellBelow().SetValue("D");
|
||||
|
||||
// Add filters
|
||||
ws.RangeUsed().SetAutoFilter().Column(2).AddFilter(3)
|
||||
.AddFilter(1);
|
||||
|
||||
// Sort the filtered list
|
||||
ws.AutoFilter.Sort(3);
|
||||
|
||||
#endregion Multi Column
|
||||
|
||||
#region Table
|
||||
|
||||
String tableSheetName = "Table";
|
||||
ws = wb.Worksheets.Add(tableSheetName);
|
||||
|
||||
// Add a bunch of numbers to filter
|
||||
ws.Cell("A1").SetValue("Numbers")
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(5)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(4);
|
||||
|
||||
// Add filters
|
||||
var table = ws.RangeUsed().CreateTable();
|
||||
table.ShowTotalsRow = true;
|
||||
table.Field(0).TotalsRowFunction = XLTotalsRowFunction.Sum;
|
||||
table.AutoFilter.Column(1).AddFilter(3).AddFilter(4);
|
||||
|
||||
table.AutoFilter.Sort(1);
|
||||
|
||||
#endregion Table
|
||||
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
wb.SaveAs(ms);
|
||||
|
||||
var workbook = new XLWorkbook(ms);
|
||||
|
||||
#region Single Column Numbers
|
||||
|
||||
workbook.Worksheet(singleColumnNumbers).AutoFilter.Column(1).AddFilter(5);
|
||||
workbook.Worksheet(singleColumnNumbers).AutoFilter.Sort(1, XLSortOrder.Descending);
|
||||
|
||||
#endregion Single Column Numbers
|
||||
|
||||
#region Single Column Strings
|
||||
|
||||
workbook.Worksheet(singleColumnStrings).AutoFilter.Column(1).AddFilter("E");
|
||||
workbook.Worksheet(singleColumnStrings).AutoFilter.Sort(1, XLSortOrder.Descending);
|
||||
|
||||
#endregion Single Column Strings
|
||||
|
||||
#region Single Column Mixed
|
||||
|
||||
workbook.Worksheet(singleColumnMixed).AutoFilter.Column(1).AddFilter("E");
|
||||
workbook.Worksheet(singleColumnMixed).AutoFilter.Column(1).AddFilter(3);
|
||||
workbook.Worksheet(singleColumnMixed).AutoFilter.Sort(1, XLSortOrder.Descending);
|
||||
|
||||
#endregion Single Column Mixed
|
||||
|
||||
#region Multi Column
|
||||
|
||||
workbook.Worksheet(multiColumn).AutoFilter.Column(3).AddFilter("C");
|
||||
workbook.Worksheet(multiColumn).AutoFilter.Sort(3, XLSortOrder.Descending);
|
||||
|
||||
#endregion Multi Column
|
||||
|
||||
#region Table
|
||||
|
||||
workbook.Worksheet(tableSheetName).Table(0).AutoFilter.Column(1).AddFilter(5);
|
||||
workbook.Worksheet(tableSheetName).Table(0).AutoFilter.Sort(1, XLSortOrder.Descending);
|
||||
|
||||
#endregion Table
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
ms.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class TopBottomAutoFilter : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
IXLWorksheet ws;
|
||||
|
||||
#region Single Column Numbers
|
||||
String singleColumnNumbers = "Single Column Numbers";
|
||||
ws = wb.Worksheets.Add(singleColumnNumbers);
|
||||
|
||||
// Add a bunch of numbers to filter
|
||||
ws.Cell("A1").SetValue("Numbers")
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(5)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(4);
|
||||
|
||||
// Add filters
|
||||
ws.RangeUsed().SetAutoFilter().Column(1).Top(2);
|
||||
|
||||
// Sort the filtered list
|
||||
//ws.AutoFilter.Sort(1);
|
||||
#endregion
|
||||
|
||||
#region Multi Column
|
||||
String multiColumn = "Multi Column";
|
||||
ws = wb.Worksheets.Add(multiColumn);
|
||||
|
||||
ws.Cell("A1").SetValue("First")
|
||||
.CellBelow().SetValue("B")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("E")
|
||||
.CellBelow().SetValue("A")
|
||||
.CellBelow().SetValue("D");
|
||||
|
||||
ws.Cell("B1").SetValue("Numbers")
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(5)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(4);
|
||||
|
||||
ws.Cell("C1").SetValue("Strings")
|
||||
.CellBelow().SetValue("B")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("C")
|
||||
.CellBelow().SetValue("E")
|
||||
.CellBelow().SetValue("A")
|
||||
.CellBelow().SetValue("D");
|
||||
|
||||
// Add filters
|
||||
ws.RangeUsed().SetAutoFilter().Column(2).Bottom(50, XLTopBottomType.Percent);
|
||||
|
||||
// Sort the filtered list
|
||||
//ws.AutoFilter.Sort(3);
|
||||
#endregion
|
||||
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
wb.SaveAs(ms);
|
||||
|
||||
var workbook = new XLWorkbook(ms);
|
||||
|
||||
#region Single Column Numbers
|
||||
//workbook.Worksheet(singleColumnNumbers).AutoFilter.Sort(1, XLSortOrder.Descending);
|
||||
#endregion
|
||||
|
||||
#region Multi Column
|
||||
//workbook.Worksheet(multiColumn).AutoFilter.Sort(3, XLSortOrder.Descending);
|
||||
#endregion
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
ms.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class BasicTable : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
// Creating a new workbook
|
||||
var wb = new XLWorkbook();
|
||||
|
||||
//Adding a worksheet
|
||||
var ws = wb.Worksheets.Add("Contacts");
|
||||
|
||||
//Adding text
|
||||
//Title
|
||||
ws.Cell("B2").Value = "Contacts";
|
||||
//First Names
|
||||
ws.Cell("B3").Value = "FName";
|
||||
ws.Cell("B4").Value = "John";
|
||||
ws.Cell("B5").Value = "Hank";
|
||||
ws.Cell("B6").Value = "Dagny";
|
||||
//Last Names
|
||||
ws.Cell("C3").Value = "LName";
|
||||
ws.Cell("C4").Value = "Galt";
|
||||
ws.Cell("C5").Value = "Rearden";
|
||||
ws.Cell("C6").Value = "Taggart";
|
||||
|
||||
//Adding more data types
|
||||
//Is an outcast?
|
||||
ws.Cell("D3").Value = "Outcast";
|
||||
ws.Cell("D4").Value = true;
|
||||
ws.Cell("D5").Value = false;
|
||||
ws.Cell("D6").Value = false;
|
||||
//Date of Birth
|
||||
ws.Cell("E3").Value = "DOB";
|
||||
ws.Cell("E4").Value = new DateTime(1919, 1, 21);
|
||||
ws.Cell("E5").Value = new DateTime(1907, 3, 4);
|
||||
ws.Cell("E6").Value = new DateTime(1921, 12, 15);
|
||||
//Income
|
||||
ws.Cell("F3").Value = "Income";
|
||||
ws.Cell("F4").Value = 2000;
|
||||
ws.Cell("F5").Value = 40000;
|
||||
ws.Cell("F6").Value = 10000;
|
||||
|
||||
//Defining ranges
|
||||
//From worksheet
|
||||
var rngTable = ws.Range("B2:F6");
|
||||
//From another range
|
||||
var rngDates = rngTable.Range("E4:E6");
|
||||
var rngNumbers = rngTable.Range("F4:F6");
|
||||
|
||||
//Formatting dates and numbers
|
||||
//Using a OpenXML's predefined formats
|
||||
rngDates.Style.NumberFormat.NumberFormatId = 15;
|
||||
//Using a custom format
|
||||
rngNumbers.Style.NumberFormat.Format = "$ #,##0";
|
||||
|
||||
//Formatting headers
|
||||
var rngHeaders = rngTable.Range("B3:F3");
|
||||
rngHeaders.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
|
||||
rngHeaders.Style.Font.Bold = true;
|
||||
rngHeaders.Style.Fill.BackgroundColor = XLColor.Aqua;
|
||||
|
||||
//Adding grid lines
|
||||
rngTable.Style.Border.BottomBorder = XLBorderStyleValues.Thin;
|
||||
|
||||
//Format title cell
|
||||
rngTable.Cell(1, 1).Style.Font.Bold = true;
|
||||
rngTable.Cell(1, 1).Style.Fill.BackgroundColor = XLColor.CornflowerBlue;
|
||||
rngTable.Cell(1, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
|
||||
|
||||
//Merge title cells
|
||||
rngTable.Row(1).Merge(); // We could've also used: rngTable.Range("A1:E1").Merge()
|
||||
|
||||
//Add thick borders
|
||||
rngTable.Style.Border.OutsideBorder = XLBorderStyleValues.Thick;
|
||||
|
||||
// You can also specify the border for each side with:
|
||||
// rngTable.FirstColumn().Style.Border.LeftBorder = XLBorderStyleValues.Thick;
|
||||
// rngTable.LastColumn().Style.Border.RightBorder = XLBorderStyleValues.Thick;
|
||||
// rngTable.FirstRow().Style.Border.TopBorder = XLBorderStyleValues.Thick;
|
||||
// rngTable.LastRow().Style.Border.BottomBorder = XLBorderStyleValues.Thick;
|
||||
|
||||
// Adjust column widths to their content
|
||||
ws.Columns(2, 6).AdjustToContents();
|
||||
|
||||
//Saving the workbook
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,46 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netcoreapp2.0;net40;net46</TargetFrameworks>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<OutputType>Exe</OutputType>
|
||||
<Version>0.95.4</Version>
|
||||
<NoWarn>$(NoWarn);NU1605</NoWarn>
|
||||
<Configurations>Debug;Release;Release.Signed</Configurations>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Release.Signed'">
|
||||
<OutputPath>bin\Release.Signed\</OutputPath>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
<AssemblyOriginatorKeyFile>ClosedXML.snk</AssemblyOriginatorKeyFile>
|
||||
<DefineConstants>$(DefineConstants);STRONGNAME</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
|
||||
<DefineConstants>$(DefineConstants);_NETSTANDARD_;_NETSTANDARD2_0_</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(TargetFramework)' == 'net40' ">
|
||||
<DefineConstants>$(DefineConstants);_NETFRAMEWORK_;_NET40_</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(TargetFramework)' == 'net46' ">
|
||||
<DefineConstants>$(DefineConstants);_NETFRAMEWORK_;_NET46_</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Resources\*.jpg" />
|
||||
<EmbeddedResource Include="Resources\*.png" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="..\.editorconfig" Link=".editorconfig" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ClosedXML\ClosedXML.csproj" />
|
||||
<PackageReference Include="DocumentFormat.OpenXml" Version="2.7.2" />
|
||||
<PackageReference Include="morelinq" Version="2.10.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class ColumnCells : IXLExample
|
||||
{
|
||||
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Column Cells");
|
||||
|
||||
var columnFromWorksheet = ws.Column(1);
|
||||
columnFromWorksheet.Cell(1).Style.Fill.BackgroundColor = XLColor.Red;
|
||||
columnFromWorksheet.Cells("2").Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
columnFromWorksheet.Cells("3,5:6").Style.Fill.BackgroundColor = XLColor.Red;
|
||||
columnFromWorksheet.Cells(8, 9).Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
|
||||
var columnFromRange = ws.Range("B1:B9").FirstColumn();
|
||||
|
||||
columnFromRange.Cell(1).Style.Fill.BackgroundColor = XLColor.Red;
|
||||
columnFromRange.Cells("2").Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
columnFromRange.Cells("3,5:6").Style.Fill.BackgroundColor = XLColor.Red;
|
||||
columnFromRange.Cells(8, 9).Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Columns
|
||||
{
|
||||
public class ColumnCollection : 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 workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Columns of a Range");
|
||||
|
||||
// All columns in a range
|
||||
ws.Range("A1:B2").Columns().Style.Fill.BackgroundColor = XLColor.DimGray;
|
||||
|
||||
var bigRange = ws.Range("A4:V6");
|
||||
|
||||
// Contiguous columns by number
|
||||
bigRange.Columns(1, 2).Style.Fill.BackgroundColor = XLColor.Red;
|
||||
|
||||
// Contiguous columns by letter
|
||||
bigRange.Columns("D", "E").Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
|
||||
// Contiguous columns by letter
|
||||
bigRange.Columns("G:H").Style.Fill.BackgroundColor = XLColor.DeepPink;
|
||||
|
||||
// Spread columns by number
|
||||
bigRange.Columns("10:11,13:14").Style.Fill.BackgroundColor = XLColor.Orange;
|
||||
|
||||
// Spread columns by letter
|
||||
bigRange.Columns("P:Q,S:T").Style.Fill.BackgroundColor = XLColor.Turquoise;
|
||||
|
||||
// Use a single number/letter
|
||||
bigRange.Columns("V").Style.Fill.BackgroundColor = XLColor.Cyan;
|
||||
|
||||
// Adjust the width
|
||||
ws.Columns("A:V").Width = 3;
|
||||
|
||||
var ws2 = workbook.Worksheets.Add("Columns of a worksheet");
|
||||
|
||||
// Contiguous columns by number
|
||||
ws2.Columns(1, 2).Style.Fill.BackgroundColor = XLColor.Red;
|
||||
|
||||
// Contiguous columns by letter
|
||||
ws2.Columns("D", "E").Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
|
||||
// Contiguous columns by letter
|
||||
ws2.Columns("G:H").Style.Fill.BackgroundColor = XLColor.DeepPink;
|
||||
|
||||
// Spread columns by number
|
||||
ws2.Columns("10:11,13:14").Style.Fill.BackgroundColor = XLColor.Orange;
|
||||
|
||||
// Spread columns by letter
|
||||
ws2.Columns("P:Q,S:T").Style.Fill.BackgroundColor = XLColor.Turquoise;
|
||||
|
||||
// Use a single number/letter
|
||||
ws2.Columns("V").Style.Fill.BackgroundColor = XLColor.Cyan;
|
||||
|
||||
// Adjust the width
|
||||
ws2.Columns("A:V").Width = 3;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Columns
|
||||
{
|
||||
public class ColumnSettings : IXLExample
|
||||
{
|
||||
#region Variables
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
// Public
|
||||
public ColumnSettings()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Private
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Column Settings");
|
||||
|
||||
var col1 = ws.Column("B");
|
||||
col1.Style.Fill.BackgroundColor = XLColor.Red;
|
||||
col1.Width = 20;
|
||||
|
||||
var col2 = ws.Column(4);
|
||||
col2.Style.Fill.BackgroundColor = XLColor.DarkOrange;
|
||||
col2.Width = 5;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class DeletingColumns : 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 workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Deleting Columns");
|
||||
|
||||
var rngTitles = ws.Range("B2:D2");
|
||||
ws.Row(1).InsertRowsBelow(2);
|
||||
|
||||
var rng1 = ws.Range("B2:D2");
|
||||
var rng2 = ws.Range("F2:G2");
|
||||
var rng3 = ws.Range("A1:A3");
|
||||
var col1 = ws.Column(1);
|
||||
|
||||
rng1.Style.Fill.BackgroundColor = XLColor.Orange;
|
||||
rng2.Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
rng3.Style.Fill.BackgroundColor = XLColor.Red;
|
||||
col1.Style.Fill.BackgroundColor = XLColor.Black;
|
||||
|
||||
ws.Columns("A,C,E:H").Delete();
|
||||
ws.Cell("A2").Value = "OK";
|
||||
ws.Cell("B2").Value = "OK";
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace ClosedXML_Examples.Columns
|
||||
{
|
||||
public class InsertColumns : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Inserting Columns");
|
||||
|
||||
// Color the entire spreadsheet using columns
|
||||
ws.Columns().Style.Fill.BackgroundColor = XLColor.LightCyan;
|
||||
|
||||
// Put a value in a few cells
|
||||
foreach (var r in Enumerable.Range(1, 5))
|
||||
foreach (var c in Enumerable.Range(1, 5))
|
||||
ws.Cell(r, c).Value = "X";
|
||||
|
||||
var blueColumn = ws.Column(2);
|
||||
var redColumn = ws.Column(5);
|
||||
|
||||
blueColumn.Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
blueColumn.InsertColumnsAfter(2);
|
||||
|
||||
redColumn.Style.Fill.BackgroundColor = XLColor.Red;
|
||||
redColumn.InsertColumnsBefore(2);
|
||||
|
||||
ws.Rows(3, 4).Style.Fill.BackgroundColor = XLColor.Orange;
|
||||
ws.Range("B1:D1").InsertColumnsAfter(2);
|
||||
ws.Range("B2:D2").InsertColumnsBefore(2);
|
||||
ws.Range("B3:D3").InsertColumnsAfter(2);
|
||||
ws.Range("B4:D4").InsertColumnsBefore(2);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using ClosedXML.Excel;
|
||||
using System.IO;
|
||||
using MoreLinq;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class AddingComments : IXLExample
|
||||
{
|
||||
|
||||
public void Create(string filePath)
|
||||
{
|
||||
var wb = new XLWorkbook {Author = "Manuel"};
|
||||
AddMiscComments(wb);
|
||||
AddVisibilityComments(wb);
|
||||
AddPosition(wb);
|
||||
AddSignatures(wb);
|
||||
AddStyleAlignment(wb);
|
||||
AddColorsAndLines(wb);
|
||||
AddMagins(wb);
|
||||
AddProperties(wb);
|
||||
AddProtection(wb);
|
||||
AddSize(wb);
|
||||
AddWeb(wb);
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
private void AddWeb(XLWorkbook wb)
|
||||
{
|
||||
var ws = wb.Worksheets.Add("Web");
|
||||
ws.Cell("A1").Comment.Style.Web.AlternateText = "The alternate text in case you need it.";
|
||||
}
|
||||
|
||||
private void AddSize(XLWorkbook wb)
|
||||
{
|
||||
var ws = wb.Worksheets.Add("Size");
|
||||
|
||||
// Automatic size is a copy of the property comment.Style.Alignment.AutomaticSize
|
||||
// I created the duplicate because it makes more sense for it to be in Size
|
||||
// but Excel has it under the Alignment tab.
|
||||
ws.Cell("A2").Comment.AddText("Things are very tight around here.");
|
||||
ws.Cell("A2").Comment.Style.Size.SetAutomaticSize();
|
||||
|
||||
ws.Cell("A4").Comment.AddText("Different size");
|
||||
ws.Cell("A4").Comment.Style
|
||||
.Size.SetHeight(30) // The height is set in the same units as row.Height
|
||||
.Size.SetWidth(30); // The width is set in the same units as row.Width
|
||||
|
||||
// Set all comments to visible
|
||||
ws.CellsUsed(XLCellsUsedOptions.All, c => c.HasComment).ForEach(c => c.Comment.SetVisible());
|
||||
}
|
||||
|
||||
private void AddProtection(XLWorkbook wb)
|
||||
{
|
||||
var ws = wb.Worksheets.Add("Protection");
|
||||
|
||||
ws.Cell("A1").Comment.Style
|
||||
.Protection.SetLocked(false)
|
||||
.Protection.SetLockText(false);
|
||||
}
|
||||
|
||||
private void AddProperties(XLWorkbook wb)
|
||||
{
|
||||
var ws = wb.Worksheets.Add("Properties");
|
||||
|
||||
ws.Cell("A1").Comment.Style.Properties.Positioning = XLDrawingAnchor.Absolute;
|
||||
ws.Cell("A2").Comment.Style.Properties.Positioning = XLDrawingAnchor.MoveAndSizeWithCells;
|
||||
ws.Cell("A3").Comment.Style.Properties.Positioning = XLDrawingAnchor.MoveWithCells;
|
||||
}
|
||||
|
||||
private void AddMagins(XLWorkbook wb)
|
||||
{
|
||||
var ws = wb.Worksheets.Add("Margins");
|
||||
|
||||
ws.Cell("A2").Comment
|
||||
.SetVisible()
|
||||
.AddText("Lorem ipsum dolor sit amet, adipiscing elit. ").AddNewLine()
|
||||
.AddText("Nunc elementum, sapien a ultrices, commodo nisl. ").AddNewLine()
|
||||
.AddText("Consequat erat lectus a nisi. Aliquam facilisis.");
|
||||
|
||||
ws.Cell("A2").Comment.Style
|
||||
.Margins.SetAll(0.25)
|
||||
.Size.SetAutomaticSize();
|
||||
}
|
||||
|
||||
private void AddColorsAndLines(XLWorkbook wb)
|
||||
{
|
||||
var ws = wb.Worksheets.Add("Colors and Lines");
|
||||
|
||||
ws.Cell("A2").Comment
|
||||
.AddText("Now ")
|
||||
.AddText("THIS").SetBold().SetFontColor(XLColor.Red)
|
||||
.AddText(" is colorful!");
|
||||
ws.Cell("A2").Comment.Style
|
||||
.ColorsAndLines.SetFillColor(XLColor.RichCarmine)
|
||||
.ColorsAndLines.SetFillTransparency(0.25) // 25% opaque
|
||||
.ColorsAndLines.SetLineColor(XLColor.Blue)
|
||||
.ColorsAndLines.SetLineTransparency(0.75) // 75% opaque
|
||||
.ColorsAndLines.SetLineDash(XLDashStyle.LongDash)
|
||||
.ColorsAndLines.SetLineStyle(XLLineStyle.ThickBetweenThin)
|
||||
.ColorsAndLines.SetLineWeight(7.5);
|
||||
|
||||
// Set all comments to visible
|
||||
ws.CellsUsed(XLCellsUsedOptions.All, c => c.HasComment).ForEach(c => c.Comment.SetVisible());
|
||||
}
|
||||
|
||||
private void AddStyleAlignment(XLWorkbook wb)
|
||||
{
|
||||
var ws = wb.Worksheets.Add("Alignment");
|
||||
|
||||
// Automagically adjust the size of the comment to fit the contents
|
||||
ws.Cell("A1").Comment.Style.Alignment.SetAutomaticSize();
|
||||
ws.Cell("A1").Comment.AddText("Things are pretty tight around here");
|
||||
|
||||
// Default values
|
||||
ws.Cell("A3").Comment
|
||||
.AddText("Default Alignments:").AddNewLine()
|
||||
.AddText("Vertical = Top").AddNewLine()
|
||||
.AddText("Horizontal = Left").AddNewLine()
|
||||
.AddText("Orientation = Left to Right");
|
||||
|
||||
// Let's change the alignments
|
||||
ws.Cell("A8").Comment
|
||||
.AddText("Vertical = Bottom").AddNewLine()
|
||||
.AddText("Horizontal = Right");
|
||||
ws.Cell("A8").Comment.Style
|
||||
.Alignment.SetVertical(XLDrawingVerticalAlignment.Bottom)
|
||||
.Alignment.SetHorizontal(XLDrawingHorizontalAlignment.Right);
|
||||
|
||||
// And now the orientation...
|
||||
ws.Cell("D3").Comment.AddText("Orientation = Bottom to Top");
|
||||
ws.Cell("D3").Comment.Style
|
||||
.Alignment.SetOrientation(XLDrawingTextOrientation.BottomToTop)
|
||||
.Alignment.SetAutomaticSize();
|
||||
|
||||
ws.Cell("E3").Comment.AddText("Orientation = Top to Bottom");
|
||||
ws.Cell("E3").Comment.Style
|
||||
.Alignment.SetOrientation(XLDrawingTextOrientation.TopToBottom)
|
||||
.Alignment.SetAutomaticSize();
|
||||
|
||||
ws.Cell("F3").Comment.AddText("Orientation = Vertical");
|
||||
ws.Cell("F3").Comment.Style
|
||||
.Alignment.SetOrientation(XLDrawingTextOrientation.Vertical)
|
||||
.Alignment.SetAutomaticSize();
|
||||
|
||||
|
||||
// Set all comments to visible
|
||||
ws.CellsUsed(XLCellsUsedOptions.All, c => c.HasComment).ForEach(c => c.Comment.SetVisible());
|
||||
}
|
||||
|
||||
private static void AddMiscComments(XLWorkbook wb)
|
||||
{
|
||||
var ws = wb.Worksheets.Add("Comments");
|
||||
|
||||
ws.Cell("A1").SetValue("Hidden").Comment.AddText("Hidden");
|
||||
ws.Cell("A2").SetValue("Visible").Comment.AddText("Visible");
|
||||
ws.Cell("A3").SetValue("On Top").Comment.AddText("On Top");
|
||||
ws.Cell("A4").SetValue("Underneath").Comment.AddText("Underneath");
|
||||
ws.Cell("A4").Comment.Style.Alignment.SetVertical(XLDrawingVerticalAlignment.Bottom);
|
||||
ws.Cell("A3").Comment.SetZOrder(ws.Cell("A4").Comment.ZOrder + 1);
|
||||
|
||||
ws.Cell("D9").Comment.AddText("Vertical");
|
||||
ws.Cell("D9").Comment.Style.Alignment.Orientation = XLDrawingTextOrientation.Vertical;
|
||||
ws.Cell("D9").Comment.Style.Size.SetAutomaticSize();
|
||||
|
||||
ws.Cell("E9").Comment.AddText("Top to Bottom");
|
||||
ws.Cell("E9").Comment.Style.Alignment.Orientation = XLDrawingTextOrientation.TopToBottom;
|
||||
ws.Cell("E9").Comment.Style.Size.SetAutomaticSize();
|
||||
|
||||
ws.Cell("F9").Comment.AddText("Bottom to Top");
|
||||
ws.Cell("F9").Comment.Style.Alignment.Orientation = XLDrawingTextOrientation.BottomToTop;
|
||||
ws.Cell("F9").Comment.Style.Size.SetAutomaticSize();
|
||||
|
||||
ws.Cell("E1").Comment.Position.SetColumn(5);
|
||||
ws.Cell("E1").Comment.AddText("Start on Col E, on top border");
|
||||
ws.Cell("E1").Comment.Style.Size.SetWidth(10);
|
||||
var cE3 = ws.Cell("E3").Comment;
|
||||
cE3.AddText("Size and position");
|
||||
cE3.Position.SetColumn(5).SetRow(4).SetColumnOffset(7).SetRowOffset(10);
|
||||
cE3.Style.Size.SetHeight(25).Size.SetWidth(10);
|
||||
var cE7 = ws.Cell("E7").Comment;
|
||||
cE7.Position.SetColumn(6).SetRow(7).SetColumnOffset(0).SetRowOffset(0);
|
||||
cE7.Style.Size.SetHeight(ws.Row(7).Height).Size.SetWidth(ws.Column(6).Width);
|
||||
|
||||
ws.Cell("G1").Comment.AddText("Automatic Size");
|
||||
ws.Cell("G1").Comment.Style.Alignment.SetAutomaticSize();
|
||||
var cG3 = ws.Cell("G3").Comment;
|
||||
cG3.SetAuthor("MDeLeon");
|
||||
cG3.AddSignature();
|
||||
cG3.AddText("This is a test of the emergency broadcast system.");
|
||||
cG3.AddNewLine();
|
||||
cG3.AddText("Do ");
|
||||
cG3.AddText("NOT").SetFontColor(XLColor.RadicalRed).SetUnderline().SetBold();
|
||||
cG3.AddText(" forget it.");
|
||||
cG3.Style
|
||||
.Size.SetWidth(25)
|
||||
.Size.SetHeight(100)
|
||||
.Alignment.SetDirection(XLDrawingTextDirection.LeftToRight)
|
||||
.Alignment.SetHorizontal(XLDrawingHorizontalAlignment.Distributed)
|
||||
.Alignment.SetVertical(XLDrawingVerticalAlignment.Center)
|
||||
.Alignment.SetOrientation(XLDrawingTextOrientation.LeftToRight)
|
||||
.ColorsAndLines.SetFillColor(XLColor.Cyan)
|
||||
.ColorsAndLines.SetFillTransparency(0.25)
|
||||
.ColorsAndLines.SetLineColor(XLColor.DarkBlue)
|
||||
.ColorsAndLines.SetLineTransparency(0.75)
|
||||
.ColorsAndLines.SetLineDash(XLDashStyle.DashDot)
|
||||
.ColorsAndLines.SetLineStyle(XLLineStyle.ThinThick)
|
||||
.ColorsAndLines.SetLineWeight(5)
|
||||
.Margins.SetAll(0.25)
|
||||
.Properties.SetPositioning(XLDrawingAnchor.MoveAndSizeWithCells)
|
||||
.Protection.SetLocked(false)
|
||||
.Protection.SetLockText(false)
|
||||
.Web.SetAlternateText("This won't be released to the web");
|
||||
|
||||
ws.Cell("A9").Comment.SetAuthor("MDeLeon").AddSignature().AddText("Something");
|
||||
ws.Cell("A9").Comment.SetBold().SetFontColor(XLColor.DarkBlue);
|
||||
|
||||
ws.CellsUsed(XLCellsUsedOptions.All, c => !c.Address.ToStringRelative().Equals("A1") && c.HasComment).ForEach(c => c.Comment.SetVisible());
|
||||
}
|
||||
|
||||
private static void AddVisibilityComments(XLWorkbook wb)
|
||||
{
|
||||
var ws = wb.Worksheets.Add("Visibility");
|
||||
|
||||
// By default comments are hidden
|
||||
ws.Cell("A1").SetValue("I have a hidden comment").Comment.AddText("Hidden");
|
||||
|
||||
// Set the comment as visible
|
||||
ws.Cell("A2").Comment.SetVisible().AddText("Visible");
|
||||
|
||||
// The ZOrder on previous comments were 1 and 2 respectively
|
||||
// here we're explicit about the ZOrder
|
||||
ws.Cell("A3").Comment.SetZOrder(5).SetVisible().AddText("On Top");
|
||||
|
||||
// We want this comment to appear underneath the one for A3
|
||||
// so we set the ZOrder to something lower
|
||||
ws.Cell("A4").Comment.SetZOrder(4).SetVisible().AddText("Underneath");
|
||||
ws.Cell("A4").Comment.Style.Alignment.SetVertical(XLDrawingVerticalAlignment.Bottom);
|
||||
|
||||
// Alternatively you could set all comments to visible with the following line:
|
||||
// ws.CellsUsed(true, c => c.HasComment).ForEach(c => c.Comment.SetVisible());
|
||||
|
||||
ws.Columns().AdjustToContents();
|
||||
}
|
||||
|
||||
private void AddPosition(XLWorkbook wb)
|
||||
{
|
||||
var ws = wb.Worksheets.Add("Position");
|
||||
|
||||
ws.Columns().Width = 10;
|
||||
|
||||
ws.Cell("A1").Comment.AddText("This is an unusual place for a comment...");
|
||||
ws.Cell("A1").Comment.Position
|
||||
.SetColumn(3) // Starting from the third column
|
||||
.SetColumnOffset(5) // The comment will start in the middle of the third column
|
||||
.SetRow(5) // Starting from the fifth row
|
||||
.SetRowOffset(7.5); // The comment will start in the middle of the fifth row
|
||||
|
||||
// Set all comments to visible
|
||||
ws.CellsUsed(XLCellsUsedOptions.All, c => c.HasComment).ForEach(c => c.Comment.SetVisible());
|
||||
}
|
||||
|
||||
private void AddSignatures(XLWorkbook wb)
|
||||
{
|
||||
var ws = wb.Worksheets.Add("Signatures");
|
||||
|
||||
// By default the signature will be with the logged user
|
||||
// ws.Cell("A2").Comment.AddSignature().AddText("Hello World!");
|
||||
|
||||
// You can override this by specifying the comment's author:
|
||||
ws.Cell("A2").Comment
|
||||
.SetAuthor("MDeLeon")
|
||||
.AddSignature()
|
||||
.AddText("Hello World!");
|
||||
|
||||
|
||||
// Set all comments to visible
|
||||
ws.CellsUsed(XLCellsUsedOptions.All, c => c.HasComment).ForEach(c => c.Comment.SetVisible());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using ClosedXML.Excel;
|
||||
using System.IO;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
class EditingComments : IXLExample
|
||||
{
|
||||
|
||||
public void Create(string filePath) {
|
||||
|
||||
// Exercise(@"path/to/test/resources/comments");
|
||||
|
||||
}
|
||||
|
||||
public void Exercise(string basePath)
|
||||
{
|
||||
|
||||
// INCOMPLETE
|
||||
|
||||
var book = new XLWorkbook(Path.Combine(basePath, "EditingComments.xlsx"));
|
||||
var sheet = book.Worksheet(1);
|
||||
|
||||
// no change
|
||||
// A1
|
||||
|
||||
// edit existing comment
|
||||
sheet.Cell("B3").Comment.AddNewLine();
|
||||
sheet.Cell("B3").Comment.AddSignature();
|
||||
sheet.Cell("B3").Comment.AddText("more comment");
|
||||
|
||||
// delete
|
||||
//sheet.Cell("C1").DeleteComment();
|
||||
|
||||
// clear contents
|
||||
sheet.Cell("D3").Clear(XLClearOptions.Contents);
|
||||
|
||||
// new basic
|
||||
sheet.Cell("E1").Comment.AddText("non authored comment");
|
||||
|
||||
// new with author
|
||||
sheet.Cell("F3").Comment.AddSignature();
|
||||
sheet.Cell("F3").Comment.AddText("comment from author");
|
||||
|
||||
// TODO: merge with cells
|
||||
// TODO: resize with cells
|
||||
// TODO: visible
|
||||
|
||||
book.SaveAs(Path.Combine(basePath, "EditingComments_modified.xlsx"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,761 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class CFColorScaleLowMidHigh : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().ColorScale()
|
||||
.LowestValue(XLColor.Red)
|
||||
.Midpoint(XLCFContentType.Percent, "50", XLColor.Yellow)
|
||||
.HighestValue(XLColor.Green);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFColorScaleLowHigh : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().ColorScale()
|
||||
.Minimum(XLCFContentType.Number, "2", XLColor.Red)
|
||||
.Maximum(XLCFContentType.Percentile, "90", XLColor.Green);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFColorScaleMinimumMaximum : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().ColorScale()
|
||||
.LowestValue(XLColor.FromHtml("#FFFF7128"))
|
||||
.HighestValue(XLColor.FromHtml("#FFFFEF9C"));
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFStartsWith : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue("Hello")
|
||||
.CellBelow().SetValue("Hellos")
|
||||
.CellBelow().SetValue("Hell")
|
||||
.CellBelow().SetValue("Holl");
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenStartsWith("Hell")
|
||||
.Fill.SetBackgroundColor(XLColor.Red)
|
||||
.Border.SetOutsideBorder(XLBorderStyleValues.Thick)
|
||||
.Border.SetOutsideBorderColor(XLColor.Blue)
|
||||
.Font.SetBold();
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFEndsWith : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue("Hello")
|
||||
.CellBelow().SetValue("Hellos")
|
||||
.CellBelow().SetValue("Hell")
|
||||
.CellBelow().SetValue("Holl");
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenEndsWith("ll")
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFIsBlank : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue("Hello")
|
||||
.CellBelow().SetValue("")
|
||||
.CellBelow().SetValue("")
|
||||
.CellBelow().SetValue("Holl");
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenIsBlank()
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFNotBlank : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue("Hello")
|
||||
.CellBelow().SetValue("")
|
||||
.CellBelow().SetValue("")
|
||||
.CellBelow().SetValue("Holl");
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenNotBlank()
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFIsError : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue("Hello")
|
||||
.CellBelow().SetFormulaA1("1/0")
|
||||
.CellBelow().SetFormulaA1("1/0")
|
||||
.CellBelow().SetValue("Holl");
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenIsError()
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFNotError : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue("Hello")
|
||||
.CellBelow().SetFormulaA1("1/0")
|
||||
.CellBelow().SetFormulaA1("1/0")
|
||||
.CellBelow().SetValue("Holl");
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenNotError()
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFContains : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue("Hello")
|
||||
.CellBelow().SetValue("Hellos")
|
||||
.CellBelow().SetValue("Hell")
|
||||
.CellBelow().SetValue("Holl");
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenContains("Hell")
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFNotContains : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue("Hello")
|
||||
.CellBelow().SetValue("Hellos")
|
||||
.CellBelow().SetValue("Hell")
|
||||
.CellBelow().SetValue("Holl");
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenNotContains("Hell")
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFEqualsString : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue("Hello")
|
||||
.CellBelow().SetValue("Hellos")
|
||||
.CellBelow().SetValue("Hell")
|
||||
.CellBelow().SetValue("Holl");
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenEquals("Hell")
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFEqualsNumber : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenEquals(2)
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFNotEqualsString : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue("Hello")
|
||||
.CellBelow().SetValue("Hellos")
|
||||
.CellBelow().SetValue("Hell")
|
||||
.CellBelow().SetValue("Holl");
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenNotEquals("Hell")
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFNotEqualsNumber : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenNotEquals(2)
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFGreaterThan : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenGreaterThan("2")
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFEqualOrGreaterThan : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenEqualOrGreaterThan("2")
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFLessThan : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenLessThan("2")
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFEqualOrLessThan : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenEqualOrLessThan("2")
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFBetween : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenBetween("2", "3")
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFNotBetween : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenNotBetween("2", "3")
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFUnique : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenIsUnique()
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFDuplicate : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenIsDuplicate()
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFIsTrue : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenIsTrue("TRUE")
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFTop : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenIsTop(2)
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFBottom : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenIsBottom(10, XLTopBottomType.Percent)
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFDataBar : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().DataBar(XLColor.Red, true)
|
||||
.LowestValue()
|
||||
.Maximum(XLCFContentType.Percent, "100");
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFDataBarNegative : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.Cell(1, 1).SetValue(-1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.Range(ws.Cell(1, 1), ws.Cell(4, 1))
|
||||
.AddConditionalFormat()
|
||||
.DataBar(XLColor.Green, XLColor.Red, showBarOnly: false)
|
||||
.LowestValue()
|
||||
.HighestValue();
|
||||
|
||||
ws.Cell(1, 3).SetValue(-20)
|
||||
.CellBelow().SetValue(40)
|
||||
.CellBelow().SetValue(-60)
|
||||
.CellBelow().SetValue(30);
|
||||
|
||||
ws.Range(ws.Cell(1, 3), ws.Cell(4, 3))
|
||||
.AddConditionalFormat()
|
||||
.DataBar(XLColor.Green, XLColor.Red, showBarOnly: true)
|
||||
.Minimum(XLCFContentType.Number, -100)
|
||||
.Maximum(XLCFContentType.Number, 100);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFIconSet : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().IconSet(XLIconSetStyle.ThreeTrafficLights2, true, true)
|
||||
.AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "0", XLCFContentType.Number)
|
||||
.AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "2", XLCFContentType.Number)
|
||||
.AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "3", XLCFContentType.Number);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFTwoConditions : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().IconSet(XLIconSetStyle.ThreeTrafficLights2, true, true)
|
||||
.AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "0", XLCFContentType.Number)
|
||||
.AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "2", XLCFContentType.Number)
|
||||
.AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "3", XLCFContentType.Number);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().WhenContains("1")
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFInsertRows : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.Cell(2, 1).SetValue(1)
|
||||
.CellRight().SetValue(1)
|
||||
.CellRight().SetValue(2)
|
||||
.CellRight().SetValue(3);
|
||||
|
||||
var range = ws.RangeUsed();
|
||||
range.AddConditionalFormat().WhenEquals("1").Font.SetBold();
|
||||
range.InsertRowsAbove(1);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFTest : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(1)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3)
|
||||
.CellBelow().SetValue(4);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().DataBar(XLColor.Red, XLColor.Green)
|
||||
.LowestValue()
|
||||
.HighestValue();
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFMultipleConditions : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
var range = ws.Range("A1:A10");
|
||||
range.AddConditionalFormat().WhenEquals("3")
|
||||
.Fill.SetBackgroundColor(XLColor.Blue);
|
||||
range.AddConditionalFormat().WhenEquals("2")
|
||||
.Fill.SetBackgroundColor(XLColor.Green);
|
||||
range.AddConditionalFormat().WhenEquals("1")
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFStopIfTrue : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
ws.FirstCell().SetValue(6)
|
||||
.CellBelow().SetValue(1)
|
||||
.CellBelow().SetValue(2)
|
||||
.CellBelow().SetValue(3);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().SetStopIfTrue().WhenGreaterThan(5);
|
||||
|
||||
ws.RangeUsed().AddConditionalFormat().IconSet(XLIconSetStyle.ThreeTrafficLights2, true, true)
|
||||
.AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "0", XLCFContentType.Number)
|
||||
.AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "2", XLCFContentType.Number)
|
||||
.AddValue(XLCFIconSetOperator.EqualOrGreaterThan, "3", XLCFContentType.Number);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public class CFDatesOccurring : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
using (var workbook = new XLWorkbook())
|
||||
{
|
||||
var ws = workbook.AddWorksheet("Sheet1");
|
||||
|
||||
var range = ws.Range("A1:A10");
|
||||
range.AddConditionalFormat()
|
||||
.WhenDateIs(XLTimePeriod.Tomorrow)
|
||||
.Fill.SetBackgroundColor(XLColor.GrannySmithApple);
|
||||
|
||||
range.AddConditionalFormat()
|
||||
.WhenDateIs(XLTimePeriod.Yesterday)
|
||||
.Fill.SetBackgroundColor(XLColor.Orange);
|
||||
|
||||
range.AddConditionalFormat()
|
||||
.WhenDateIs(XLTimePeriod.InTheLast7Days)
|
||||
.Fill.SetBackgroundColor(XLColor.Blue);
|
||||
|
||||
range.AddConditionalFormat()
|
||||
.WhenDateIs(XLTimePeriod.ThisMonth)
|
||||
.Fill.SetBackgroundColor(XLColor.Red);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class CFDataBars : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
using var workbook = new XLWorkbook();
|
||||
var ws = workbook.AddWorksheet();
|
||||
|
||||
ws.Range("A2:F3").Value = 1;
|
||||
ws.Range("A4:F4").Value = 2;
|
||||
ws.Range("A5:F5").Value = 3;
|
||||
ws.Range("A6:F6").Value = 4;
|
||||
|
||||
ws.Cell("A1").Value = "Automatic";
|
||||
ws.Range("A2:A6").AddConditionalFormat().DataBar(XLColor.Amber);
|
||||
|
||||
ws.Cell("B1").Value = "Lowest/Highest";
|
||||
ws.Range("B2:B6").AddConditionalFormat().DataBar(XLColor.BallBlue)
|
||||
.LowestValue()
|
||||
.HighestValue();
|
||||
|
||||
ws.Cell("C1").Value = "Value";
|
||||
ws.Range("C2:C6").AddConditionalFormat().DataBar(XLColor.Cadet)
|
||||
.Minimum(XLCFContentType.Number, 0)
|
||||
.Maximum(XLCFContentType.Number, 10);
|
||||
|
||||
ws.Cell("D1").Value = "Percent";
|
||||
ws.Range("D2:D6").AddConditionalFormat().DataBar(XLColor.Desert)
|
||||
.Minimum(XLCFContentType.Percent, 50)
|
||||
.Maximum(XLCFContentType.Percent, 100);
|
||||
|
||||
ws.Cell("E1").Value = "Formula";
|
||||
ws.Range("E2:E6").AddConditionalFormat().DataBar(XLColor.Ecru)
|
||||
.Minimum(XLCFContentType.Formula, "-SUM($A$2:$E$2)")
|
||||
.Maximum(XLCFContentType.Formula, "SUM($A$6:$E$6)");
|
||||
|
||||
ws.Cell("F1").Value = "Percentile";
|
||||
ws.Range("F2:F6").AddConditionalFormat().DataBar(XLColor.Fandango)
|
||||
.Minimum(XLCFContentType.Percentile, 30)
|
||||
.Maximum(XLCFContentType.Percentile, 70);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
using ClosedXML_Examples.Columns;
|
||||
using ClosedXML_Examples.Misc;
|
||||
using ClosedXML_Examples.PageSetup;
|
||||
using ClosedXML_Examples.Ranges;
|
||||
using ClosedXML_Examples.Rows;
|
||||
using ClosedXML_Examples.Styles;
|
||||
using ClosedXML_Examples.Tables;
|
||||
using System.IO;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class CreateFiles
|
||||
{
|
||||
public static void CreateAllFiles()
|
||||
{
|
||||
var path = Program.BaseCreatedDirectory;
|
||||
|
||||
new HelloWorld().Create(Path.Combine(path, "HelloWorld.xlsx"));
|
||||
new BasicTable().Create(Path.Combine(path, "BasicTable.xlsx"));
|
||||
|
||||
new StyleExamples().Create();
|
||||
new ChangingBasicTable().Create(Path.Combine(path, "BasicTable_Modified.xlsx"));
|
||||
new ShiftingRanges().Create(Path.Combine(path, "ShiftingRanges.xlsx"));
|
||||
new ColumnSettings().Create(Path.Combine(path, "ColumnSettings.xlsx"));
|
||||
new RowSettings().Create(Path.Combine(path, "RowSettings.xlsx"));
|
||||
new MergeCells().Create(Path.Combine(path, "MergedCells.xlsx"));
|
||||
new InsertRows().Create(Path.Combine(path, "InsertRows.xlsx"));
|
||||
new InsertColumns().Create(Path.Combine(path, "InsertColumns.xlsx"));
|
||||
new ColumnCollection().Create(Path.Combine(path, "ColumnCollection.xlsx"));
|
||||
new DataTypes().Create(Path.Combine(path, "DataTypes.xlsx"));
|
||||
new DataTypesUnderDifferentCulture().Create(Path.Combine(path, "DataTypesUnderDifferentCulture.xlsx"));
|
||||
new MultipleSheets().Create(Path.Combine(path, "MultipleSheets.xlsx"));
|
||||
new RowCollection().Create(Path.Combine(path, "RowCollection.xlsx"));
|
||||
new DefiningRanges().Create(Path.Combine(path, "DefiningRanges.xlsx"));
|
||||
new ClearingRanges().Create(Path.Combine(path, "ClearingRanges.xlsx"));
|
||||
new DeletingRanges().Create(Path.Combine(path, "DeletingRanges.xlsx"));
|
||||
new Margins().Create(Path.Combine(path, "Margins.xlsx"));
|
||||
new Page().Create(Path.Combine(path, "Page.xlsx"));
|
||||
new HeaderFooters().Create(Path.Combine(path, "HeaderFooters.xlsx"));
|
||||
new Sheets().Create(Path.Combine(path, "Sheets.xlsx"));
|
||||
new SheetTab().Create(Path.Combine(path, "SheetTab.xlsx"));
|
||||
new MultipleRanges().Create(Path.Combine(path, "MultipleRanges.xlsx"));
|
||||
new StyleWorksheet().Create(Path.Combine(path, "StyleWorksheet.xlsx"));
|
||||
new StyleRowsColumns().Create(Path.Combine(path, "StyleRowsColumns.xlsx"));
|
||||
new InsertingDeletingRows().Create(Path.Combine(path, "InsertingDeletingRows.xlsx"));
|
||||
new InsertingDeletingColumns().Create(Path.Combine(path, "InsertingDeletingColumns.xlsx"));
|
||||
new DeletingColumns().Create(Path.Combine(path, "DeletingColumns.xlsx"));
|
||||
new CellValues().Create(Path.Combine(path, "CellValues.xlsx"));
|
||||
new LambdaExpressions().Create(Path.Combine(path, "LambdaExpressions.xlsx"));
|
||||
new DefaultStyles().Create(Path.Combine(path, "DefaultStyles.xlsx"));
|
||||
new TransposeRanges().Create(Path.Combine(path, "TransposeRanges.xlsx"));
|
||||
new TransposeRangesPlus().Create(Path.Combine(path, "TransposeRangesPlus.xlsx"));
|
||||
new MergeMoves().Create(Path.Combine(path, "MergedMoves.xlsx"));
|
||||
new WorkbookProperties().Create(Path.Combine(path, "WorkbookProperties.xlsx"));
|
||||
new AdjustToContents().Create(Path.Combine(path, "AdjustToContents.xlsx"));
|
||||
new AdjustToContentsWithAutoFilter().Create(Path.Combine(path, "AdjustToContentsWithAutoFilter.xlsx"));
|
||||
new HideUnhide().Create(Path.Combine(path, "HideUnhide.xlsx"));
|
||||
new Outline().Create(Path.Combine(path, "Outline.xlsx"));
|
||||
new Formulas().Create(Path.Combine(path, "Formulas.xlsx"));
|
||||
new Collections().Create(Path.Combine(path, "Collections.xlsx"));
|
||||
new NamedRanges().Create(Path.Combine(path, "NamedRanges.xlsx"));
|
||||
new CopyingRanges().Create(Path.Combine(path, "CopyingRanges.xlsx"));
|
||||
new BlankCells().Create(Path.Combine(path, "BlankCells.xlsx"));
|
||||
new TwoPages().Create(Path.Combine(path, "TwoPages.xlsx"));
|
||||
new UsingColors().Create(Path.Combine(path, "UsingColors.xlsx"));
|
||||
|
||||
new ColumnCells().Create(Path.Combine(path, "ColumnCells.xlsx"));
|
||||
new RowCells().Create(Path.Combine(path, "RowCells.xlsx"));
|
||||
new FreezePanes().Create(Path.Combine(path, "FreezePanes.xlsx"));
|
||||
new UsingTables().Create(Path.Combine(path, "UsingTables.xlsx"));
|
||||
new ResizingTables().Create(Path.Combine(path, "ResizingTables.xlsx"));
|
||||
new AddingRowToTables().Create(Path.Combine(path, "AddingRowToTables.xlsx"));
|
||||
new RightToLeft().Create(Path.Combine(path, "RightToLeft.xlsx"));
|
||||
new ShowCase().Create(Path.Combine(path, "ShowCase.xlsx"));
|
||||
new CopyingWorksheets().Create(Path.Combine(path, "CopyingWorksheets.xlsx"));
|
||||
new InsertingTables().Create(Path.Combine(path, "InsertingTables.xlsx"));
|
||||
new InsertingData().Create(Path.Combine(path, "InsertingData.xlsx"));
|
||||
new Hyperlinks().Create(Path.Combine(path, "Hyperlinks.xlsx"));
|
||||
new DataValidation().Create(Path.Combine(path, "DataValidation.xlsx"));
|
||||
new HideSheets().Create(Path.Combine(path, "HideSheets.xlsx"));
|
||||
new SheetProtection().Create(Path.Combine(path, "SheetProtection.xlsx"));
|
||||
new AutoFilter().Create(Path.Combine(path, "AutoFilter.xlsx"));
|
||||
new Sorting().Create(Path.Combine(path, "Sorting.xlsx"));
|
||||
new SortExample().Create(Path.Combine(path, "SortExample.xlsx"));
|
||||
new AddingDataSet().Create(Path.Combine(path, "AddingDataSet.xlsx"));
|
||||
new AddingDataTableAsWorksheet().Create(Path.Combine(path, "AddingDataTableAsWorksheet.xlsx"));
|
||||
new TabColors().Create(Path.Combine(path, "TabColors.xlsx"));
|
||||
new ShiftingFormulas().Create(Path.Combine(path, "ShiftingFormulas.xlsx"));
|
||||
new CopyingRowsAndColumns().Create(Path.Combine(path, "CopyingRowsAndColumns.xlsx"));
|
||||
new UsingRichText().Create(Path.Combine(path, "UsingRichText.xlsx"));
|
||||
new UsingPhonetics().Create(Path.Combine(path, "UsingPhonetics.xlsx"));
|
||||
new WalkingRanges().Create(Path.Combine(path, "CellMoves.xlsx"));
|
||||
new AddingComments().Create(Path.Combine(path, "AddingComments.xlsx"));
|
||||
new PivotTables().Create(Path.Combine(path, "PivotTables.xlsx"));
|
||||
new SheetViews().Create(Path.Combine(path, "SheetViews.xlsx"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.IO;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples.Delete
|
||||
{
|
||||
public class DeleteFewWorksheets:IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
string tempFile = ExampleHelper.GetTempFilePath(filePath);
|
||||
try
|
||||
{
|
||||
//Note: Prepare
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
workbook.Worksheets.Add("1");
|
||||
workbook.Worksheets.Add("2");
|
||||
workbook.Worksheets.Add("3");
|
||||
workbook.Worksheets.Add("4");
|
||||
workbook.SaveAs(tempFile);
|
||||
}
|
||||
|
||||
//Note: Delate few worksheet
|
||||
{
|
||||
var workbook = new XLWorkbook(tempFile);
|
||||
workbook.Worksheets.Delete("1");
|
||||
workbook.Worksheets.Delete("2");
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(tempFile))
|
||||
{
|
||||
File.Delete(tempFile);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Delete
|
||||
{
|
||||
public class DeleteRows : 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)
|
||||
{
|
||||
#region Create case
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Delete red rows");
|
||||
|
||||
// Put a value in a few cells
|
||||
foreach (var r in Enumerable.Range(1, 5))
|
||||
foreach (var c in Enumerable.Range(1, 5))
|
||||
ws.Cell(r, c).Value = string.Format("R{0}C{1}", r, c);
|
||||
|
||||
|
||||
var blueRow = ws.Rows(1, 2);
|
||||
var redRow = ws.Row(5);
|
||||
|
||||
blueRow.Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
|
||||
redRow.Style.Fill.BackgroundColor = XLColor.Red;
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Remove rows
|
||||
{
|
||||
var workbook = new XLWorkbook(filePath);
|
||||
var ws = workbook.Worksheets.Worksheet("Delete red rows");
|
||||
|
||||
ws.Rows(1, 2).Delete();
|
||||
workbook.Save();
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.IO;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public static class ExampleHelper
|
||||
{
|
||||
public static string GetTempFilePath()
|
||||
{
|
||||
return Path.GetTempFileName();
|
||||
}
|
||||
|
||||
public static string GetTempFilePath(string filePath)
|
||||
{
|
||||
var extension = Path.GetExtension(filePath);
|
||||
var tempFilePath = GetTempFilePath();
|
||||
return Path.ChangeExtension(tempFilePath, extension);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class HelloWorld
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var worksheet = workbook.Worksheets.Add("Sample Sheet");
|
||||
worksheet.Cell("A1").Value = "Hello World!";
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public interface IXLExample
|
||||
{
|
||||
void Create(string filePath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using ClosedXML.Excel;
|
||||
using ClosedXML.Excel.Drawings;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class ImageAnchors : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
IXLWorksheet ws;
|
||||
|
||||
using (Stream fs = Assembly.GetExecutingAssembly().GetManifestResourceStream("ClosedXML_Examples.Resources.ImageHandling.png"))
|
||||
{
|
||||
ws = wb.Worksheets.Add("Images1");
|
||||
|
||||
#region AbsoluteAnchor
|
||||
|
||||
ws.AddPicture(fs, XLPictureFormat.Png, "Image10")
|
||||
.MoveTo(220, 150);
|
||||
|
||||
#endregion AbsoluteAnchor
|
||||
|
||||
#region OneCellAnchor
|
||||
|
||||
fs.Position = 0;
|
||||
ws.AddPicture(fs, XLPictureFormat.Png, "Image11")
|
||||
.MoveTo(ws.Cell(1, 1));
|
||||
|
||||
#endregion OneCellAnchor
|
||||
|
||||
ws = wb.Worksheets.Add("Images2");
|
||||
|
||||
#region TwoCellAnchor
|
||||
|
||||
fs.Position = 0;
|
||||
ws.AddPicture(fs, XLPictureFormat.Png, "Image20")
|
||||
.MoveTo(ws.Cell(6, 5), ws.Cell(9, 7));
|
||||
|
||||
#endregion TwoCellAnchor
|
||||
}
|
||||
|
||||
using (Stream fs = Assembly.GetExecutingAssembly().GetManifestResourceStream("ClosedXML_Examples.Resources.SampleImage.jpg"))
|
||||
{
|
||||
// Moving images around and scaling them
|
||||
ws = wb.Worksheets.Add("Images3");
|
||||
|
||||
ws.AddPicture(fs, XLPictureFormat.Jpeg)
|
||||
.MoveTo(ws.Cell(2, 2), 20, 5, ws.Cell(5, 5), 30, 10)
|
||||
.MoveTo(ws.Cell(2, 2), ws.Cell(5, 5));
|
||||
|
||||
ws.AddPicture(fs, XLPictureFormat.Jpeg)
|
||||
.MoveTo(ws.Cell(6, 2), 2, 2, ws.Cell(9, 5), 2, 2)
|
||||
.MoveTo(ws.Cell(6, 2), 20, 5, ws.Cell(9, 5), 30, 10);
|
||||
|
||||
ws.AddPicture(fs, XLPictureFormat.Jpeg)
|
||||
.MoveTo(ws.Cell(10, 2), 20, 5)
|
||||
.Scale(0.2, true)
|
||||
.MoveTo(ws.Cell(10, 1));
|
||||
}
|
||||
|
||||
using (Stream fs = Assembly.GetExecutingAssembly().GetManifestResourceStream("ClosedXML_Examples.Resources.SampleImage.jpg"))
|
||||
{
|
||||
// Changing of placement
|
||||
ws = wb.Worksheets.Add("Images4");
|
||||
|
||||
ws.AddPicture(fs, XLPictureFormat.Jpeg)
|
||||
.MoveTo(100, 100)
|
||||
.WithPlacement(XLPicturePlacement.FreeFloating);
|
||||
|
||||
// Add and delete picture immediately
|
||||
ws.AddPicture(fs, XLPictureFormat.Jpeg)
|
||||
.MoveTo(100, 600)
|
||||
.Delete();
|
||||
}
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using ClosedXML.Excel;
|
||||
using ClosedXML.Excel.Drawings;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class ImageFormats : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
IXLWorksheet ws;
|
||||
|
||||
using (Stream fs = Assembly.GetExecutingAssembly().GetManifestResourceStream("ClosedXML_Examples.Resources.ImageHandling.jpg"))
|
||||
{
|
||||
#region Jpeg
|
||||
|
||||
ws = wb.Worksheets.Add("Jpg");
|
||||
ws.AddPicture(fs, XLPictureFormat.Jpeg, "JpegImage")
|
||||
.MoveTo(ws.Cell(1, 1));
|
||||
|
||||
#endregion Jpeg
|
||||
}
|
||||
|
||||
using (Stream fs = Assembly.GetExecutingAssembly().GetManifestResourceStream("ClosedXML_Examples.Resources.ImageHandling.png"))
|
||||
{
|
||||
#region Png
|
||||
|
||||
ws = wb.Worksheets.Add("Png");
|
||||
ws.AddPicture(fs, XLPictureFormat.Png, "PngImage")
|
||||
.MoveTo(ws.Cell(1, 1));
|
||||
|
||||
#endregion Png
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System.IO;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class ChangingBasicTable : 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);
|
||||
|
||||
// Change the background color of the headers
|
||||
var rngHeaders = ws.Range("B3:F3");
|
||||
rngHeaders.Style.Fill.BackgroundColor = XLColor.LightSalmon;
|
||||
|
||||
// Change the date formats
|
||||
var rngDates = ws.Range("E4:E6");
|
||||
rngDates.Style.DateFormat.Format = "MM/dd/yyyy";
|
||||
|
||||
// Change the income values to text
|
||||
var rngNumbers = ws.Range("F4:F6");
|
||||
foreach (var cell in rngNumbers.Cells())
|
||||
{
|
||||
string formattedString = cell.GetFormattedString();
|
||||
cell.DataType = XLDataType.Text;
|
||||
cell.Value = formattedString + " Dollars";
|
||||
}
|
||||
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(tempFile))
|
||||
{
|
||||
File.Delete(tempFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class LoadFiles
|
||||
{
|
||||
public static void LoadAllFiles()
|
||||
{
|
||||
foreach (var file in Directory.GetFiles(Program.BaseCreatedDirectory))
|
||||
{
|
||||
var fileInfo = new FileInfo(file);
|
||||
var fileName = fileInfo.Name;
|
||||
LoadAndSaveFile(Path.Combine(Program.BaseCreatedDirectory, fileName), Path.Combine(Program.BaseModifiedDirectory, fileName));
|
||||
}
|
||||
}
|
||||
|
||||
private static void LoadAndSaveFile(String input, String output)
|
||||
{
|
||||
var wb = new XLWorkbook(input);
|
||||
wb.SaveAs(output);
|
||||
wb.SaveAs(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class AddingDataSet : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
|
||||
var dataSet = GetDataSet();
|
||||
|
||||
// Add all DataTables in the DataSet as a worksheets
|
||||
wb.Worksheets.Add(dataSet);
|
||||
|
||||
foreach (var ws in wb.Worksheets)
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
private DataSet GetDataSet()
|
||||
{
|
||||
var ds = new DataSet();
|
||||
ds.Tables.Add(GetTable("Patients"));
|
||||
ds.Tables.Add(GetTable("Employees"));
|
||||
ds.Tables.Add(GetTable("Information"));
|
||||
return ds;
|
||||
}
|
||||
|
||||
private DataTable GetTable(String tableName)
|
||||
{
|
||||
DataTable table = new DataTable();
|
||||
table.TableName = tableName;
|
||||
table.Columns.Add("Dosage", typeof(int));
|
||||
table.Columns.Add("Drug", typeof(string));
|
||||
table.Columns.Add("Patient", typeof(string));
|
||||
table.Columns.Add("Date", typeof(DateTime));
|
||||
|
||||
table.Rows.Add(25, "Indocin", "David", new DateTime(2000, 1, 1));
|
||||
table.Rows.Add(50, "Enebrel", "Sam", new DateTime(2000, 1, 2));
|
||||
table.Rows.Add(10, "Hydralazine", "Christoff", new DateTime(2000, 1, 3));
|
||||
table.Rows.Add(21, "Combivent", "Janet", new DateTime(2000, 1, 4));
|
||||
table.Rows.Add(100, "Dilantin", "Melanie", new DateTime(2000, 1, 5));
|
||||
return table;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class AddingDataTableAsWorksheet : 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();
|
||||
|
||||
var dataTable = GetTable("Information");
|
||||
|
||||
// Add a DataTable as a worksheet
|
||||
wb.Worksheets.Add(dataTable);
|
||||
wb.Worksheets.First().Columns().AdjustToContents();
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
private DataTable GetTable(String tableName)
|
||||
{
|
||||
DataTable table = new DataTable();
|
||||
table.TableName = tableName;
|
||||
table.Columns.Add("Dosage", typeof(int));
|
||||
table.Columns.Add("Drug", typeof(string));
|
||||
table.Columns.Add("Patient", typeof(string));
|
||||
table.Columns.Add("Date", typeof(DateTime));
|
||||
|
||||
table.Rows.Add(25, "Indocin", "David", new DateTime(2000, 1, 1));
|
||||
table.Rows.Add(50, "Enebrel", "Sam", new DateTime(2000, 1, 2));
|
||||
table.Rows.Add(10, "Hydralazine", "Christoff", new DateTime(2000, 1, 3));
|
||||
table.Rows.Add(21, "Combivent", "Janet", new DateTime(2000, 1, 4));
|
||||
table.Rows.Add(100, "Dilantin", "Melanie", new DateTime(2000, 1, 5));
|
||||
return table;
|
||||
}
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class AdjustToContents : IXLExample
|
||||
{
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
var ws = wb.Worksheets.Add("Adjust To Contents");
|
||||
|
||||
// Set some values with different font sizes
|
||||
ws.Cell(1, 1).Value = "Tall Row";
|
||||
ws.Cell(1, 1).Style.Font.FontSize = 30;
|
||||
ws.Cell(2, 1).Value = "Very Wide Column";
|
||||
ws.Cell(2, 1).Style.Font.FontSize = 20;
|
||||
|
||||
// Adjust column width
|
||||
ws.Column(1).AdjustToContents();
|
||||
|
||||
// Adjust row heights
|
||||
ws.Rows(1, 2).AdjustToContents();
|
||||
|
||||
// You can also adjust all rows/columns in one shot
|
||||
// ws.Rows().AdjustToContents();
|
||||
// ws.Columns().AdjustToContents();
|
||||
|
||||
// We'll now select which cells should be used for calculating the
|
||||
// column widths (same method applies for row heights)
|
||||
|
||||
// Set the values
|
||||
ws.Cell(4, 2).Value = "Width ignored because calling column.AdjustToContents(5, 7)";
|
||||
ws.Cell(5, 2).Value = "Short text";
|
||||
ws.Cell(6, 2).Value = "Width ignored because it's part of a merge";
|
||||
ws.Range(6, 2, 6, 4).Merge();
|
||||
ws.Cell(7, 2).Value = "Width should adjust to this cell";
|
||||
ws.Cell(8, 2).Value = "Width ignored because calling column.AdjustToContents(5, 7)";
|
||||
|
||||
// Adjust column widths only taking into account rows 5-7
|
||||
// (merged cells will be ignored)
|
||||
ws.Column(2).AdjustToContents(5, 7);
|
||||
|
||||
// You can also specify the starting row to start calculating the widths:
|
||||
// e.g. ws.Column(3).AdjustToContents(9);
|
||||
|
||||
var ws2 = wb.Worksheets.Add("Adjust Widths");
|
||||
ws2.Cell(1, 1).SetValue("Text to adjust - 255").Style.Alignment.TextRotation = 255;
|
||||
for (Int32 co = 0; co < 90; co += 5)
|
||||
{
|
||||
ws2.Cell(1, (co / 5) + 2).SetValue("Text to adjust - " + co).Style.Alignment.TextRotation = co;
|
||||
}
|
||||
|
||||
ws2.Columns().AdjustToContents();
|
||||
|
||||
var ws4 = wb.Worksheets.Add("Adjust Widths 2");
|
||||
ws4.Cell(1, 1).SetValue("Text to adjust - 255").Style.Alignment.TextRotation = 255;
|
||||
for (Int32 co = 0; co < 90; co += 5)
|
||||
{
|
||||
var c = ws4.Cell(1, (co / 5) + 2);
|
||||
|
||||
c.RichText.AddText("Text to adjust - " + co).SetBold();
|
||||
c.RichText.AddText(Environment.NewLine);
|
||||
c.RichText.AddText("World!").SetBold().SetFontColor(XLColor.Blue).SetFontSize(25);
|
||||
c.RichText.AddText(Environment.NewLine);
|
||||
c.RichText.AddText("Hello Cruel and unsusual world").SetBold().SetFontSize(20);
|
||||
c.RichText.AddText(Environment.NewLine);
|
||||
c.RichText.AddText("Hello").SetBold();
|
||||
c.Style.Alignment.SetTextRotation(co);
|
||||
}
|
||||
ws4.Columns().AdjustToContents();
|
||||
|
||||
var ws3 = wb.Worksheets.Add("Adjust Heights");
|
||||
ws3.Cell(1, 1).SetValue("Text to adjust - 255").Style.Alignment.TextRotation = 255;
|
||||
for (Int32 ro = 0; ro < 90; ro += 5)
|
||||
{
|
||||
ws3.Cell((ro / 5) + 2, 1).SetValue("Text to adjust - " + ro).Style.Alignment.TextRotation = ro;
|
||||
}
|
||||
|
||||
ws3.Rows().AdjustToContents();
|
||||
|
||||
var ws5 = wb.Worksheets.Add("Adjust Heights 2");
|
||||
ws5.Cell(1, 1).SetValue("Text to adjust - 255").Style.Alignment.TextRotation = 255;
|
||||
for (Int32 ro = 0; ro < 90; ro += 5)
|
||||
{
|
||||
var c = ws5.Cell((ro / 5) + 2, 1);
|
||||
c.RichText.AddText("Text to adjust - " + ro).SetBold();
|
||||
c.RichText.AddText(Environment.NewLine);
|
||||
c.RichText.AddText("World!").SetBold().SetFontColor(XLColor.Blue).SetFontSize(10);
|
||||
c.RichText.AddText(Environment.NewLine);
|
||||
c.RichText.AddText("Hello Cruel and unsusual world").SetBold().SetFontSize(15);
|
||||
c.RichText.AddText(Environment.NewLine);
|
||||
c.RichText.AddText("Hello").SetBold();
|
||||
c.Style.Alignment.SetTextRotation(ro);
|
||||
}
|
||||
|
||||
ws5.Rows().AdjustToContents();
|
||||
|
||||
var ws6 = wb.Worksheets.Add("Absurdly wide column");
|
||||
ws6.Cell("A1").Value = "Some string";
|
||||
|
||||
// This column's width should be capped at 255
|
||||
ws6.Cell("B1").Value = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
|
||||
|
||||
ws6.Columns().AdjustToContents();
|
||||
|
||||
wb.SaveAs(filePath, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class AdjustToContentsWithAutoFilter : IXLExample
|
||||
{
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
var ws = wb.Worksheets.Add("AutoFilter");
|
||||
ws.Cell("A1").Value = "AVeryLongColumnHeader";
|
||||
ws.Cell("A2").Value = "John";
|
||||
ws.Cell("A3").Value = "Hank";
|
||||
ws.Cell("A4").Value = "Dagny";
|
||||
|
||||
ws.RangeUsed().SetAutoFilter();
|
||||
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class AutoFilter : 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();
|
||||
var ws = wb.Worksheets.Add("AutoFilter");
|
||||
ws.Cell("A1").Value = "Names";
|
||||
ws.Cell("A2").Value = "John";
|
||||
ws.Cell("A3").Value = "Hank";
|
||||
ws.Cell("A4").Value = "Dagny";
|
||||
|
||||
ws.RangeUsed().SetAutoFilter();
|
||||
|
||||
// Your can turn off the autofilter by:
|
||||
// 1) worksheet.AutoFilter.Clear()
|
||||
// 2) worksheet.SetAutoFilter(false)
|
||||
// 3) Pick any range in the worksheet and call the above methods on the range
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class BlankCells : 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();
|
||||
var ws = wb.Worksheets.Add("Sheet1");
|
||||
ws.Cell(1, 1).Value = "X";
|
||||
ws.Cell(1, 1).Clear();
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class CellValues : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
using (var workbook = new XLWorkbook())
|
||||
{
|
||||
var ws = workbook.Worksheets.Add("Cell Values");
|
||||
|
||||
// Set the titles
|
||||
ws.Cell(2, 2).Value = "Initial Value";
|
||||
ws.Cell(2, 3).Value = "Casting";
|
||||
ws.Cell(2, 4).Value = "Using Get...()";
|
||||
ws.Cell(2, 5).Value = "Using GetValue<T>()";
|
||||
ws.Cell(2, 6).Value = "GetString()";
|
||||
ws.Cell(2, 7).Value = "GetFormattedString()";
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// DateTime
|
||||
|
||||
// Fill a cell with a date
|
||||
var cellDateTime = ws.Cell(3, 2);
|
||||
cellDateTime.Value = new DateTime(2010, 9, 2);
|
||||
cellDateTime.Style.DateFormat.Format = "yyyy-MMM-dd";
|
||||
|
||||
// Extract the date in different ways
|
||||
DateTime dateTime1 = (DateTime)cellDateTime.Value;
|
||||
DateTime dateTime2 = cellDateTime.GetDateTime();
|
||||
DateTime dateTime3 = cellDateTime.GetValue<DateTime>();
|
||||
String dateTimeString = cellDateTime.GetString();
|
||||
String dateTimeFormattedString = cellDateTime.GetFormattedString();
|
||||
|
||||
// Set the values back to cells
|
||||
// The apostrophe is to force ClosedXML to treat the date as a string
|
||||
ws.Cell(3, 3).Value = dateTime1;
|
||||
ws.Cell(3, 4).Value = dateTime2;
|
||||
ws.Cell(3, 5).Value = dateTime3;
|
||||
ws.Cell(3, 6).Value = "'" + dateTimeString;
|
||||
ws.Cell(3, 7).Value = "'" + dateTimeFormattedString;
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// Boolean
|
||||
|
||||
// Fill a cell with a boolean
|
||||
var cellBoolean = ws.Cell(4, 2);
|
||||
cellBoolean.Value = true;
|
||||
|
||||
// Extract the boolean in different ways
|
||||
Boolean boolean1 = (Boolean)cellBoolean.Value;
|
||||
Boolean boolean2 = cellBoolean.GetBoolean();
|
||||
Boolean boolean3 = cellBoolean.GetValue<Boolean>();
|
||||
String booleanString = cellBoolean.GetString();
|
||||
String booleanFormattedString = cellBoolean.GetFormattedString();
|
||||
|
||||
// Set the values back to cells
|
||||
// The apostrophe is to force ClosedXML to treat the boolean as a string
|
||||
ws.Cell(4, 3).Value = boolean1;
|
||||
ws.Cell(4, 4).Value = boolean2;
|
||||
ws.Cell(4, 5).Value = boolean3;
|
||||
ws.Cell(4, 6).Value = "'" + booleanString;
|
||||
ws.Cell(4, 7).Value = "'" + booleanFormattedString;
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// Double
|
||||
|
||||
// Fill a cell with a double
|
||||
var cellDouble = ws.Cell(5, 2);
|
||||
cellDouble.Value = 1234.567;
|
||||
cellDouble.Style.NumberFormat.Format = "#,##0.00";
|
||||
|
||||
// Extract the double in different ways
|
||||
Double double1 = (Double)cellDouble.Value;
|
||||
Double double2 = cellDouble.GetDouble();
|
||||
Double double3 = cellDouble.GetValue<Double>();
|
||||
String doubleString = cellDouble.GetString();
|
||||
String doubleFormattedString = cellDouble.GetFormattedString();
|
||||
|
||||
// Set the values back to cells
|
||||
// The apostrophe is to force ClosedXML to treat the double as a string
|
||||
ws.Cell(5, 3).Value = double1;
|
||||
ws.Cell(5, 4).Value = double2;
|
||||
ws.Cell(5, 5).Value = double3;
|
||||
ws.Cell(5, 6).Value = "'" + doubleString;
|
||||
ws.Cell(5, 7).Value = "'" + doubleFormattedString;
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// String
|
||||
|
||||
// Fill a cell with a string
|
||||
var cellString = ws.Cell(6, 2);
|
||||
cellString.Value = "Test Case";
|
||||
|
||||
// Extract the string in different ways
|
||||
String string1 = (String)cellString.Value;
|
||||
String string2 = cellString.GetString();
|
||||
String string3 = cellString.GetValue<String>();
|
||||
String stringString = cellString.GetString();
|
||||
String stringFormattedString = cellString.GetFormattedString();
|
||||
|
||||
// Set the values back to cells
|
||||
ws.Cell(6, 3).Value = string1;
|
||||
ws.Cell(6, 4).Value = string2;
|
||||
ws.Cell(6, 5).Value = string3;
|
||||
ws.Cell(6, 6).Value = stringString;
|
||||
ws.Cell(6, 7).Value = stringFormattedString;
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// TimeSpan
|
||||
|
||||
// Fill a cell with a timeSpan
|
||||
var cellTimeSpan = ws.Cell(7, 2);
|
||||
cellTimeSpan.Value = new TimeSpan(1, 2, 31, 45);
|
||||
|
||||
// Extract the timeSpan in different ways
|
||||
TimeSpan timeSpan1 = (TimeSpan)cellTimeSpan.Value;
|
||||
TimeSpan timeSpan2 = cellTimeSpan.GetTimeSpan();
|
||||
TimeSpan timeSpan3 = cellTimeSpan.GetValue<TimeSpan>();
|
||||
String timeSpanString = "'" + cellTimeSpan.GetString();
|
||||
String timeSpanFormattedString = "'" + cellTimeSpan.GetFormattedString();
|
||||
|
||||
// Set the values back to cells
|
||||
ws.Cell(7, 3).Value = timeSpan1;
|
||||
ws.Cell(7, 4).Value = timeSpan2;
|
||||
ws.Cell(7, 5).Value = timeSpan3;
|
||||
ws.Cell(7, 6).Value = timeSpanString;
|
||||
ws.Cell(7, 7).Value = timeSpanFormattedString;
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// Do some formatting
|
||||
ws.Columns("B:G").Width = 20;
|
||||
var rngTitle = ws.Range("B2:G2");
|
||||
rngTitle.Style.Font.Bold = true;
|
||||
rngTitle.Style.Fill.BackgroundColor = XLColor.Cyan;
|
||||
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
ws = workbook.AddWorksheet("Test Whitespace");
|
||||
ws.FirstCell().Value = "' ";
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class Collections : 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();
|
||||
var ws = wb.Worksheets.Add("Collections");
|
||||
|
||||
// From a list of strings
|
||||
var listOfStrings = new List<String>();
|
||||
listOfStrings.Add("House");
|
||||
listOfStrings.Add("Car");
|
||||
ws.Cell(1, 1).Value = "Strings";
|
||||
ws.Cell(1, 1).AsRange().AddToNamed("Titles");
|
||||
ws.Cell(2, 1).Value = listOfStrings;
|
||||
|
||||
// From a list of arrays
|
||||
var listOfArr = new List<Int32[]>();
|
||||
listOfArr.Add(new Int32[] { 1, 2, 3 });
|
||||
listOfArr.Add(new Int32[] { 1 });
|
||||
listOfArr.Add(new Int32[] { 1, 2, 3, 4, 5, 6 });
|
||||
ws.Cell(1, 3).Value = "Arrays";
|
||||
ws.Range(1, 3, 1, 8).Merge().AddToNamed("Titles");
|
||||
ws.Cell(2, 3).Value = listOfArr;
|
||||
|
||||
// From a DataTable
|
||||
var dataTable = GetTable();
|
||||
ws.Cell(6, 1).Value = "DataTable";
|
||||
ws.Range(6, 1, 6, 4).Merge().AddToNamed("Titles");
|
||||
ws.Cell(7, 1).Value = dataTable;
|
||||
|
||||
// From a query
|
||||
var list = new List<Person>();
|
||||
list.Add(new Person() { Name = "John", Age = 30, House = "On Elm St." });
|
||||
list.Add(new Person() { Name = "Mary", Age = 15, House = "On Main St." });
|
||||
list.Add(new Person() { Name = "Luis", Age = 21, House = "On 23rd St." });
|
||||
list.Add(new Person() { Name = "Henry", Age = 45, House = "On 5th Ave." });
|
||||
|
||||
var people = from p in list
|
||||
where p.Age >= 21
|
||||
select new { p.Name, p.House, p.Age };
|
||||
|
||||
ws.Cell(6, 6).Value = "Query";
|
||||
ws.Range(6, 6, 6, 8).Merge().AddToNamed("Titles");
|
||||
ws.Cell(7, 6).Value = people;
|
||||
|
||||
|
||||
// Prepare the style for the titles
|
||||
var titlesStyle = wb.Style;
|
||||
titlesStyle.Font.Bold = true;
|
||||
titlesStyle.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
|
||||
titlesStyle.Fill.BackgroundColor = XLColor.Cyan;
|
||||
|
||||
// Format all titles in one shot
|
||||
wb.NamedRanges.NamedRange("Titles").Ranges.Style = titlesStyle;
|
||||
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
class Person
|
||||
{
|
||||
public String House { get; set; }
|
||||
public String Name { get; set; }
|
||||
public Int32 Age { get; set; }
|
||||
}
|
||||
|
||||
// Private
|
||||
private DataTable GetTable()
|
||||
{
|
||||
|
||||
DataTable table = new DataTable();
|
||||
table.Columns.Add("Dosage", typeof(int));
|
||||
table.Columns.Add("Drug", typeof(string));
|
||||
table.Columns.Add("Patient", typeof(string));
|
||||
table.Columns.Add("Date", typeof(DateTime));
|
||||
|
||||
table.Rows.Add(25, "Indocin", "David", new DateTime(2000, 1, 1));
|
||||
table.Rows.Add(50, "Enebrel", "Sam", new DateTime(2000, 1, 2));
|
||||
table.Rows.Add(10, "Hydralazine", "Christoff", new DateTime(2000, 1, 3));
|
||||
table.Rows.Add(21, "Combivent", "Janet", new DateTime(2000, 1, 4));
|
||||
table.Rows.Add(100, "Dilantin", "Melanie", new DateTime(2000, 1, 5));
|
||||
return table;
|
||||
}
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class CopyingRowsAndColumns : 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 workbook = new XLWorkbook();
|
||||
|
||||
var originalSheet = workbook.Worksheets.Add("original");
|
||||
|
||||
originalSheet.Cell("A2").SetValue("test value");
|
||||
originalSheet.Range("A2:E2").Merge();
|
||||
|
||||
originalSheet.Cell("F1").SetValue("test value").Style.Alignment.SetTopToBottom();
|
||||
originalSheet.Range("F1:F6").Merge();
|
||||
|
||||
var fromRow = workbook.Worksheets.Add("From a Row");
|
||||
fromRow.Cell(1, 1).SetValue("Row to Row:");
|
||||
originalSheet.Row(2).CopyTo(fromRow.Row(2));
|
||||
fromRow.Cell(3, 1).SetValue("Row to Range:");
|
||||
originalSheet.Row(2).CopyTo(fromRow.Row(4).AsRange());
|
||||
fromRow.Cell(5, 1).SetValue("Row to Cell:");
|
||||
originalSheet.Row(2).CopyTo(fromRow.Row(6).FirstCell());
|
||||
|
||||
var fromRange = workbook.Worksheets.Add("From a Range");
|
||||
fromRange.Cell(1, 1).SetValue("Range to Row:");
|
||||
originalSheet.Row(2).AsRange().CopyTo(fromRange.Row(2));
|
||||
fromRange.Cell(3, 1).SetValue("Range to Range:");
|
||||
originalSheet.Row(2).AsRange().CopyTo(fromRange.Row(4).AsRange());
|
||||
fromRange.Cell(5, 1).SetValue("Range to Cell:");
|
||||
originalSheet.Row(2).AsRange().CopyTo(fromRange.Row(6).FirstCell());
|
||||
|
||||
CopyRowAsRange(originalSheet, 2, fromRange, 8);
|
||||
|
||||
var fromColumn = workbook.Worksheets.Add("From a Column to Column");
|
||||
fromColumn.Cell(1, 1).SetValue("Column to Column:").Style.Alignment.SetTopToBottom();
|
||||
originalSheet.Column("F").CopyTo(fromColumn.Column(2));
|
||||
fromColumn.Cell(1, 3).SetValue("Column to Range:").Style.Alignment.SetTopToBottom();
|
||||
originalSheet.Column("F").CopyTo(fromColumn.Column(4).AsRange());
|
||||
fromColumn.Cell(1, 5).SetValue("Column to Cell:").Style.Alignment.SetTopToBottom();
|
||||
originalSheet.Column("F").CopyTo(fromColumn.Column(6).FirstCell());
|
||||
|
||||
var fromRangeToColumn = workbook.Worksheets.Add("From a Range to Column");
|
||||
fromRangeToColumn.Cell(1, 1).SetValue("Range to Column:").Style.Alignment.SetTopToBottom();
|
||||
originalSheet.Column("F").AsRange().CopyTo(fromRangeToColumn.Column(2));
|
||||
fromRangeToColumn.Cell(1, 3).SetValue("Range to Range:").Style.Alignment.SetTopToBottom();
|
||||
originalSheet.Column("F").AsRange().CopyTo(fromRangeToColumn.Column(4).AsRange());
|
||||
fromRangeToColumn.Cell(1, 5).SetValue("Range to Cell:").Style.Alignment.SetTopToBottom();
|
||||
originalSheet.Column("F").AsRange().CopyTo(fromRangeToColumn.Column(6).FirstCell());
|
||||
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
private static void CopyRowAsRange(IXLWorksheet originalSheet, int originalRowNumber, IXLWorksheet destSheet, int destRowNumber)
|
||||
{
|
||||
{
|
||||
var destinationRow = destSheet.Row(destRowNumber);
|
||||
destinationRow.Clear();
|
||||
|
||||
var originalRow = originalSheet.Row(originalRowNumber);
|
||||
int columnNumber = originalRow.LastCellUsed(XLCellsUsedOptions.All).Address.ColumnNumber;
|
||||
|
||||
var originalRange = originalSheet.Range(originalRowNumber, 1, originalRowNumber, columnNumber);
|
||||
var destRange = destSheet.Range(destRowNumber, 1, destRowNumber, columnNumber);
|
||||
originalRange.CopyTo(destRange);
|
||||
}
|
||||
}
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using ClosedXML.Excel;
|
||||
using ClosedXML_Examples.Tables;
|
||||
using System.IO;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class CopyingWorksheets : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
string tempFile1 = ExampleHelper.GetTempFilePath(filePath);
|
||||
string tempFile2 = ExampleHelper.GetTempFilePath(filePath);
|
||||
try
|
||||
{
|
||||
new UsingTables().Create(tempFile1);
|
||||
var wb = new XLWorkbook(tempFile1);
|
||||
|
||||
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);
|
||||
var wbSource = new XLWorkbook(tempFile2);
|
||||
wbSource.Worksheet(1).CopyTo(wb, "Copy From Other");
|
||||
|
||||
// Save the workbook with the 2 copies
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(tempFile1))
|
||||
{
|
||||
File.Delete(tempFile1);
|
||||
}
|
||||
if (File.Exists(tempFile2))
|
||||
{
|
||||
File.Delete(tempFile2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class DataTypes : 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 workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Data Types");
|
||||
|
||||
var co = 2;
|
||||
var ro = 1;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Plain Text:";
|
||||
ws.Cell(ro, co + 1).Value = "Hello World.";
|
||||
|
||||
ws.Cell(++ro, co).Value = "Plain Date:";
|
||||
ws.Cell(ro, co + 1).Value = new DateTime(2010, 9, 2);
|
||||
|
||||
ws.Cell(++ro, co).Value = "Plain DateTime:";
|
||||
ws.Cell(ro, co + 1).Value = new DateTime(2010, 9, 2, 13, 45, 22);
|
||||
|
||||
ws.Cell(++ro, co).Value = "Plain Boolean:";
|
||||
ws.Cell(ro, co + 1).Value = true;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Plain Number:";
|
||||
ws.Cell(ro, co + 1).Value = 123.45;
|
||||
|
||||
ws.Cell(++ro, co).Value = "TimeSpan:";
|
||||
ws.Cell(ro, co + 1).Value = new TimeSpan(33, 45, 22);
|
||||
|
||||
ro++;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Decimal Number:";
|
||||
ws.Cell(ro, co + 1).Value = 123.45m;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Float Number:";
|
||||
ws.Cell(ro, co + 1).Value = 123.45f;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Double Number:";
|
||||
ws.Cell(ro, co + 1).Value = 123.45d;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Large Double Number:";
|
||||
ws.Cell(ro, co + 1).Value = 9.999E307d;
|
||||
|
||||
ro++;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Explicit Text:";
|
||||
ws.Cell(ro, co + 1).Value = "'Hello World.";
|
||||
|
||||
ws.Cell(++ro, co).Value = "Date as Text:";
|
||||
ws.Cell(ro, co + 1).Value = "'" + new DateTime(2010, 9, 2).ToString();
|
||||
|
||||
ws.Cell(++ro, co).Value = "DateTime as Text:";
|
||||
ws.Cell(ro, co + 1).Value = "'" + new DateTime(2010, 9, 2, 13, 45, 22).ToString();
|
||||
|
||||
ws.Cell(++ro, co).Value = "Boolean as Text:";
|
||||
ws.Cell(ro, co + 1).Value = "'" + true.ToString();
|
||||
|
||||
ws.Cell(++ro, co).Value = "Number as Text:";
|
||||
ws.Cell(ro, co + 1).Value = "'123.45";
|
||||
|
||||
ws.Cell(++ro, co).Value = "Number with @ format:";
|
||||
ws.Cell(ro, co + 1).Style.NumberFormat.Format = "@";
|
||||
ws.Cell(ro, co + 1).Value = 123.45;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Format number with @:";
|
||||
ws.Cell(ro, co + 1).Value = 123.45;
|
||||
ws.Cell(ro, co + 1).Style.NumberFormat.Format = "@";
|
||||
|
||||
ws.Cell(++ro, co).Value = "TimeSpan as Text:";
|
||||
ws.Cell(ro, co + 1).Value = "'" + new TimeSpan(33, 45, 22).ToString();
|
||||
|
||||
ro++;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Changing Data Types:";
|
||||
|
||||
ro++;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Date to Text:";
|
||||
ws.Cell(ro, co + 1).Value = new DateTime(2010, 9, 2);
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.Text;
|
||||
|
||||
ws.Cell(++ro, co).Value = "DateTime to Text:";
|
||||
ws.Cell(ro, co + 1).Value = new DateTime(2010, 9, 2, 13, 45, 22);
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.Text;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Boolean to Text:";
|
||||
ws.Cell(ro, co + 1).Value = true;
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.Text;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Number to Text:";
|
||||
ws.Cell(ro, co + 1).Value = 123.45;
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.Text;
|
||||
|
||||
ws.Cell(++ro, co).Value = "TimeSpan to Text:";
|
||||
ws.Cell(ro, co + 1).Value = new TimeSpan(33, 45, 22);
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.Text;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Text to Date:";
|
||||
ws.Cell(ro, co + 1).Value = "'" + new DateTime(2010, 9, 2).ToString();
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.DateTime;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Text to DateTime:";
|
||||
ws.Cell(ro, co + 1).Value = "'" + new DateTime(2010, 9, 2, 13, 45, 22).ToString();
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.DateTime;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Text to Boolean:";
|
||||
ws.Cell(ro, co + 1).Value = "'" + true.ToString();
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.Boolean;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Text to Number:";
|
||||
ws.Cell(ro, co + 1).Value = "'123.45";
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.Number;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Percentage Text to Number:";
|
||||
ws.Cell(ro, co + 1).Value = "'55.12%";
|
||||
ws.Cell(ro, co + 1).Style.NumberFormat.SetNumberFormatId((int)XLPredefinedFormat.Number.PercentPrecision2);
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.Number;
|
||||
|
||||
ws.Cell(++ro, co).Value = "@ format to Number:";
|
||||
ws.Cell(ro, co + 1).Style.NumberFormat.Format = "@";
|
||||
ws.Cell(ro, co + 1).Value = 123.45;
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.Number;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Text to TimeSpan:";
|
||||
ws.Cell(ro, co + 1).Value = "'" + new TimeSpan(33, 45, 22).ToString();
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.TimeSpan;
|
||||
|
||||
ro++;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Formatted Date to Text:";
|
||||
ws.Cell(ro, co + 1).Value = new DateTime(2010, 9, 2);
|
||||
ws.Cell(ro, co + 1).Style.DateFormat.Format = "yyyy-MM-dd";
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.Text;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Formatted Number to Text:";
|
||||
ws.Cell(ro, co + 1).Value = 12345.6789;
|
||||
ws.Cell(ro, co + 1).Style.NumberFormat.Format = "#,##0.00";
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.Text;
|
||||
|
||||
ro++;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Blank Text:";
|
||||
ws.Cell(ro, co + 1).Value = 12345.6789;
|
||||
ws.Cell(ro, co + 1).Style.NumberFormat.Format = "#,##0.00";
|
||||
ws.Cell(ro, co + 1).DataType = XLDataType.Text;
|
||||
ws.Cell(ro, co + 1).Value = "";
|
||||
|
||||
ro++;
|
||||
|
||||
// Using inline strings (few users will ever need to use this feature)
|
||||
//
|
||||
// By default all strings are stored as shared so one block of text
|
||||
// can be reference by multiple cells.
|
||||
// You can override this by setting the .ShareString property to false
|
||||
ws.Cell(++ro, co).Value = "Inline String:";
|
||||
var cell = ws.Cell(ro, co + 1);
|
||||
cell.Value = "Not Shared";
|
||||
cell.ShareString = false;
|
||||
|
||||
// To view all shared strings (all texts in the workbook actually), use the following:
|
||||
// workbook.GetSharedStrings()
|
||||
|
||||
ws.Cell(++ro, co)
|
||||
.SetDataType(XLDataType.Text)
|
||||
.SetDataType(XLDataType.Boolean)
|
||||
.SetDataType(XLDataType.DateTime)
|
||||
.SetDataType(XLDataType.Number)
|
||||
.SetDataType(XLDataType.TimeSpan)
|
||||
.SetDataType(XLDataType.Text)
|
||||
.SetDataType(XLDataType.TimeSpan)
|
||||
.SetDataType(XLDataType.Number)
|
||||
.SetDataType(XLDataType.DateTime)
|
||||
.SetDataType(XLDataType.Boolean)
|
||||
.SetDataType(XLDataType.Text);
|
||||
|
||||
ws.Columns(2, 3).AdjustToContents();
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
using System.Threading;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class DataTypesUnderDifferentCulture : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
var backupCulture = Thread.CurrentThread.CurrentCulture;
|
||||
|
||||
// Set thread culture to French, which should format numbers using decimal COMMA
|
||||
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
|
||||
|
||||
string tempFile = ExampleHelper.GetTempFilePath(filePath);
|
||||
try
|
||||
{
|
||||
new DataTypes().Create(tempFile);
|
||||
var workbook = new XLWorkbook(tempFile);
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Thread.CurrentThread.CurrentCulture = backupCulture;
|
||||
if (File.Exists(tempFile))
|
||||
{
|
||||
File.Delete(tempFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class DataValidation : 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();
|
||||
var ws = wb.Worksheets.Add("Data Validation");
|
||||
|
||||
// Decimal between 1 and 5
|
||||
ws.Cell(1, 1).SetDataValidation().Decimal.Between(1, 5);
|
||||
|
||||
// Whole number equals 2
|
||||
var dv1 = ws.Range("A2:A3").SetDataValidation();
|
||||
dv1.WholeNumber.EqualTo(2);
|
||||
// Change the error message
|
||||
dv1.ErrorStyle = XLErrorStyle.Warning;
|
||||
dv1.ErrorTitle = "Number out of range";
|
||||
dv1.ErrorMessage = "This cell only allows the number 2.";
|
||||
|
||||
// Date after the millenium
|
||||
var dv2 = ws.Cell("A4").SetDataValidation();
|
||||
dv2.Date.EqualOrGreaterThan(new DateTime(2000, 1, 1));
|
||||
// Change the input message
|
||||
dv2.InputTitle = "Can't party like it's 1999.";
|
||||
dv2.InputMessage = "Please enter a date in this century.";
|
||||
|
||||
// From a list
|
||||
ws.Cell("C1").Value = "Yes";
|
||||
ws.Cell("C2").Value = "No";
|
||||
ws.Cell("A5").SetDataValidation().List(ws.Range("C1:C2"));
|
||||
|
||||
ws.Range("C1:C2").AddToNamed("YesNo");
|
||||
ws.Cell("A6").SetDataValidation().List("=YesNo");
|
||||
|
||||
// Intersecting dataValidations
|
||||
ws.Range("B1:B4").SetDataValidation().WholeNumber.EqualTo(1);
|
||||
ws.Range("B3:B4").SetDataValidation().WholeNumber.EqualTo(2);
|
||||
|
||||
|
||||
// Validate with multiple ranges
|
||||
var ws2 = wb.Worksheets.Add("Validate Ranges");
|
||||
var rng1 = ws2.Ranges("A1:B2,B4:D7,F4:G5");
|
||||
rng1.Style.Fill.SetBackgroundColor(XLColor.YellowGreen);
|
||||
var rng1Validation = rng1.SetDataValidation();
|
||||
rng1Validation.Decimal.EqualTo(1);
|
||||
rng1Validation.IgnoreBlanks = false;
|
||||
|
||||
var rng2 = ws2.Range("A11:E14");
|
||||
rng2.Style.Fill.SetBackgroundColor(XLColor.YellowGreen);
|
||||
var rng2Validation = rng2.SetDataValidation();
|
||||
rng2Validation.Decimal.EqualTo(2);
|
||||
rng2Validation.IgnoreBlanks = false;
|
||||
|
||||
var rng3 = ws2.Range("B2:B12");
|
||||
//rng3.Style.Fill.SetBackgroundColor(XLColor.YellowGreen);
|
||||
var rng3Validation = rng3.SetDataValidation();
|
||||
rng3Validation.Decimal.EqualTo(3);
|
||||
rng3Validation.IgnoreBlanks = true;
|
||||
|
||||
var rng4 = ws2.Range("D5:D6");
|
||||
//rng4.Style.Fill.SetBackgroundColor(XLColor.YellowGreen);
|
||||
var rng4Validation = rng4.SetDataValidation();
|
||||
rng4Validation.Decimal.EqualTo(4);
|
||||
rng4Validation.IgnoreBlanks = true;
|
||||
|
||||
var rng5 = ws2.Range("C13:C14");
|
||||
//rng5.Style.Fill.SetBackgroundColor(XLColor.YellowGreen);
|
||||
var rng5Validation = rng5.SetDataValidation();
|
||||
rng5Validation.Decimal.EqualTo(5);
|
||||
rng5Validation.IgnoreBlanks = true;
|
||||
|
||||
var rng6 = ws2.Range("D11:D12");
|
||||
//rng6.Style.Fill.SetBackgroundColor(XLColor.YellowGreen);
|
||||
var rng6Validation = rng6.SetDataValidation();
|
||||
rng6Validation.Decimal.EqualTo(5);
|
||||
rng6Validation.IgnoreBlanks = true;
|
||||
|
||||
var rng7 = ws2.Range("G4:G5");
|
||||
//rng7.Style.Fill.SetBackgroundColor(XLColor.YellowGreen);
|
||||
var rng7Validation = rng7.SetDataValidation();
|
||||
rng7Validation.Decimal.EqualTo(5);
|
||||
rng7Validation.IgnoreBlanks = true;
|
||||
|
||||
ws.CopyTo(ws.Name + " - Copy");
|
||||
ws2.CopyTo(ws2.Name + " - Copy");
|
||||
|
||||
wb.AddWorksheet("Copy From Range 1").FirstCell().Value = ws.RangeUsed(XLCellsUsedOptions.All);
|
||||
wb.AddWorksheet("Copy From Range 2").FirstCell().Value = ws2.RangeUsed(XLCellsUsedOptions.All);
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class Formulas : IXLExample
|
||||
{
|
||||
public virtual void Create(String filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
var ws = wb.Worksheets.Add("Formulas");
|
||||
|
||||
ws.Cell(1, 1).Value = "Num1";
|
||||
ws.Cell(1, 2).Value = "Num2";
|
||||
ws.Cell(1, 3).Value = "Total";
|
||||
ws.Cell(1, 4).Value = "cell.FormulaA1";
|
||||
ws.Cell(1, 5).Value = "cell.FormulaR1C1";
|
||||
ws.Cell(1, 6).Value = "cell.Value";
|
||||
ws.Cell(1, 7).Value = "Are Equal?";
|
||||
|
||||
ws.Cell(2, 1).Value = 1;
|
||||
ws.Cell(2, 2).Value = 2;
|
||||
var cellWithFormulaA1 = ws.Cell(2, 3);
|
||||
// Use A1 notation
|
||||
cellWithFormulaA1.FormulaA1 = "=A2+$B$2"; // The equal sign (=) in a formula is optional
|
||||
ws.Cell(2, 4).Value = cellWithFormulaA1.FormulaA1;
|
||||
ws.Cell(2, 5).Value = cellWithFormulaA1.FormulaR1C1;
|
||||
ws.Cell(2, 6).Value = cellWithFormulaA1.Value;
|
||||
|
||||
ws.Cell(3, 1).Value = 1;
|
||||
ws.Cell(3, 2).Value = 2;
|
||||
var cellWithFormulaR1C1 = ws.Cell(3, 3);
|
||||
// Use R1C1 notation
|
||||
cellWithFormulaR1C1.FormulaR1C1 = "RC[-2]+R3C2"; // The equal sign (=) in a formula is optional
|
||||
ws.Cell(3, 4).Value = cellWithFormulaR1C1.FormulaA1;
|
||||
ws.Cell(3, 5).Value = cellWithFormulaR1C1.FormulaR1C1;
|
||||
ws.Cell(3, 6).Value = cellWithFormulaR1C1.Value;
|
||||
|
||||
ws.Cell(4, 1).Value = "A";
|
||||
ws.Cell(4, 2).Value = "B";
|
||||
var cellWithStringFormula = ws.Cell(4, 3);
|
||||
|
||||
// Use R1C1 notation
|
||||
cellWithStringFormula.FormulaR1C1 = "=\"Test\" & RC[-2] & \"R3C2\"";
|
||||
ws.Cell(4, 4).Value = cellWithStringFormula.FormulaA1;
|
||||
ws.Cell(4, 5).Value = cellWithStringFormula.FormulaR1C1;
|
||||
ws.Cell(4, 6).Value = cellWithStringFormula.Value;
|
||||
|
||||
// Setting the formula of a range
|
||||
var rngData = ws.Range(2, 1, 4, 7);
|
||||
rngData.LastColumn().FormulaR1C1 = "=IF(RC[-4]=RC[-1],\"Yes\", \"No\")";
|
||||
|
||||
// Using an array formula:
|
||||
// Just put the formula between curly braces
|
||||
ws.Cell("A6").Value = "Array Formula: ";
|
||||
ws.Cell("B6").FormulaA1 = "{A2+A3}";
|
||||
ws.Range("C6:D6").FormulaA1 = "{TRANSPOSE(A2:A3)}";
|
||||
|
||||
ws.Range(1, 1, 1, 7).Style.Fill.BackgroundColor = XLColor.Cyan;
|
||||
ws.Range(1, 1, 1, 7).Style.Font.Bold = true;
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
// You can also change the reference notation:
|
||||
wb.ReferenceStyle = XLReferenceStyle.R1C1;
|
||||
|
||||
// And the workbook calculation mode:
|
||||
wb.CalculateMode = XLCalculateMode.Auto;
|
||||
|
||||
ws.Range("A10").AddToNamed("A10_R1C1_A10_R1C1");
|
||||
ws.Cell("A10").Value = 0;
|
||||
ws.Cell("A11").FormulaA1 = "A2 + A10_R1C1_A10_R1C1";
|
||||
ws.Cell("A12").FormulaR1C1 = "R2C1 + A10_R1C1_A10_R1C1";
|
||||
ws.Cell("A13").FormulaR1C1 = "=SUM(R[-5]:R[-4])";
|
||||
ws.Cell("A14").FormulaA1 = "=SUM(8:9)";
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class FormulasWithEvaluation : Formulas
|
||||
{
|
||||
public override void Create(string filePath)
|
||||
{
|
||||
base.Create(filePath);
|
||||
using (var wb = new XLWorkbook(filePath))
|
||||
{
|
||||
wb.Save(true, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class FreezePanes : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
// Freeze rows and columns in one shot
|
||||
var ws1 = wb.AddWorksheet("Freeze1");
|
||||
ws1.Cell(5, 5).SetActive();
|
||||
ws1.SheetView.Freeze(3, 3);
|
||||
|
||||
// You can also be more specific on what you want to freeze
|
||||
// For example:
|
||||
var ws2 = wb.AddWorksheet("FreezeRows");
|
||||
ws2.Cell(5, 5).SetActive();
|
||||
ws2.SheetView.FreezeRows(3);
|
||||
|
||||
var ws3 = wb.AddWorksheet("FreezeColumns");
|
||||
ws3.Cell(5, 5).SetActive();
|
||||
ws3.SheetView.FreezeColumns(3);
|
||||
|
||||
var wsSplit = wb.AddWorksheet("Split View");
|
||||
wsSplit.Cell(2, 2).SetActive();
|
||||
wsSplit.SheetView.SplitRow = 3;
|
||||
wsSplit.SheetView.SplitColumn = 3;
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class HideSheets : 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();
|
||||
|
||||
wb.Worksheets.Add("First Hidden").Hide();
|
||||
wb.Worksheets.Add("Visible");
|
||||
wb.Worksheets.Add("Unhidden").Hide().Unhide();
|
||||
wb.Worksheets.Add("VeryHidden").Visibility = XLWorksheetVisibility.VeryHidden;
|
||||
wb.Worksheets.Add("Last Hidden").Hide();
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class HideUnhide : 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();
|
||||
var ws = wb.Worksheets.Add("Hide Rows Columns");
|
||||
|
||||
ws.Columns(1, 3).Hide();
|
||||
ws.Rows(1, 3).Hide();
|
||||
|
||||
ws.Column(2).Unhide();
|
||||
ws.Row(2).Unhide();
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class Hyperlinks : 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();
|
||||
var ws = wb.Worksheets.Add("Hyperlinks");
|
||||
wb.Worksheets.Add("Second Sheet");
|
||||
|
||||
Int32 ro = 0;
|
||||
|
||||
// You can create a link with pretty much anything you can put on a
|
||||
// browser: http, ftp, mailto, gopher, news, nntp, etc.
|
||||
|
||||
ws.Cell(++ro, 1).Value = "Link to a web page, no tooltip - Yahoo!";
|
||||
ws.Cell(ro, 1).Hyperlink = new XLHyperlink(@"http://www.yahoo.com");
|
||||
|
||||
ws.Cell(++ro, 1).Value = "Link to a web page, with a tooltip - Yahoo!";
|
||||
ws.Cell(ro, 1).Hyperlink = new XLHyperlink(@"http://www.yahoo.com", "Click to go to Yahoo!");
|
||||
|
||||
ws.Cell(++ro, 1).Value = "Link to a file - same folder";
|
||||
ws.Cell(ro, 1).Hyperlink = new XLHyperlink("Test.xlsx");
|
||||
|
||||
ws.Cell(++ro, 1).Value = "Link to a file - Absolute";
|
||||
ws.Cell(ro, 1).Hyperlink = new XLHyperlink(@"D:\Test.xlsx");
|
||||
|
||||
ws.Cell(++ro, 1).Value = "Link to a file - relative address";
|
||||
ws.Cell(ro, 1).Hyperlink = new XLHyperlink(@"../Test.xlsx");
|
||||
|
||||
ws.Cell(++ro, 1).Value = "Link to an address in this worksheet";
|
||||
ws.Cell(ro, 1).Hyperlink = new XLHyperlink("B1");
|
||||
|
||||
ws.Cell(++ro, 1).Value = "Link to an address in another worksheet";
|
||||
ws.Cell(ro, 1).Hyperlink = new XLHyperlink("'Second Sheet'!A1");
|
||||
|
||||
// You can also set the properties of a hyperlink directly:
|
||||
|
||||
ws.Cell(++ro, 1).Value = "Link to a range in this worksheet";
|
||||
ws.Cell(ro, 1).Hyperlink.InternalAddress = "B1:C2";
|
||||
ws.Cell(ro, 1).Hyperlink.Tooltip = "SquareBox";
|
||||
|
||||
ws.Cell(++ro, 1).Value = "Link to an email message";
|
||||
ws.Cell(ro, 1).Hyperlink.ExternalAddress = new Uri(@"mailto:SantaClaus@NorthPole.com?subject=Presents");
|
||||
|
||||
// Deleting a hyperlink
|
||||
ws.Cell(++ro, 1).Value = "This is no longer a link";
|
||||
ws.Cell(ro, 1).Hyperlink.InternalAddress = "A1";
|
||||
ws.Cell(ro, 1).Hyperlink.Delete();
|
||||
|
||||
// Setting a hyperlink preserves previous formatting:
|
||||
ws.Cell(++ro, 1).Value = "Odd looking link";
|
||||
ws.Cell(ro, 1).Style.Font.FontColor = XLColor.Red;
|
||||
ws.Cell(ro, 1).Style.Font.Underline = XLFontUnderlineValues.Double;
|
||||
ws.Cell(ro, 1).Hyperlink = new XLHyperlink(ws.Range("B1:C2"));
|
||||
|
||||
// Hyperlink via formula
|
||||
ws.Cell( ++ro, 1 ).SetValue( "Send Email" )
|
||||
.SetFormulaA1( "=HYPERLINK(\"mailto:test@test.com\", \"Send Email\")" )
|
||||
.Hyperlink = new XLHyperlink( "mailto:test@test.com", "'Send Email'" );
|
||||
|
||||
// List all hyperlinks in a worksheet:
|
||||
var hyperlinksInWorksheet = ws.Hyperlinks;
|
||||
|
||||
// List all hyperlinks in a range:
|
||||
var hyperlinksInRange = ws.Range("A1:A3").Hyperlinks;
|
||||
|
||||
// Clearing a cell with a hyperlink
|
||||
ws.Cell(++ro, 1).Value = "ERROR!";
|
||||
ws.Cell(ro, 1).Hyperlink.InternalAddress = "A1";
|
||||
ws.Cell(ro, 1).Clear();
|
||||
|
||||
// Deleting a cell with a hyperlink
|
||||
ws.Cell(++ro, 1).Value = "ERROR!";
|
||||
ws.Cell(ro, 1).Hyperlink.InternalAddress = "A1";
|
||||
ws.Cell(ro, 1).Clear();
|
||||
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class InsertingData : IXLExample
|
||||
{
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
var ws = wb.Worksheets.Add("Inserting Data");
|
||||
|
||||
// From a list of strings
|
||||
var listOfStrings = new List<String>();
|
||||
listOfStrings.Add("House");
|
||||
listOfStrings.Add("001");
|
||||
ws.Cell(1, 1).Value = "From Strings";
|
||||
ws.Cell(1, 1).AsRange().AddToNamed("Titles");
|
||||
ws.Cell(2, 1).InsertData(listOfStrings);
|
||||
|
||||
// From a list of arrays
|
||||
var listOfArr = new List<Int32[]>();
|
||||
listOfArr.Add(new Int32[] { 1, 2, 3 });
|
||||
listOfArr.Add(new Int32[] { 1 });
|
||||
listOfArr.Add(new Int32[] { 1, 2, 3, 4, 5, 6 });
|
||||
ws.Cell(1, 3).Value = "From Arrays";
|
||||
ws.Range(1, 3, 1, 8).Merge().AddToNamed("Titles");
|
||||
ws.Cell(2, 3).InsertData(listOfArr);
|
||||
|
||||
// From a DataTable
|
||||
var dataTable = GetTable();
|
||||
ws.Cell(6, 1).Value = "From DataTable";
|
||||
ws.Range(6, 1, 6, 4).Merge().AddToNamed("Titles");
|
||||
ws.Cell(7, 1).InsertData(dataTable);
|
||||
|
||||
// From a query
|
||||
var list = new List<Person>();
|
||||
list.Add(new Person() { Name = "John", Age = 30, House = "On Elm St." });
|
||||
list.Add(new Person() { Name = "Mary", Age = 15, House = "On Main St." });
|
||||
list.Add(new Person() { Name = "Luis", Age = 21, House = "On 23rd St." });
|
||||
list.Add(new Person() { Name = "Henry", Age = 45, House = "On 5th Ave." });
|
||||
|
||||
var people = from p in list
|
||||
where p.Age >= 21
|
||||
select new { p.Name, p.House, p.Age };
|
||||
|
||||
ws.Cell(6, 6).Value = "From Query";
|
||||
ws.Range(6, 6, 6, 8).Merge().AddToNamed("Titles");
|
||||
ws.Cell(7, 6).InsertData(people);
|
||||
|
||||
ws.Cell(11, 6).Value = "From List";
|
||||
ws.Range(11, 6, 11, 9).Merge().AddToNamed("Titles");
|
||||
ws.Cell(12, 6).InsertData(list);
|
||||
|
||||
ws.Cell("A13").Value = "Transposed";
|
||||
ws.Range(13, 1, 13, 3).Merge().AddToNamed("Titles");
|
||||
ws.Cell("A14").InsertData(people.AsEnumerable(), true);
|
||||
|
||||
// Prepare the style for the titles
|
||||
var titlesStyle = wb.Style;
|
||||
titlesStyle.Font.Bold = true;
|
||||
titlesStyle.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
|
||||
titlesStyle.Fill.BackgroundColor = XLColor.Cyan;
|
||||
|
||||
// Format all titles in one shot
|
||||
wb.NamedRanges.NamedRange("Titles").Ranges.Style = titlesStyle;
|
||||
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
private class Person
|
||||
{
|
||||
public String House { get; set; }
|
||||
public String Name { get; set; }
|
||||
public Int32 Age { get; set; }
|
||||
public static String ClassType { get { return nameof(Person); } }
|
||||
}
|
||||
|
||||
// Private
|
||||
private DataTable GetTable()
|
||||
{
|
||||
DataTable table = new DataTable();
|
||||
table.Columns.Add("Dosage", typeof(int));
|
||||
table.Columns.Add("Drug", typeof(string));
|
||||
table.Columns.Add("Patient", typeof(string));
|
||||
table.Columns.Add("Date", typeof(DateTime));
|
||||
|
||||
table.Rows.Add(25, "Indocin", "David", new DateTime(2000, 1, 1));
|
||||
table.Rows.Add(50, "Enebrel", "Sam", new DateTime(2000, 1, 2));
|
||||
table.Rows.Add(10, "Hydralazine", "Christoff", new DateTime(2000, 1, 3));
|
||||
table.Rows.Add(21, "Combivent", "Janet", new DateTime(2000, 1, 4));
|
||||
table.Rows.Add(100, "Dilantin", "Melanie", new DateTime(2000, 1, 5));
|
||||
return table;
|
||||
}
|
||||
|
||||
// Override
|
||||
|
||||
#endregion Methods
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using ClosedXML.Excel;
|
||||
using MoreLinq;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class LambdaExpressions : 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);
|
||||
|
||||
// Define a range with the data
|
||||
var firstDataCell = ws.Cell("B4");
|
||||
var lastDataCell = ws.LastCellUsed();
|
||||
var rngData = ws.Range(firstDataCell.Address, lastDataCell.Address);
|
||||
|
||||
// Delete all rows where Outcast = false (the 3rd column)
|
||||
rngData.Rows() // From all rows
|
||||
.Where(r => !r.Cell(3).GetBoolean()) // where the 3rd cell of each row is false
|
||||
.ForEach(r => r.Delete()); // delete the row and shift the cells up (the default for rows in a range)
|
||||
|
||||
//// Put a light gray background to all text cells
|
||||
//rngData.Cells() // From all cells
|
||||
// .Where(c => c.DataType == XLCellValues.Text) // where the data type is Text
|
||||
// .ForEach(c => c.Style.Fill.BackgroundColor = XLColor.LightGray); // Fill with a light gray
|
||||
|
||||
var cells = rngData.Cells();
|
||||
var filtered = cells.Where(c => c.DataType == XLDataType.Text);
|
||||
var list = filtered.ToList();
|
||||
foreach (var c in list)
|
||||
{
|
||||
c.Style.Fill.BackgroundColor = XLColor.LightGray;
|
||||
}
|
||||
|
||||
// Put a thick border to the bottom of the table (we may have deleted the bottom cells with the border)
|
||||
rngData.LastRow().Style.Border.BottomBorder = XLBorderStyleValues.Thick;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(tempFile))
|
||||
{
|
||||
File.Delete(tempFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class MergeCells : IXLExample
|
||||
{
|
||||
#region Variables
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
// Public
|
||||
|
||||
|
||||
// Private
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Merge Cells");
|
||||
|
||||
// Merge a row
|
||||
ws.Cell("B2").Value = "Merged Row(1) of Range (B2:D3)";
|
||||
ws.Range("B2:D3").Row(1).Merge();
|
||||
|
||||
// Merge a column
|
||||
ws.Cell("F2").Value = "Merged Column(1) of Range (F2:G8)";
|
||||
ws.Cell("F2").Style.Alignment.WrapText = true;
|
||||
ws.Range("F2:G8").Column(1).Merge();
|
||||
|
||||
// Merge a range
|
||||
ws.Cell("B4").Value = "Merged Range (B4:D6)";
|
||||
ws.Cell("B4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
|
||||
ws.Cell("B4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
|
||||
ws.Range("B4:D6").Merge();
|
||||
|
||||
// Unmerging a range...
|
||||
ws.Cell("B8").Value = "Unmerged";
|
||||
ws.Range("B8:D8").Merge();
|
||||
ws.Range("B8:D8").Unmerge();
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using System.IO;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class MergeMoves : IXLExample
|
||||
{
|
||||
|
||||
public void Create(string filePath)
|
||||
{
|
||||
string tempFile = ExampleHelper.GetTempFilePath(filePath);
|
||||
try
|
||||
{
|
||||
new MergeCells().Create(tempFile);
|
||||
var workbook = new XLWorkbook(tempFile);
|
||||
|
||||
var ws = workbook.Worksheet(1);
|
||||
|
||||
ws.Range("B1:F1").InsertRowsBelow(1);
|
||||
ws.Range("A3:A9").InsertColumnsAfter(1);
|
||||
ws.Row(1).Delete();
|
||||
ws.Column(1).Delete();
|
||||
|
||||
ws.Range("E8:E9").InsertColumnsAfter(1);
|
||||
ws.Range("F2:F8").Merge();
|
||||
ws.Range("E3:E4").InsertColumnsAfter(1);
|
||||
ws.Range("F2:F8").Merge();
|
||||
ws.Range("E1:E2").InsertColumnsAfter(1);
|
||||
ws.Range("G2:G8").Merge();
|
||||
ws.Range("E1:E2").Delete(XLShiftDeletedCells.ShiftCellsLeft);
|
||||
|
||||
ws.Range("D3:E3").InsertRowsBelow(1);
|
||||
ws.Range("A1:B1").InsertRowsBelow(1);
|
||||
ws.Range("B3:D3").Merge();
|
||||
ws.Range("A1:B1").Delete(XLShiftDeletedCells.ShiftCellsUp);
|
||||
|
||||
ws.Range("B8:D8").Merge();
|
||||
ws.Range("D8:D9").Clear();
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(tempFile))
|
||||
{
|
||||
File.Delete(tempFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using System.Linq;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class MultipleSheets : IXLExample
|
||||
{
|
||||
|
||||
public void Create(string filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
foreach (var wsNum in Enumerable.Range(1, 5))
|
||||
{
|
||||
wb.Worksheets.Add("Original Pos. is " + wsNum.ToString());
|
||||
}
|
||||
|
||||
// Move first worksheet to the last position
|
||||
wb.Worksheet(1).Position = wb.Worksheets.Count() + 1;
|
||||
|
||||
// Delete worksheet on position 4 (in this case it's where original position = 5)
|
||||
wb.Worksheet(4).Delete();
|
||||
|
||||
// Swap sheets in positions 1 and 2
|
||||
wb.Worksheet(2).Position = 1;
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class Outline : 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();
|
||||
var ws = wb.Worksheets.Add("Outline");
|
||||
|
||||
ws.Outline.SummaryHLocation = XLOutlineSummaryHLocation.Right;
|
||||
ws.Columns(2, 6).Group(); // Create an outline (level 1) for columns 2-6
|
||||
ws.Columns(2, 4).Group(); // Create an outline (level 2) for columns 2-4
|
||||
ws.Column(2).Ungroup(true); // Remove column 2 from all outlines
|
||||
|
||||
ws.Outline.SummaryVLocation = XLOutlineSummaryVLocation.Bottom;
|
||||
ws.Rows(1, 5).Group(); // Create an outline (level 1) for rows 1-5
|
||||
ws.Rows(1, 4).Group(); // Create an outline (level 2) for rows 1-4
|
||||
ws.Rows(1, 4).Collapse(); // Collapse rows 1-4
|
||||
ws.Rows(1, 2).Group(); // Create an outline (level 3) for rows 1-2
|
||||
ws.Rows(1, 2).Ungroup(); // Ungroup rows 1-2 from their last outline
|
||||
|
||||
// You can also Collapse/Expand specific outline levels
|
||||
//
|
||||
// ws.CollapseRows(Int32 outlineLevel)
|
||||
// ws.CollapseColumns(Int32 outlineLevel)
|
||||
//
|
||||
// ws.ExpandRows(Int32 outlineLevel)
|
||||
// ws.ExpandColumns(Int32 outlineLevel)
|
||||
|
||||
// And you can also Collapse/Expand ALL outline levels in one shot
|
||||
//
|
||||
// ws.CollapseRows()
|
||||
// ws.CollapseColumns()
|
||||
//
|
||||
// ws.ExpandRows()
|
||||
// ws.ExpandColumns()
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class RightToLeft : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
|
||||
var ws = wb.Worksheets.Add("RightToLeftSheet");
|
||||
ws.Cell("A1").Value = "A1";
|
||||
ws.Cell("B1").Value = "B1";
|
||||
ws.Cell("C1").Value = "C1";
|
||||
ws.RightToLeft = true;
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
using static ClosedXML.Excel.XLProtectionAlgorithm;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class SheetProtection : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
var ws = wb.Worksheets.Add("Protected No-Password");
|
||||
|
||||
ws.Protect().AllowElement
|
||||
(
|
||||
// On this sheet we will only allow:
|
||||
XLSheetProtectionElements.FormatCells
|
||||
| XLSheetProtectionElements.InsertColumns
|
||||
| XLSheetProtectionElements.DeleteColumns
|
||||
| XLSheetProtectionElements.DeleteRows
|
||||
| XLSheetProtectionElements.EditScenarios
|
||||
);
|
||||
|
||||
ws.Cell("A1").SetValue("Locked, No Hidden (Default):").Style.Font.SetBold().Fill.SetBackgroundColor(XLColor.Cyan);
|
||||
ws.Cell("B1").Style
|
||||
.Border.SetOutsideBorder(XLBorderStyleValues.Medium);
|
||||
|
||||
ws.Cell("A2").SetValue("Locked, Hidden:").Style.Font.SetBold().Fill.SetBackgroundColor(XLColor.Cyan);
|
||||
ws.Cell("B2").Style
|
||||
.Protection.SetHidden()
|
||||
.Border.SetOutsideBorder(XLBorderStyleValues.Medium);
|
||||
|
||||
ws.Cell("A3").SetValue("Not Locked, Hidden:").Style.Font.SetBold().Fill.SetBackgroundColor(XLColor.Cyan);
|
||||
ws.Cell("B3").Style
|
||||
.Protection.SetLocked(false)
|
||||
.Protection.SetHidden()
|
||||
.Border.SetOutsideBorder(XLBorderStyleValues.Medium);
|
||||
|
||||
ws.Cell("A4").SetValue("Not Locked, Not Hidden:").Style.Font.SetBold().Fill.SetBackgroundColor(XLColor.Cyan);
|
||||
ws.Cell("B4").Style
|
||||
.Protection.SetLocked(false)
|
||||
.Border.SetOutsideBorder(XLBorderStyleValues.Medium);
|
||||
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
// Protect a sheet with a password
|
||||
var protectedSheet = wb.Worksheets.Add("Protected Password = 123");
|
||||
var protection = protectedSheet.Protect("123", Algorithm.SimpleHash);
|
||||
protection.AllowElement
|
||||
(
|
||||
XLSheetProtectionElements.InsertRows
|
||||
| XLSheetProtectionElements.InsertColumns
|
||||
);
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class SheetViews : IXLExample
|
||||
{
|
||||
public void Create(string filePath)
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
IXLWorksheet ws;
|
||||
|
||||
ws = wb.AddWorksheet("ZoomScale");
|
||||
ws.FirstCell().SetValue(ws.Name);
|
||||
ws.SheetView.ZoomScale = 50;
|
||||
|
||||
ws = wb.AddWorksheet("ZoomScaleNormal");
|
||||
ws.FirstCell().SetValue(ws.Name);
|
||||
ws.SheetView.ZoomScaleNormal = 70;
|
||||
|
||||
ws = wb.AddWorksheet("ZoomScalePageLayoutView");
|
||||
ws.FirstCell().SetValue(ws.Name);
|
||||
ws.SheetView.ZoomScalePageLayoutView = 85;
|
||||
|
||||
ws = wb.AddWorksheet("ZoomScaleSheetLayoutView");
|
||||
ws.FirstCell().SetValue(ws.Name);
|
||||
ws.SheetView.ZoomScaleSheetLayoutView = 120;
|
||||
|
||||
ws = wb.AddWorksheet("ZoomScaleTooSmall");
|
||||
ws.FirstCell().SetValue(ws.Name);
|
||||
ws.SheetView.ZoomScale = 5;
|
||||
|
||||
ws = wb.AddWorksheet("ZoomScaleTooBig");
|
||||
ws.FirstCell().SetValue(ws.Name);
|
||||
ws.SheetView.ZoomScale = 500;
|
||||
|
||||
ws = wb.AddWorksheet("TopLeftCell");
|
||||
ws.SheetView.TopLeftCellAddress = ws.Cell("AZ2000").Address;
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class ShiftingFormulas : 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();
|
||||
var ws = wb.Worksheets.Add("Shifting Formulas");
|
||||
ws.Cell("B2").Value = 5;
|
||||
ws.Cell("B3").Value = 6;
|
||||
ws.Cell("C2").Value = 1;
|
||||
ws.Cell("C3").Value = 2;
|
||||
ws.Cell("A4").Value = "Sum:";
|
||||
ws.Range("B4:C4").FormulaR1C1 = "Sum(R[-2]C:R[-1]C)";
|
||||
ws.Range("B4:C4").AddToNamed("WorkbookB4C4");
|
||||
ws.Range("B4:C4").AddToNamed("WorksheetB4C4", XLScope.Worksheet);
|
||||
ws.Cell("E2").Value = "Avg:";
|
||||
|
||||
ws.Cell("F2").FormulaA1 = "Average(B2:C3)";
|
||||
ws.Ranges("A4,E2").Style
|
||||
.Font.SetBold()
|
||||
.Fill.SetBackgroundColor(XLColor.CyanProcess);
|
||||
|
||||
var ws2 = wb.Worksheets.Add("WS2");
|
||||
ws2.Cell(1, 1).FormulaA1 = "='Shifting Formulas'!B2";
|
||||
ws2.Cell(1, 2).Value = ws2.Cell(1, 1).Value;
|
||||
ws2.Cell(2, 1).FormulaA1 = "Average('Shifting Formulas'!$B$2:$C$3)";
|
||||
ws2.Cell(3, 1).FormulaA1 = "Average('Shifting Formulas'!$B$2:$C3)";
|
||||
ws2.Cell(4, 1).FormulaA1 = "Average('Shifting Formulas'!$B$2:C3)";
|
||||
ws2.Cell(5, 1).FormulaA1 = "Average('Shifting Formulas'!$B2:C3)";
|
||||
ws2.Cell(6, 1).FormulaA1 = "Average('Shifting Formulas'!B2:C3)";
|
||||
ws2.Cell(7, 1).FormulaA1 = "Average('Shifting Formulas'!B2:C$3)";
|
||||
ws2.Cell(8, 1).FormulaA1 = "Average('Shifting Formulas'!B2:$C$3)";
|
||||
ws2.Cell(9, 1).FormulaA1 = "Average('Shifting Formulas'!B$2:$C$3)";
|
||||
|
||||
var dataGrid = ws.Range("B2:D3");
|
||||
ws.Row(1).InsertRowsAbove(1);
|
||||
var newRow = dataGrid.LastRow().InsertRowsAbove(1).First();
|
||||
newRow.Value = 1;
|
||||
dataGrid.LastColumn().FormulaR1C1 = String.Format("SUM(RC[-{0}]:RC[-1])", dataGrid.ColumnCount() - 1);
|
||||
ws.Cell(1, 1).InsertCellsBelow(1);
|
||||
ws.Column(1).InsertColumnsBefore(1);
|
||||
ws.Row(4).Delete();
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class ShowCase : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
// Creating a new workbook
|
||||
var wb = new XLWorkbook();
|
||||
|
||||
//Adding a worksheet
|
||||
var ws = wb.Worksheets.Add("Contacts");
|
||||
|
||||
//Adding text
|
||||
//Title
|
||||
ws.Cell("B2").Value = "Contacts";
|
||||
//First Names
|
||||
ws.Cell("B3").Value = "FName";
|
||||
ws.Cell("B4").Value = "John";
|
||||
ws.Cell("B5").Value = "Hank";
|
||||
ws.Cell("B6").SetValue("Dagny"); // Another way to set the value
|
||||
//Last Names
|
||||
ws.Cell("C3").Value = "LName";
|
||||
ws.Cell("C4").Value = "Galt";
|
||||
ws.Cell("C5").Value = "Rearden";
|
||||
ws.Cell("C6").SetValue("Taggart"); // Another way to set the value
|
||||
|
||||
//Adding more data types
|
||||
//Is an outcast?
|
||||
ws.Cell("D3").Value = "Outcast";
|
||||
ws.Cell("D4").Value = true;
|
||||
ws.Cell("D5").Value = false;
|
||||
ws.Cell("D6").SetValue(false); // Another way to set the value
|
||||
//Date of Birth
|
||||
ws.Cell("E3").Value = "DOB";
|
||||
ws.Cell("E4").Value = new DateTime(1919, 1, 21);
|
||||
ws.Cell("E5").Value = new DateTime(1907, 3, 4);
|
||||
ws.Cell("E6").SetValue(new DateTime(1921, 12, 15)); // Another way to set the value
|
||||
//Income
|
||||
ws.Cell("F3").Value = "Income";
|
||||
ws.Cell("F4").Value = 2000;
|
||||
ws.Cell("F5").Value = 40000;
|
||||
ws.Cell("F6").SetValue(10000); // Another way to set the value
|
||||
|
||||
//Defining ranges
|
||||
//From worksheet
|
||||
var rngTable = ws.Range("B2:F6");
|
||||
//From another range
|
||||
var rngDates = rngTable.Range("E4:E6");
|
||||
var rngNumbers = rngTable.Range("F4:F6");
|
||||
|
||||
//Formatting dates and numbers
|
||||
//Using a OpenXML's predefined formats
|
||||
rngDates.Style.NumberFormat.NumberFormatId = 15;
|
||||
//Using a custom format
|
||||
rngNumbers.Style.NumberFormat.Format = "$ #,##0";
|
||||
|
||||
//Format title cell in one shot
|
||||
rngTable.Cell(1, 1).Style
|
||||
.Font.SetBold()
|
||||
.Fill.SetBackgroundColor(XLColor.CornflowerBlue)
|
||||
.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center);
|
||||
|
||||
//Merge title cells
|
||||
rngTable.FirstRow().Merge(); // We could've also used: rngTable.Range("A1:E1").Merge() or rngTable.Row(1).Merge()
|
||||
|
||||
//Formatting headers
|
||||
var rngHeaders = rngTable.Range("B3:F3");
|
||||
rngHeaders.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
|
||||
rngHeaders.Style.Font.Bold = true;
|
||||
rngHeaders.Style.Font.FontColor = XLColor.DarkBlue;
|
||||
rngHeaders.Style.Fill.BackgroundColor = XLColor.Aqua;
|
||||
|
||||
// Create an Excel table with the data portion
|
||||
var rngData = ws.Range("B3:F6");
|
||||
var excelTable = rngData.CreateTable();
|
||||
|
||||
// Add the totals row
|
||||
excelTable.ShowTotalsRow = true;
|
||||
// Put the average on the field "Income"
|
||||
// Notice how we're calling the cell by the column name
|
||||
excelTable.Field("Income").TotalsRowFunction = XLTotalsRowFunction.Average;
|
||||
// Put a label on the totals cell of the field "DOB"
|
||||
excelTable.Field("DOB").TotalsRowLabel = "Average:";
|
||||
|
||||
//Add thick borders to the contents of our spreadsheet
|
||||
ws.RangeUsed().Style.Border.OutsideBorder = XLBorderStyleValues.Thick;
|
||||
|
||||
// You can also specify the border for each side with:
|
||||
// contents.FirstColumn().Style.Border.LeftBorder = XLBorderStyleValues.Thick;
|
||||
// contents.LastColumn().Style.Border.RightBorder = XLBorderStyleValues.Thick;
|
||||
// contents.FirstRow().Style.Border.TopBorder = XLBorderStyleValues.Thick;
|
||||
// contents.LastRow().Style.Border.BottomBorder = XLBorderStyleValues.Thick;
|
||||
|
||||
// Adjust column widths to their content
|
||||
ws.Columns(2, 6).AdjustToContents();
|
||||
|
||||
//Saving the workbook
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class TabColors : 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();
|
||||
|
||||
var wsRed = wb.Worksheets.Add("Red").SetTabColor(XLColor.Red);
|
||||
|
||||
var wsAccent3 = wb.Worksheets.Add("Accent3").SetTabColor(XLColor.FromTheme(XLThemeColor.Accent3));
|
||||
|
||||
var wsIndexed = wb.Worksheets.Add("Indexed");
|
||||
wsIndexed.TabColor = XLColor.FromIndex(24);
|
||||
|
||||
var wsArgb = wb.Worksheets.Add("Argb");
|
||||
wsArgb.TabColor = XLColor.FromArgb(23, 23, 23);
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class WorkbookProperties : 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();
|
||||
var ws = wb.Worksheets.Add("Workbook Properties");
|
||||
|
||||
wb.Properties.Author = "theAuthor";
|
||||
wb.Properties.Title = "theTitle";
|
||||
wb.Properties.Subject = "theSubject";
|
||||
wb.Properties.Category = "theCategory";
|
||||
wb.Properties.Keywords = "theKeywords";
|
||||
wb.Properties.Comments = "theComments";
|
||||
wb.Properties.Status = "theStatus";
|
||||
wb.Properties.LastModifiedBy = "theLastModifiedBy";
|
||||
wb.Properties.Company = "theCompany";
|
||||
wb.Properties.Manager = "theManager";
|
||||
|
||||
// Creating/Using custom properties
|
||||
wb.CustomProperties.Add("theText", "XXX");
|
||||
wb.CustomProperties.Add("theDate", new DateTime(2011, 1, 1, 17, 0, 0, DateTimeKind.Utc)); // Use UTC to make sure test can be run in any time zone
|
||||
wb.CustomProperties.Add("theNumber", 123.456);
|
||||
wb.CustomProperties.Add("theBoolean", true);
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples.Misc
|
||||
{
|
||||
public class WorkbookProtection : IXLExample
|
||||
{
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
var ws = wb.Worksheets.Add("Workbook Protection");
|
||||
wb.Protect(true, false, "Abc@123");
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using ClosedXML_Examples.Delete;
|
||||
using System.IO;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class ModifyFiles
|
||||
{
|
||||
public static void Run()
|
||||
{
|
||||
var path = Program.BaseModifiedDirectory;
|
||||
new DeleteRows().Create(Path.Combine(path, "DeleteRows.xlsx"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.PageSetup
|
||||
{
|
||||
public class HeaderFooters : IXLExample
|
||||
{
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Headers and Footers");
|
||||
|
||||
// Simple left header to be placed on all pages
|
||||
ws.PageSetup.Header.Left.AddText("Created with ClosedXML");
|
||||
|
||||
// Using various font decorations for the right header on the first page only
|
||||
// Here we show different methods for setting font decorations.
|
||||
|
||||
// Set single font decorations immediately
|
||||
ws.PageSetup.Header.Right.AddText("The ", XLHFOccurrence.FirstPage).SetBold();
|
||||
ws.PageSetup.Header.Right.AddText("First ", XLHFOccurrence.FirstPage).SetFontColor(XLColor.Red);
|
||||
|
||||
// Use the IXLRichText returned by the AddText(...) method to later on modify the font
|
||||
var richText = ws.PageSetup.Header.Right.AddText("Colorful ", XLHFOccurrence.FirstPage);
|
||||
richText.FontColor = XLColor.Blue;
|
||||
richText.Underline = XLFontUnderlineValues.Double;
|
||||
|
||||
// Set multiple font decorations chained
|
||||
ws.PageSetup.Header.Right.AddText("Page", XLHFOccurrence.FirstPage)
|
||||
.SetBold()
|
||||
.SetItalic()
|
||||
.SetFontName("Broadway");
|
||||
|
||||
|
||||
// Using predefined header/footer text:
|
||||
|
||||
// Let's put the full path to the file on the right footer of every odd page:
|
||||
ws.PageSetup.Footer.Right.AddText(XLHFPredefinedText.FullPath, XLHFOccurrence.OddPages);
|
||||
|
||||
// Let's put the current page number and total pages on the center of every footer:
|
||||
ws.PageSetup.Footer.Center.AddText(XLHFPredefinedText.PageNumber, XLHFOccurrence.AllPages);
|
||||
ws.PageSetup.Footer.Center.AddText(" / ", XLHFOccurrence.AllPages);
|
||||
ws.PageSetup.Footer.Center.AddText(XLHFPredefinedText.NumberOfPages, XLHFOccurrence.AllPages);
|
||||
|
||||
// Don't align headers and footers with the margins
|
||||
ws.PageSetup.AlignHFWithMargins = false;
|
||||
|
||||
// Don't scale headers and footers with the document
|
||||
ws.PageSetup.ScaleHFWithDocument = false;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.PageSetup
|
||||
{
|
||||
public class Margins : IXLExample
|
||||
{
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Margins");
|
||||
ws.PageSetup.Margins.Top = 1;
|
||||
ws.PageSetup.Margins.Bottom = 1.25;
|
||||
ws.PageSetup.Margins.Left = 0.5;
|
||||
ws.PageSetup.Margins.Right = 0.75;
|
||||
ws.PageSetup.Margins.Footer = 0.15;
|
||||
ws.PageSetup.Margins.Header = 0.30;
|
||||
|
||||
ws.PageSetup.CenterHorizontally = true;
|
||||
ws.PageSetup.CenterVertically = true;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.PageSetup
|
||||
{
|
||||
public class Page : IXLExample
|
||||
{
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws1 = workbook.Worksheets.Add("Page Setup - Page1");
|
||||
ws1.PageSetup.PageOrientation = XLPageOrientation.Landscape;
|
||||
ws1.PageSetup.AdjustTo(80);
|
||||
ws1.PageSetup.PaperSize = XLPaperSize.LegalPaper;
|
||||
ws1.PageSetup.VerticalDpi = 600;
|
||||
ws1.PageSetup.HorizontalDpi = 600;
|
||||
|
||||
var ws2 = workbook.Worksheets.Add("Page Setup - Page2");
|
||||
ws2.PageSetup.PageOrientation = XLPageOrientation.Portrait;
|
||||
ws2.PageSetup.FitToPages(2, 2); // Alternatively you can use
|
||||
// ws2.PageSetup.PagesTall = #
|
||||
// and/or ws2.PageSetup.PagesWide = #
|
||||
|
||||
ws2.PageSetup.PaperSize = XLPaperSize.LetterPaper;
|
||||
ws2.PageSetup.VerticalDpi = 600;
|
||||
ws2.PageSetup.HorizontalDpi = 600;
|
||||
ws2.PageSetup.FirstPageNumber = 5;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.PageSetup
|
||||
{
|
||||
public class SheetTab : IXLExample
|
||||
{
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Sheet Tab");
|
||||
|
||||
// Adding print areas
|
||||
ws.PageSetup.PrintAreas.Add("A1:B2");
|
||||
ws.PageSetup.PrintAreas.Add("D3:D5");
|
||||
|
||||
// Adding rows to repeat at top
|
||||
ws.PageSetup.SetRowsToRepeatAtTop(1,2);
|
||||
|
||||
// Adding columns to repeat at left
|
||||
ws.PageSetup.SetColumnsToRepeatAtLeft(1, 2);
|
||||
|
||||
// Show gridlines
|
||||
ws.PageSetup.ShowGridlines = true;
|
||||
|
||||
// Print in black and white
|
||||
ws.PageSetup.BlackAndWhite = true;
|
||||
|
||||
// Print in draft quality
|
||||
ws.PageSetup.DraftQuality = true;
|
||||
|
||||
// Show row and column headings
|
||||
ws.PageSetup.ShowRowAndColumnHeadings = true;
|
||||
|
||||
// Set the page print order to over, then down
|
||||
ws.PageSetup.PageOrder = XLPageOrderValues.OverThenDown;
|
||||
|
||||
// Place comments at the end of the sheet
|
||||
ws.PageSetup.ShowComments = XLShowCommentsValues.AtEnd;
|
||||
|
||||
// Print errors as #N/A
|
||||
ws.PageSetup.PrintErrorValue = XLPrintErrorValues.NA;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.PageSetup
|
||||
{
|
||||
public class Sheets : IXLExample
|
||||
{
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws1 = workbook.Worksheets.Add("Separate PrintAreas");
|
||||
ws1.PageSetup.PrintAreas.Add("A1:B2");
|
||||
ws1.PageSetup.PrintAreas.Add("D3:D5");
|
||||
|
||||
var ws2 = workbook.Worksheets.Add("Page Breaks");
|
||||
ws2.PageSetup.PrintAreas.Add("A1:D5");
|
||||
ws2.PageSetup.AddHorizontalPageBreak(2);
|
||||
ws2.PageSetup.AddVerticalPageBreak(2);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.PageSetup
|
||||
{
|
||||
public class TwoPages : IXLExample
|
||||
{
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
var ws = wb.Worksheets.Add("Sheet1");
|
||||
foreach (var ro in Enumerable.Range(1, 100))
|
||||
{
|
||||
foreach (var co in Enumerable.Range(1, 10))
|
||||
{
|
||||
ws.Cell(ro, co).Value = ws.Cell(ro, co).Address.ToString();
|
||||
}
|
||||
}
|
||||
ws.PageSetup.PagesWide = 1;
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class PivotTables : IXLExample
|
||||
{
|
||||
private class Pastry
|
||||
{
|
||||
public Pastry(string name, int? code, int numberOfOrders, double quality, string month, DateTime? bakeDate)
|
||||
{
|
||||
Name = name;
|
||||
Code = code;
|
||||
NumberOfOrders = numberOfOrders;
|
||||
Quality = quality;
|
||||
Month = month;
|
||||
BakeDate = bakeDate;
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
public int? Code { get; }
|
||||
public int NumberOfOrders { get; set; }
|
||||
public double Quality { get; set; }
|
||||
public string Month { get; set; }
|
||||
public DateTime? BakeDate { get; set; }
|
||||
}
|
||||
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var pastries = new List<Pastry>
|
||||
{
|
||||
new Pastry("Croissant", 101, 150, 60.2, "Apr", new DateTime(2016, 04, 21)),
|
||||
new Pastry("Croissant", 101, 250, 50.42, "May", new DateTime(2016, 05, 03)),
|
||||
new Pastry("Croissant", 101, 134, 22.12, "Jun", new DateTime(2016, 06, 24)),
|
||||
new Pastry("Doughnut", 102, 250, 89.99, "Apr", new DateTime(2017, 04, 23)),
|
||||
new Pastry("Doughnut", 102, 225, 70, "May", new DateTime(2016, 05, 24)),
|
||||
new Pastry("Doughnut", 102, 210, 75.33, "Jun", new DateTime(2016, 06, 02)),
|
||||
new Pastry("Bearclaw", 103, 134, 10.24, "Apr", new DateTime(2016, 04, 27)),
|
||||
new Pastry("Bearclaw", 103, 184, 33.33, "May", new DateTime(2016, 05, 20)),
|
||||
new Pastry("Bearclaw", 103, 124, 25, "Jun", new DateTime(2017, 06, 05)),
|
||||
new Pastry("Danish", 104, 394, -20.24, "Apr", new DateTime(2017, 04, 24)),
|
||||
new Pastry("Danish", 104, 190, 60, "May", new DateTime(2017, 05, 08)),
|
||||
new Pastry("Danish", 104, 221, 24.76, "Jun", new DateTime(2016, 06, 21)),
|
||||
|
||||
// Deliberately add different casings of same string to ensure pivot table doesn't duplicate it.
|
||||
new Pastry("Scone", 105, 135, 0, "Apr", new DateTime(2017, 04, 22)),
|
||||
new Pastry("SconE", 105, 122, 5.19, "May", new DateTime(2017, 05, 03)),
|
||||
new Pastry("SCONE", 105, 243, 44.2, "Jun", new DateTime(2017, 06, 14)),
|
||||
|
||||
// For ContainsBlank and integer rows/columns test
|
||||
new Pastry("Scone", null, 255, 18.4, null, null),
|
||||
};
|
||||
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
var ws = wb.Worksheets.Add("PastrySalesData");
|
||||
// Insert our list of pastry data into the "PastrySalesData" sheet at cell 1,1
|
||||
var table = ws.Cell(1, 1).InsertTable(pastries, "PastrySalesData", true);
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
|
||||
IXLWorksheet ptSheet;
|
||||
IXLPivotTable pt;
|
||||
|
||||
#region Pivots
|
||||
|
||||
for (int i = 1; i <= 3; i++)
|
||||
{
|
||||
// Add a new sheet for our pivot table
|
||||
ptSheet = wb.Worksheets.Add("pvt" + i);
|
||||
|
||||
// Create the pivot table, using the data from the "PastrySalesData" table
|
||||
pt = ptSheet.PivotTables.Add("pvt", ptSheet.Cell(1, 1), table.AsRange());
|
||||
|
||||
// The rows in our pivot table will be the names of the pastries
|
||||
if (i == 2) pt.RowLabels.Add(XLConstants.PivotTable.ValuesSentinalLabel);
|
||||
pt.RowLabels.Add("Name");
|
||||
|
||||
// The columns will be the months
|
||||
pt.ColumnLabels.Add("Month");
|
||||
if (i == 3) pt.ColumnLabels.Add(XLConstants.PivotTable.ValuesSentinalLabel);
|
||||
|
||||
// The values in our table will come from the "NumberOfOrders" field
|
||||
// The default calculation setting is a total of each row/column
|
||||
pt.Values.Add("NumberOfOrders", "NumberOfOrdersPercentageOfBearclaw")
|
||||
.ShowAsPercentageFrom("Name").And("Bearclaw")
|
||||
.NumberFormat.Format = "0%";
|
||||
|
||||
if (i > 1)
|
||||
{
|
||||
pt.Values.Add("Quality", "Sum of Quality")
|
||||
.NumberFormat.SetFormat("#,##0.00");
|
||||
}
|
||||
if (i > 2)
|
||||
{
|
||||
pt.Values.Add("NumberOfOrders", "Sum of NumberOfOrders");
|
||||
}
|
||||
|
||||
ptSheet.Columns().AdjustToContents();
|
||||
}
|
||||
|
||||
#endregion Pivots
|
||||
|
||||
#region Different kind of pivot
|
||||
|
||||
ptSheet = wb.Worksheets.Add("pvtNoColumnLabels");
|
||||
pt = ptSheet.PivotTables.Add("pvtNoColumnLabels", ptSheet.Cell(1, 1), table.AsRange());
|
||||
|
||||
pt.RowLabels.Add("Name");
|
||||
pt.RowLabels.Add("Month");
|
||||
|
||||
pt.Values.Add("NumberOfOrders").SetSummaryFormula(XLPivotSummary.Sum);
|
||||
pt.Values.Add("Quality").SetSummaryFormula(XLPivotSummary.Sum);
|
||||
|
||||
pt.SetRowHeaderCaption("Pastry name");
|
||||
|
||||
#endregion Different kind of pivot
|
||||
|
||||
#region Pivot table with collapsed fields
|
||||
|
||||
ptSheet = wb.Worksheets.Add("pvtCollapsedFields");
|
||||
pt = ptSheet.PivotTables.Add("pvtCollapsedFields", ptSheet.Cell(1, 1), table.AsRange());
|
||||
|
||||
pt.RowLabels.Add("Name").SetCollapsed();
|
||||
pt.RowLabels.Add("Month").SetCollapsed();
|
||||
|
||||
pt.Values.Add("NumberOfOrders").SetSummaryFormula(XLPivotSummary.Sum);
|
||||
pt.Values.Add("Quality").SetSummaryFormula(XLPivotSummary.Sum);
|
||||
|
||||
#endregion Pivot table with collapsed fields
|
||||
|
||||
#region Pivot table with a field both as a value and as a row/column/filter label
|
||||
|
||||
ptSheet = wb.Worksheets.Add("pvtFieldAsValueAndLabel");
|
||||
pt = ptSheet.PivotTables.Add("pvtFieldAsValueAndLabel", ptSheet.Cell(1, 1), table.AsRange());
|
||||
|
||||
pt.RowLabels.Add("Name");
|
||||
pt.RowLabels.Add("Month");
|
||||
|
||||
pt.Values.Add("Name").SetSummaryFormula(XLPivotSummary.Count);//.NumberFormat.Format = "#0.00";
|
||||
|
||||
#endregion Pivot table with a field both as a value and as a row/column/filter label
|
||||
|
||||
#region Pivot table with subtotals disabled
|
||||
|
||||
ptSheet = wb.Worksheets.Add("pvtHideSubTotals");
|
||||
|
||||
// Create the pivot table, using the data from the "PastrySalesData" table
|
||||
pt = ptSheet.PivotTables.Add("pvtHidesubTotals", ptSheet.Cell(1, 1), table.AsRange());
|
||||
|
||||
// The rows in our pivot table will be the names of the pastries
|
||||
pt.RowLabels.Add(XLConstants.PivotTable.ValuesSentinalLabel);
|
||||
|
||||
// The columns will be the months
|
||||
pt.ColumnLabels.Add("Month");
|
||||
pt.ColumnLabels.Add("Name");
|
||||
|
||||
// The values in our table will come from the "NumberOfOrders" field
|
||||
// The default calculation setting is a total of each row/column
|
||||
pt.Values.Add("NumberOfOrders", "NumberOfOrdersPercentageOfBearclaw")
|
||||
.ShowAsPercentageFrom("Name").And("Bearclaw")
|
||||
.NumberFormat.Format = "0%";
|
||||
|
||||
pt.Values.Add("Quality", "Sum of Quality")
|
||||
.NumberFormat.SetFormat("#,##0.00");
|
||||
|
||||
pt.Subtotals = XLPivotSubtotals.DoNotShow;
|
||||
|
||||
pt.SetColumnHeaderCaption("Measures");
|
||||
|
||||
ptSheet.Columns().AdjustToContents();
|
||||
|
||||
#endregion Pivot table with subtotals disabled
|
||||
|
||||
#region Pivot Table with filter
|
||||
|
||||
ptSheet = wb.Worksheets.Add("pvtFilter");
|
||||
|
||||
pt = table.CreatePivotTable(ptSheet.FirstCell(), "pvtFilter");
|
||||
|
||||
pt.RowLabels.Add("Month");
|
||||
|
||||
pt.Values.Add("NumberOfOrders").SetSummaryFormula(XLPivotSummary.Sum);
|
||||
|
||||
pt.ReportFilters.Add("Name")
|
||||
.AddSelectedValue("Scone")
|
||||
.AddSelectedValue("Doughnut");
|
||||
|
||||
pt.ReportFilters.Add("Quality")
|
||||
.AddSelectedValue(5.19);
|
||||
|
||||
pt.ReportFilters.Add("BakeDate")
|
||||
.AddSelectedValue(new DateTime(2017, 05, 03));
|
||||
|
||||
#endregion Pivot Table with filter
|
||||
|
||||
#region Pivot table sorting
|
||||
|
||||
ptSheet = wb.Worksheets.Add("pvtSort");
|
||||
pt = ptSheet.PivotTables.Add("pvtSort", ptSheet.Cell(1, 1), table.AsRange());
|
||||
|
||||
pt.RowLabels.Add("Name").SetSort(XLPivotSortType.Ascending);
|
||||
pt.RowLabels.Add("Month").SetSort(XLPivotSortType.Descending);
|
||||
|
||||
pt.Values.Add("NumberOfOrders").SetSummaryFormula(XLPivotSummary.Sum);
|
||||
pt.Values.Add("Quality").SetSummaryFormula(XLPivotSummary.Sum);
|
||||
|
||||
pt.SetRowHeaderCaption("Pastry name");
|
||||
|
||||
#endregion Different kind of pivot
|
||||
|
||||
#region Pivot Table with integer rows
|
||||
|
||||
ptSheet = wb.Worksheets.Add("pvtInteger");
|
||||
|
||||
pt = ptSheet.PivotTables.Add("pvtInteger", ptSheet.Cell(1, 1), table);
|
||||
|
||||
pt.RowLabels.Add("Name");
|
||||
pt.RowLabels.Add("Code");
|
||||
pt.RowLabels.Add("BakeDate");
|
||||
|
||||
pt.ColumnLabels.Add("Month");
|
||||
|
||||
pt.Values.Add("NumberOfOrders").SetSummaryFormula(XLPivotSummary.Sum);
|
||||
pt.Values.Add("Quality").SetSummaryFormula(XLPivotSummary.Sum);
|
||||
|
||||
#endregion Pivot Table with filter
|
||||
|
||||
wb.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static string BaseCreatedDirectory
|
||||
{
|
||||
get
|
||||
{
|
||||
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Created");
|
||||
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
public static string BaseModifiedDirectory
|
||||
{
|
||||
get
|
||||
{
|
||||
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modified");
|
||||
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
CreateFiles.CreateAllFiles();
|
||||
LoadFiles.LoadAllFiles();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 7.4 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.7 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 34 KiB |
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Rows
|
||||
{
|
||||
public class InsertRows : 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 workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Inserting Rows");
|
||||
|
||||
// Color the entire spreadsheet using rows
|
||||
ws.Rows().Style.Fill.BackgroundColor = XLColor.LightCyan;
|
||||
|
||||
// Put a value in a few cells
|
||||
foreach (var r in Enumerable.Range(1, 5))
|
||||
foreach (var c in Enumerable.Range(1, 5))
|
||||
ws.Cell(r, c).Value = "X";
|
||||
|
||||
var blueRow = ws.Row(2);
|
||||
var redRow = ws.Row(5);
|
||||
|
||||
blueRow.Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
blueRow.InsertRowsBelow(2);
|
||||
|
||||
|
||||
redRow.Style.Fill.BackgroundColor = XLColor.Red;
|
||||
redRow.InsertRowsAbove(2);
|
||||
|
||||
ws.Columns(3, 4).Style.Fill.BackgroundColor = XLColor.Orange;
|
||||
ws.Range("A2:A4").InsertRowsBelow(2);
|
||||
ws.Range("B2:B4").InsertRowsAbove(2);
|
||||
ws.Range("C2:C4").InsertRowsBelow(2);
|
||||
ws.Range("D2:D4").InsertRowsAbove(2);
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class RowCells : 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 workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Row Cells");
|
||||
|
||||
var rowFromWorksheet = ws.Row(1);
|
||||
rowFromWorksheet.Cell(1).Style.Fill.BackgroundColor = XLColor.Red;
|
||||
rowFromWorksheet.Cells("2").Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
rowFromWorksheet.Cells("3,5:6").Style.Fill.BackgroundColor = XLColor.Red;
|
||||
rowFromWorksheet.Cells(8, 9).Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
|
||||
var rowFromRange = ws.Range("A2:I2").FirstRow();
|
||||
|
||||
rowFromRange.Cell(1).Style.Fill.BackgroundColor = XLColor.Red;
|
||||
rowFromRange.Cells("2").Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
rowFromRange.Cells("3,5:6").Style.Fill.BackgroundColor = XLColor.Red;
|
||||
rowFromRange.Cells(8, 9).Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
|
||||
ws.Columns().Width = 7;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Rows
|
||||
{
|
||||
public class RowCollection : 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 workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Rows of a Range");
|
||||
|
||||
// All rows in a range
|
||||
ws.Range("A1:B2").Rows().Style.Fill.BackgroundColor = XLColor.DimGray;
|
||||
|
||||
var bigRange = ws.Range("B4:C17");
|
||||
|
||||
// Contiguous rows by number
|
||||
bigRange.Rows(1, 2).Style.Fill.BackgroundColor = XLColor.Red;
|
||||
|
||||
// Contiguous rows by number
|
||||
bigRange.Rows("4:5").Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
|
||||
// Spread rows by number
|
||||
bigRange.Rows("7:8,10:11").Style.Fill.BackgroundColor = XLColor.Orange;
|
||||
|
||||
// Using a single number
|
||||
bigRange.Rows("13").Style.Fill.BackgroundColor = XLColor.Cyan;
|
||||
|
||||
// Adjust the height
|
||||
ws.Rows().Height = 15;
|
||||
|
||||
var ws2 = workbook.Worksheets.Add("Rows of a Worksheet");
|
||||
|
||||
// Contiguous rows by number
|
||||
ws2.Rows(1, 2).Style.Fill.BackgroundColor = XLColor.Red;
|
||||
|
||||
// Contiguous rows by number
|
||||
ws2.Rows("4:5").Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
|
||||
// Spread rows by number
|
||||
ws2.Rows("7:8,10:11").Style.Fill.BackgroundColor = XLColor.Orange;
|
||||
|
||||
// Using a single number
|
||||
ws2.Rows("13").Style.Fill.BackgroundColor = XLColor.Cyan;
|
||||
|
||||
// Adjust the height
|
||||
ws2.Rows("1:13").Height = 15;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Rows
|
||||
{
|
||||
public class RowSettings : IXLExample
|
||||
{
|
||||
#region Variables
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
// Public
|
||||
public RowSettings()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Private
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Row Settings");
|
||||
|
||||
var row1 = ws.Row(2);
|
||||
row1.Style.Fill.BackgroundColor = XLColor.Red;
|
||||
row1.Height = 30;
|
||||
|
||||
var row2 = ws.Row(4);
|
||||
row2.Style.Fill.BackgroundColor = XLColor.DarkOrange;
|
||||
row2.Height = 3;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
using ClosedXML.Excel;
|
||||
using MoreLinq;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace ClosedXML_Examples.Sparklines
|
||||
{
|
||||
public class SampleSparklines : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws1 = workbook.AddWorksheet("Linear");
|
||||
|
||||
FillSampleData(ws1);
|
||||
|
||||
ws1.Range("A2:A4").Merge().SetValue("Linear, Colorful 1, All markers, SameForAll scale");
|
||||
ws1.SparklineGroups.Add("B2:B4", "C2:P4")
|
||||
.SetStyle(XLSparklineTheme.Colorful1)
|
||||
.SetShowMarkers(XLSparklineMarkers.All).VerticalAxis
|
||||
.SetMaxAxisType(XLSparklineAxisMinMax.SameForAll)
|
||||
.SetMinAxisType(XLSparklineAxisMinMax.SameForAll);
|
||||
|
||||
ws1.Range("A5:A7").Merge().SetValue("Linear, Colorful 2, First+Last+High+Low, Automatic scale");
|
||||
ws1.SparklineGroups.Add("B5:B7", "C5:P7")
|
||||
.SetStyle(XLSparklineTheme.Colorful2)
|
||||
.SetShowMarkers(XLSparklineMarkers.FirstPoint | XLSparklineMarkers.LastPoint | XLSparklineMarkers.HighPoint | XLSparklineMarkers.LowPoint);
|
||||
|
||||
ws1.Range("A8:A10").Merge().SetValue("Linear, Colorful 3, Markers+Negative, Custom scale");
|
||||
ws1.SparklineGroups.Add("B8:B10", "C8:P10")
|
||||
.SetStyle(XLSparklineTheme.Colorful3)
|
||||
.SetShowMarkers(XLSparklineMarkers.Markers | XLSparklineMarkers.NegativePoints)
|
||||
.VerticalAxis
|
||||
.SetManualMax(100)
|
||||
.SetManualMin(-80);
|
||||
|
||||
ws1.Range("A11:A13").Merge().SetValue("Linear, Colorful 1, Date range");
|
||||
ws1.SparklineGroups.Add("B11:B13", "C11:P13")
|
||||
.SetStyle(XLSparklineTheme.Colorful1)
|
||||
.SetDateRange(ws1.Range("C1:P1"));
|
||||
|
||||
ws1.Range("A14:A16").Merge().SetValue("Linear, Colorful 4, Line weight=2, Right to left");
|
||||
ws1.SparklineGroups.Add("B14:B16", "C14:P16")
|
||||
.SetStyle(XLSparklineTheme.Colorful4)
|
||||
.SetLineWeight(2)
|
||||
.HorizontalAxis
|
||||
.SetVisible(true)
|
||||
.SetColor(XLColor.Red)
|
||||
.SetRightToLeft(true);
|
||||
|
||||
ws1.Range("A17:A19").Merge().SetValue("Linear, Colorful 3, Different ranges");
|
||||
ws1.SparklineGroups.Add("B17", "C17:P17")
|
||||
.Add("B18", "C18:K18").Single().SparklineGroup
|
||||
.Add("B19", "C19:E19").Single().SparklineGroup
|
||||
.SetStyle(XLSparklineTheme.Colorful3)
|
||||
.SetShowMarkers(XLSparklineMarkers.FirstPoint | XLSparklineMarkers.LastPoint);
|
||||
|
||||
|
||||
var ws2 = ws1.CopyTo("Column");
|
||||
ws2.SparklineGroups.ForEach(g =>
|
||||
g.SetType(XLSparklineType.Column));
|
||||
|
||||
ws2.Cell("A2").Value = "Column, Colorful 1, All markers, SameForAll scale";
|
||||
ws2.Cell("A5").Value = "Column, Colorful 2, First+Last+High+Low, Automatic scale";
|
||||
ws2.Cell("A8").Value = "Column, Colorful 3, Markers+Negative, Custom scale";
|
||||
ws2.Cell("A11").Value = "Column, Colorful 1, Date range";
|
||||
ws2.Cell("A14").Value = "Column, Colorful 4, Line weight=2, Right to left";
|
||||
ws2.Cell("A17").Value = "Column, Colorful 3, Different ranges";
|
||||
|
||||
|
||||
var ws3 = ws1.CopyTo("Stacked");
|
||||
ws3.SparklineGroups.ForEach(g =>
|
||||
g.SetType(XLSparklineType.Stacked));
|
||||
|
||||
ws3.Cell("A2").Value = "Stacked, Colorful 1, All markers, SameForAll scale";
|
||||
ws3.Cell("A5").Value = "Stacked, Colorful 2, First+Last+High+Low, Automatic scale";
|
||||
ws3.Cell("A8").Value = "Stacked, Colorful 3, Markers+Negative, Custom scale";
|
||||
ws3.Cell("A11").Value = "Stacked, Colorful 1, Date range";
|
||||
ws3.Cell("A14").Value = "Stacked, Colorful 4, Line weight=2, Right to left";
|
||||
ws3.Cell("A17").Value = "Stacked, Colorful 3, Different ranges";
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
private void FillSampleData(IXLWorksheet ws)
|
||||
{
|
||||
ws.Column(1).Style.Alignment.SetWrapText(true);
|
||||
ws.Column(1).Width = 30;
|
||||
ws.Column(2).Width = 30;
|
||||
|
||||
ws.Range("C1:P1").Cells()
|
||||
.ForEach(c => c.Value = new DateTime(2016, 1, 1).AddDays(c.Address.ColumnNumber * 7));
|
||||
|
||||
ws.Range("C2:P19").Cells()
|
||||
.ForEach(c => c.Value = Math.Round(c.Address.RowNumber * Math.Sin(c.Address.ColumnNumber) * 10, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using ClosedXML_Examples.Styles;
|
||||
using System.IO;
|
||||
|
||||
namespace ClosedXML_Examples
|
||||
{
|
||||
public class StyleExamples
|
||||
{
|
||||
#region Variables
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
// Public
|
||||
public StyleExamples()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Private
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create()
|
||||
{
|
||||
var path = Program.BaseCreatedDirectory;
|
||||
new StyleFont().Create(Path.Combine(path, "styleFont.xlsx"));
|
||||
new StyleFill().Create(Path.Combine(path, "styleFill.xlsx"));
|
||||
new StyleBorder().Create(Path.Combine(path, "styleBorder.xlsx"));
|
||||
new StyleAlignment().Create(Path.Combine(path, "styleAlignment.xlsx"));
|
||||
new StyleNumberFormat().Create(Path.Combine(path, "styleNumberFormat.xlsx"));
|
||||
new StyleIncludeQuotePrefix().Create(Path.Combine(path, "styleIncludeQuotePrefix.xlsx"));
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Styles
|
||||
{
|
||||
public class DefaultStyles : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
// Create our workbook
|
||||
var workbook = new XLWorkbook();
|
||||
|
||||
// This worksheet will have the default style, row height, column width, and page setup
|
||||
var ws1 = workbook.Worksheets.Add("Default Style");
|
||||
|
||||
// Change the default row height for all new worksheets in this workbook
|
||||
workbook.RowHeight = 30;
|
||||
|
||||
var ws2 = workbook.Worksheets.Add("Tall Rows");
|
||||
|
||||
// Create a worksheet and change the default row height
|
||||
var ws3 = workbook.Worksheets.Add("Short Rows");
|
||||
ws3.RowHeight = 7.5;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Styles
|
||||
{
|
||||
public class PurpleWorksheet : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Purple Worksheet");
|
||||
|
||||
ws.Style.Fill.BackgroundColor = XLColor.Purple;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
|
||||
namespace ClosedXML_Examples.Styles
|
||||
{
|
||||
public class StyleAlignment : IXLExample
|
||||
{
|
||||
#region Variables
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
// Public
|
||||
public StyleAlignment()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Private
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
// Public
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
// Public
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Style Alignment");
|
||||
|
||||
var co = 2;
|
||||
var ro = 1;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Horizontal = Right";
|
||||
ws.Cell(ro, co).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Indent = 2";
|
||||
ws.Cell(ro, co).Style.Alignment.Indent = 2;
|
||||
|
||||
ws.Cell(++ro, co).Value = "JustifyLastLine = true";
|
||||
ws.Cell(ro, co).Style.Alignment.JustifyLastLine = true;
|
||||
|
||||
ws.Cell(++ro, co).Value = "ReadingOrder = ContextDependent";
|
||||
ws.Cell(ro, co).Style.Alignment.ReadingOrder = XLAlignmentReadingOrderValues.ContextDependent;
|
||||
|
||||
ws.Cell(++ro, co).Value = "RelativeIndent = 2";
|
||||
ws.Cell(ro, co).Style.Alignment.RelativeIndent = 2;
|
||||
|
||||
ws.Cell(++ro, co).Value = "ShrinkToFit = true";
|
||||
ws.Cell(ro, co).Style.Alignment.ShrinkToFit = true;
|
||||
|
||||
ws.Cell(++ro, co).Value = "TextRotation = 45";
|
||||
ws.Cell(ro, co).Style.Alignment.TextRotation = 45;
|
||||
|
||||
ws.Cell(++ro, co).Value = "TopToBottom = true";
|
||||
ws.Cell(ro, co).Style.Alignment.TopToBottom = true;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Vertical = Center";
|
||||
ws.Cell(ro, co).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
|
||||
|
||||
ws.Cell(++ro, co).Value = "WrapText = true";
|
||||
ws.Cell(ro, co).Style.Alignment.WrapText = true;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
// Override
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ClosedXML_Examples.Styles
|
||||
{
|
||||
public class StyleBorder : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Style Border");
|
||||
|
||||
var co = 2;
|
||||
var ro = 1;
|
||||
|
||||
ws.Cell(++ro, co).Value = "BottomBorder = Thick; BottomBorderColor = Red";
|
||||
ws.Cell(ro, co).Style.Border.BottomBorder = XLBorderStyleValues.Thick;
|
||||
ws.Cell(ro, co).Style.Border.BottomBorderColor = XLColor.Red;
|
||||
|
||||
ws.Cell(++ro, co).Value = "LeftBorder = Thick; LeftBorderColor = Blue";
|
||||
ws.Cell(ro, co).Style.Border.LeftBorder = XLBorderStyleValues.Thick;
|
||||
ws.Cell(ro, co).Style.Border.LeftBorderColor = XLColor.Blue;
|
||||
|
||||
ws.Cell(++ro, co).Value = "TopBorder = Thick; TopBorderColor = Yellow";
|
||||
ws.Cell(ro, co).Style.Border.TopBorder = XLBorderStyleValues.Thick;
|
||||
ws.Cell(ro, co).Style.Border.TopBorderColor = XLColor.Yellow;
|
||||
|
||||
ws.Cell(++ro, co).Value = "RightBorder = Thick; RightBorderColor = Black";
|
||||
ws.Cell(ro, co).Style.Border.RightBorder = XLBorderStyleValues.Thick;
|
||||
ws.Cell(ro, co).Style.Border.RightBorderColor = XLColor.Black;
|
||||
|
||||
ws.Cell(++ro, co).Value = "DiagonalBorder = Thin; DiagonalBorderColor = Red; DiagonalUp = true";
|
||||
ws.Cell(ro, co).Style.Border.DiagonalBorder = XLBorderStyleValues.Thin;
|
||||
ws.Cell(ro, co).Style.Border.DiagonalBorderColor = XLColor.Red;
|
||||
ws.Cell(ro, co).Style.Border.DiagonalUp = true;
|
||||
|
||||
ws.Cell(++ro, co).Value = "DiagonalBorder = Thin; DiagonalBorderColor = Red; DiagonalDown = true";
|
||||
ws.Cell(ro, co).Style.Border.DiagonalBorder = XLBorderStyleValues.Thin;
|
||||
ws.Cell(ro, co).Style.Border.DiagonalBorderColor = XLColor.Red;
|
||||
ws.Cell(ro, co).Style.Border.DiagonalDown = true;
|
||||
|
||||
ws.Cell(++ro, co).Value = "DiagonalBorder = Thin; DiagonalBorderColor = Red; DiagonalUp = true; DiagonalDown = true";
|
||||
ws.Cell(ro, co).Style.Border.DiagonalBorder = XLBorderStyleValues.Thin;
|
||||
ws.Cell(ro, co).Style.Border.DiagonalBorderColor = XLColor.Red;
|
||||
ws.Cell(ro, co).Style.Border.DiagonalUp = true;
|
||||
ws.Cell(ro, co).Style.Border.DiagonalDown = true;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
|
||||
namespace ClosedXML_Examples.Styles
|
||||
{
|
||||
public class StyleFill : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Style Fill");
|
||||
|
||||
var co = 2;
|
||||
var ro = 1;
|
||||
|
||||
ws.Cell(++ro, co + 1).Value = "BackgroundColor = Red";
|
||||
ws.Cell(ro, co).Style.Fill.BackgroundColor = XLColor.Red;
|
||||
|
||||
ws.Cell(++ro, co + 1).Value = "PatternType = DarkTrellis; PatternColor = Orange; BackgroundColor = Blue";
|
||||
ws.Cell(ro, co).Style.Fill.PatternType = XLFillPatternValues.DarkTrellis;
|
||||
ws.Cell(ro, co).Style.Fill.PatternColor = XLColor.Orange;
|
||||
ws.Cell(ro, co).Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
|
||||
namespace ClosedXML_Examples.Styles
|
||||
{
|
||||
public class StyleFont : IXLExample
|
||||
{
|
||||
public void Create(String filePath)
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add("Style Font");
|
||||
|
||||
var co = 2;
|
||||
var ro = 1;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Bold";
|
||||
ws.Cell(ro, co).Style.Font.Bold = true;
|
||||
|
||||
ws.Cell(++ro, co).Value = "FontColor - Red";
|
||||
ws.Cell(ro, co).Style.Font.FontColor = XLColor.Red;
|
||||
|
||||
ws.Cell(++ro, co).Value = "FontFamilyNumbering - Script";
|
||||
ws.Cell(ro, co).Style.Font.FontFamilyNumbering = XLFontFamilyNumberingValues.Script;
|
||||
|
||||
ws.Cell(++ro, co).Value = "FontCharSet - العربية التنضيد";
|
||||
ws.Cell(ro, co).Style
|
||||
.Font.SetFontName("Arabic Typesetting")
|
||||
.Font.SetFontCharSet(XLFontCharSet.Arabic);
|
||||
|
||||
ws.Cell(++ro, co).Value = "FontName - Stencil";
|
||||
ws.Cell(ro, co).Style.Font.FontName = "Stencil";
|
||||
|
||||
ws.Cell(++ro, co).Value = "FontSize - 15";
|
||||
ws.Cell(ro, co).Style.Font.FontSize = 15;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Italic - true";
|
||||
ws.Cell(ro, co).Style.Font.Italic = true;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Strikethrough - true";
|
||||
ws.Cell(ro, co).Style.Font.Strikethrough = true;
|
||||
|
||||
ws.Cell(++ro, co).Value = "Underline - Double";
|
||||
ws.Cell(ro, co).Style.Font.Underline = XLFontUnderlineValues.Double;
|
||||
|
||||
ws.Cell(++ro, co).Value = "VerticalAlignment - Superscript";
|
||||
ws.Cell(ro, co).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript;
|
||||
|
||||
ws.Column(co).AdjustToContents();
|
||||
|
||||
workbook.SaveAs(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user