Cell is the smallest unit of a grid on which various operations can be carried out. Lets discuss these operations in the following sections.
Excel lets you access specific cells using values and also allows to set the cell values. To do so, you can use Value property of the XLCell class. In this example, we set the value for the cell at [3,2] index.
The following code shows how you can set value for a specified cell in Excel.
C# |
Copy Code
|
---|---|
//set value in cell
sheet[3, 2].Value = 10;
|
Excel allows you to merge cells with same values, making them span multiple rows or columns. This capability is useful for enhancing appearance and clarity of data displayed in the sheet. Cell merging has several possible uses. For example, you can use it to create merged table headers, merged data views, or grids where the text spills into adjacent columns. In Excel, you can merge cells using MergedCells property of the XLSheet class.
To merge cells in Excel, use the following code. In this example, we first create column and row ranges, set the text for them and add these ranges to the XLCellRangeCollection using the MergedCells property.
C# |
Copy Code
|
---|---|
//Select range to Merge XLCellRange _ColRange = new C1.C1Excel.XLCellRange(4, 6, 0, 8); XLCellRange _RowRange = new C1.C1Excel.XLCellRange(9, 21, 3, 4); //Set Text for Merged Cells c1XLBook1.Sheets[0][4, 0].Value = "Merged Cells"; c1XLBook1.Sheets[0][9, 3].Value = "Merged Cells"; //Merge Cells c1XLBook1.Sheets[0].MergedCells.Add(_ColRange); c1XLBook1.Sheets[0].MergedCells.Add(_RowRange); |
When a word is too long to fit on one line of text, the word-wrap feature automatically moves it to the beginning of the next line. This feature can be used when you want excel to automatically adjust large texts as shown in the following image.
To word-wrap text in Excel, you can use WordWrap property of the XLStyle class as shown in the following code. In this example, we set the WordWrap property for the cell at index [10,10] by creating an object of XLStyle class.
C# |
Copy Code
|
---|---|
XLStyle style = new XLStyle(c1XLBook1); style.WordWrap = true; //apply word wrap c1XLBook1.Sheets[0][10, 10].Style = style; |
Hyperlinks direct users to another web page or document which can be useful in providing references or additional information. In Excel, you can insert hyperlinks by using Hyperlink property of the XLCell class.
To insert a hyperlink in a worksheet, use the following code. Here, we insert a hyperlink in a cell to direct the end-users to "www.google.com".
C# |
Copy Code
|
---|---|
c1XLBook1.Sheets[0][2, 3].Hyperlink = "https://www.google.com/";
|
Comments are useful in providing additional information or description about data stored in the sheets. In Excel, you can easily access comments collection using Comments property of the XLCell class and add them in the sheet using Add method. The comments are represented by the XLComment class in Excel which provides various properties to set the author, cell, column index, and much more.
To add a comment in Excel, use the following code. In this example, we add a comment "Test comment" to the cell in a rectangular box.
C# |
Copy Code
|
---|---|
c1XLBook1.Sheets[0].Comments.Add(2, 3, "John", "Test comment"); c1XLBook1.Sheets[0].Comments[0].TextBox.Rectangle = new Rectangle(220, 210, 1900, 1200); |