- Notifications
You must be signed in to change notification settings - Fork906
Inserting Tables
Francois Botha edited this pageFeb 26, 2018 ·6 revisions
This example shows how to insert data into a worksheet by using the method cell.InsertTable(collection)
Notes:
- The difference between InsertData and InsertTable is that InsertData doesn't insert column names and returns a range. InsertTable will insert the column names and returns a table.
- You can use the different overloads to specify whether or not to create the Excel table, and the table name.
- For more information about tables seeUsing Tables
- (.Net 4 only) When inserting a table from a collection of objects ClosedXML will check for the custom property
[XLColumn(Header="Field Name")]and use it for column name if exists. Otherwise it will use the property's name.

publicvoidCreate(){varwb=newXLWorkbook();varws=wb.Worksheets.Add("Inserting Tables");// From a list of stringsvarlistOfStrings=newList<String>();listOfStrings.Add("House");listOfStrings.Add("Car");ws.Cell(1,1).Value="From Strings";ws.Cell(1,1).AsRange().AddToNamed("Titles");vartableWithStrings=ws.Cell(2,1).InsertTable(listOfStrings);// From a list of arraysvarlistOfArr=newList<Int32[]>();listOfArr.Add(newInt32[]{1,2,3});listOfArr.Add(newInt32[]{1});listOfArr.Add(newInt32[]{1,2,3,4,5,6});ws.Cell(1,3).Value="From Arrays";ws.Range(1,3,1,8).Merge().AddToNamed("Titles");vartableWithArrays=ws.Cell(2,3).InsertTable(listOfArr);// From a DataTablevardataTable=GetTable();ws.Cell(7,1).Value="From DataTable";ws.Range(7,1,7,4).Merge().AddToNamed("Titles");vartableWithData=ws.Cell(8,1).InsertTable(dataTable.AsEnumerable());// From a queryvarlist=newList<Person>();list.Add(newPerson(){Name="John",Age=30,House="On Elm St."});list.Add(newPerson(){Name="Mary",Age=15,House="On Main St."});list.Add(newPerson(){Name="Luis",Age=21,House="On 23rd St."});list.Add(newPerson(){Name="Henry",Age=45,House="On 5th Ave."});varpeople=frompinlistwherep.Age>=21selectnew{p.Name,p.House,p.Age};ws.Cell(7,6).Value="From Query";ws.Range(7,6,7,8).Merge().AddToNamed("Titles");vartableWithPeople=ws.Cell(8,6).InsertTable(people.AsEnumerable());// Prepare the style for the titlesvartitlesStyle=wb.Style;titlesStyle.Font.Bold=true;titlesStyle.Alignment.Horizontal=XLAlignmentHorizontalValues.Center;titlesStyle.Fill.BackgroundColor=XLColor.Cyan;// Format all titles in one shotwb.NamedRanges.NamedRange("Titles").Ranges.Style=titlesStyle;ws.Columns().AdjustToContents();wb.SaveAs("InsertingTables.xlsx");}classPerson{publicStringHouse{get;set;}publicStringName{get;set;}publicInt32Age{get;set;}}privateDataTableGetTable(){DataTabletable=newDataTable();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);returntable;}
- 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