-
Notifications
You must be signed in to change notification settings - Fork 891
Copying IEnumerable Collections
Jan Havlíček edited this page Jul 4, 2024
·
4 revisions
Important
This page is obsolete and no longer being actively maintained. Please refer to page Bulk insert data at docs.closedxml.io for the latest information.
Since 0.100, it is no longer possible to bulk insert data through IXLCell.Value
property, see migration to 0.100
This example shows how to insert data into a worksheet by setting cell.Value = collection
public void Create()
{
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.AsEnumerable();
// 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.AsEnumerable(); // Very Important to call the AsEnumerable method
// otherwise it won't be copied.
// 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("Collections.xlsx");
}
class Person
{
public String House { get; set; }
public String Name { get; set; }
public Int32 Age { get; set; }
}
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", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
return table;
}
- How do I deliver an Excel file in ASP.NET?
- Does it support Excel 2003 and prior formats (.xls)?
- How can I insert an image?
- Text with numbers are getting converted to numbers, what's up with that?
- How do I get the result of a formula?
- Data Types
- Creating Multiple Worksheets
- Organizing Sheets
- Loading and Modifying Files
- Using Lambda Expressions
- Cell Values
- Workbook Properties
- Using Formulas
- Evaluating Formulas
- Creating Rows And Columns Outlines
- Hide Unhide Rows And Columns
- Freeze Panes
- Copying Worksheets
- Using Hyperlinks
- Data Validation
- Hide Worksheets
- Sheet Protection
- Tab Colors
- Conditional Formatting
- Pivot Table example
- Sparklines
- Copying IEnumerable Collections
- Inserting Data
- Inserting Tables
- Adding DataTable as Worksheet
- Adding DataSet
- Styles - Alignment
- Styles - Border
- Styles - Fill
- Styles - Font
- Styles - NumberFormat
- NumberFormatId Lookup Table
- Style Worksheet
- Style Rows and Columns
- Using Default Styles
- Using Colors
- ClosedXML Predefined Colors
- Excel Indexed Colors
- Using Rich Text
- Using Phonetics
- Defining Ranges
- Merging Cells
- Clearing Ranges
- Deleting Ranges
- Multiple Ranges
- Shifting Ranges
- Transpose Ranges
- Named Ranges
- Accessing Named Ranges
- Copying Ranges
- Using Tables
- Sorting Data
- Selecting Cells and Ranges
- Row Height and Styles
- Selecting Rows
- Inserting Rows
- Inserting and Deleting Rows
- Adjust Row Height and Column Width to Contents
- Row Cells
- Column Width and Styles
- Selecting Columns
- Inserting Columns
- Inserting and Deleting Columns
- Adjust Row Height and Column Width to Contents
- Column Cells
- Pages Tab
- Paper Size Lookup Table
- Margins Tab
- Headers and Footers Tab
- Sheet Tab
- Print Areas and Page Breaks