- Notifications
You must be signed in to change notification settings - Fork906
Basic Table
Jan Havlíček edited this pageJun 6, 2024 ·4 revisions
Important
This page is obsolete and no longer being actively maintained. Please refer to pageTables atdocs.closedxml.io for the latest information.

Creating a new workbook
varwb=newXLWorkbook();
Adding a worksheet
varws=wb.Worksheets.Add("Contacts");
Adding text
// Titlews.Cell("B2").Value="Contacts";// First Namesws.Cell("B3").Value="FName";ws.Cell("B4").Value="John";ws.Cell("B5").Value="Hank";ws.Cell("B6").Value="Dagny";// Last Namesws.Cell("C3").Value="LName";ws.Cell("C4").Value="Galt";ws.Cell("C5").Value="Rearden";ws.Cell("C6").Value="Taggart";
Adding more data types
// Booleanws.Cell("D3").Value="Outcast";ws.Cell("D4").Value=true;ws.Cell("D5").Value=false;ws.Cell("D6").Value=false;// DateTimews.Cell("E3").Value="DOB";ws.Cell("E4").Value=newDateTime(1919,1,21);ws.Cell("E5").Value=newDateTime(1907,3,4);ws.Cell("E6").Value=newDateTime(1921,12,15);// Numericws.Cell("F3").Value="Income";ws.Cell("F4").Value=2000;ws.Cell("F5").Value=40000;ws.Cell("F6").Value=10000;
Defining ranges
// From worksheetvarrngTable=ws.Range("B2:F6");// From another rangevarrngDates=rngTable.Range("D3:D5");// The address is relative to rngTable (NOT the worksheet)varrngNumbers=rngTable.Range("E3:E5");// The address is relative to rngTable (NOT the worksheet)
Formatting dates and numbers
// Using OpenXML's predefined formatsrngDates.Style.NumberFormat.NumberFormatId=15;// Using a custom formatrngNumbers.Style.NumberFormat.Format="$ #,##0";
Formatting headers
varrngHeaders=rngTable.Range("A2:E2");// The address is relative to rngTable (NOT the worksheet)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
//Add a thick outside borderrngTable.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("BasicTable.xlsx");
- 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