-
Notifications
You must be signed in to change notification settings - Fork 813
Cell Values
Francois Botha edited this page Feb 26, 2018
·
4 revisions
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();
workbook.SaveAs("CellValues.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