-
Notifications
You must be signed in to change notification settings - Fork 892
Data Types
KnapSac edited this page Jul 19, 2019
·
4 revisions
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 = "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 = "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 = "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 = XLDataTypes.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.Columns(2, 3).AdjustToContents();
workbook.SaveAs("DataTypes.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