Udostępnij za pośrednictwem


Column Class

Definition

Column Width and Formatting.

This class is available in Office 2007 and above.

When the object is serialized out as xml, it's qualified name is x:col.

[DocumentFormat.OpenXml.OfficeAvailability(DocumentFormat.OpenXml.FileFormatVersions.Office2007)]
[DocumentFormat.OpenXml.SchemaAttr(22, "col")]
public class Column : DocumentFormat.OpenXml.OpenXmlLeafElement
public class Column : DocumentFormat.OpenXml.OpenXmlLeafElement
[DocumentFormat.OpenXml.SchemaAttr(22, "col")]
public class Column : DocumentFormat.OpenXml.OpenXmlLeafElement
[DocumentFormat.OpenXml.SchemaAttr("x:col")]
public class Column : DocumentFormat.OpenXml.OpenXmlLeafElement
[DocumentFormat.OpenXml.SchemaAttr("x:col")]
public class Column : DocumentFormat.OpenXml.TypedOpenXmlLeafElement
[DocumentFormat.OpenXml.OfficeAvailability(DocumentFormat.OpenXml.FileFormatVersions.Office2007)]
public class Column : DocumentFormat.OpenXml.OpenXmlLeafElement
[<DocumentFormat.OpenXml.OfficeAvailability(DocumentFormat.OpenXml.FileFormatVersions.Office2007)>]
[<DocumentFormat.OpenXml.SchemaAttr(22, "col")>]
type Column = class
    inherit OpenXmlLeafElement
type Column = class
    inherit OpenXmlLeafElement
[<DocumentFormat.OpenXml.SchemaAttr(22, "col")>]
type Column = class
    inherit OpenXmlLeafElement
[<DocumentFormat.OpenXml.SchemaAttr("x:col")>]
type Column = class
    inherit OpenXmlLeafElement
[<DocumentFormat.OpenXml.SchemaAttr("x:col")>]
type Column = class
    inherit TypedOpenXmlLeafElement
[<DocumentFormat.OpenXml.OfficeAvailability(DocumentFormat.OpenXml.FileFormatVersions.Office2007)>]
type Column = class
    inherit OpenXmlLeafElement
Public Class Column
Inherits OpenXmlLeafElement
Public Class Column
Inherits TypedOpenXmlLeafElement
Inheritance
Inheritance
Attributes

Examples

The following example opens a spreadsheet document for read-only access. By using the worksheet name and the cell name, it gets the column name and displays it.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using DocumentFormat.OpenXml.Packaging;  
using DocumentFormat.OpenXml.Spreadsheet;  
using System.Text.RegularExpressions;  

namespace ColumnEx  
{  
    class Program  
    {  
        // Given a document name, a worksheet name, and a cell name, gets the column of the cell.  
        static void Main(string[] args)  
        {  
            string docName = @"C:\Users\Public\Documents\ColumnEx.xlsx";  
            string worksheetName = "Sheet1";  
            string cellName = "B2";  

            // Open the document as read-only.  
            using (SpreadsheetDocument document = SpreadsheetDocument.Open(docName, false))  
            {  
                IEnumerable<Sheet> sheets =  
                    document.WorkbookPart.Workbook.Descendants<Sheet>()  
                    .Where(s => s.Name == worksheetName);  
                WorksheetPart worksheetPart = (WorksheetPart)document  
                    .WorkbookPart.GetPartById(sheets.First().Id);  

                // Get the column name for the specified cell.  
                // Create a regular expression to match the column name portion of the cell name.  
                Regex regex = new Regex("[A-Za-z]+");  
                Match match = regex.Match(cellName);  
                Console.WriteLine("{0} {1}","The column is:", match.Value);  
                Console.ReadKey();  
            }  
        }  
    }  
}  
//  Output:  
// The column is: B  
Imports System.Collections.Generic  
Imports System.Linq  
Imports DocumentFormat.OpenXml.Packaging  
Imports DocumentFormat.OpenXml.Spreadsheet  
Imports System.Text.RegularExpressions  

Module Module1  
    ' Given a document name, a worksheet name, and a cell name, gets the column of the cell.  
    Sub Main(ByVal args As String())  
        Dim docName As String = "C:\Users\Public\Documents\ColumnEx.xlsx"  
        Dim worksheetName As String = "Sheet1"  
        Dim cellName As String = "B2"  

        ' Open the document as read-only.  
        Using document As SpreadsheetDocument = SpreadsheetDocument.Open(docName, False)  
            Dim sheets As IEnumerable(Of Sheet) = document.WorkbookPart.Workbook.Descendants(Of Sheet)().Where(Function(s) s.Name = worksheetName)  
            Dim worksheetPart As WorksheetPart = DirectCast(document.WorkbookPart.GetPartById(sheets.First().Id), WorksheetPart)  

            ' Get the column name for the specified cell.  
            ' Create a regular expression to match the column name portion of the cell name.  
            Dim regex As New Regex("[A-Za-z]+")  
            Dim match As Match = regex.Match(cellName)  
            Console.WriteLine("{0} {1}", "The column is:", match.Value)  
            Console.ReadKey()  
        End Using  
    End Sub  
End Module  
' Output:  
' The column is: B  

Remarks

[ISO/IEC 29500-1 1st Edition]

col (Column Width & Formatting)

Defines column width and column formatting for one or more columns of the worksheet.

[Example:This example shows that column 5 (E) has width and style information applied.

<col min="5" max="5" width="9.140625" style="3"/>  

end example]

Parent Elements
cols (§18.3.1.17)
Attributes Description
bestFit (Best Fit Column Width) Flag indicating if the specified column(s) is set to 'best fit'. 'Best fit' is set to true under these conditions:

- The column width has never been manually set by the user,
- The column width is not the default width
- 'Best fit' means that when numbers are typed into a cell contained in a 'best fit' column, the column width should automatically resize to display the number. [Note: In best fit cases, column width must not be made smaller, only larger. end note]

The possible values for this attribute are defined by the W3C XML Schema boolean datatype.
collapsed (Collapsed) Flag indicating if the outlining of the affected column(s) is in the collapsed state. See description of row collapsed and outlinePr element's summaryBelow and summaryRight attributes for detailed information.

The possible values for this attribute are defined by the W3C XML Schema boolean datatype.
customWidth (Custom Width) Flag indicating that the column width for the affected column(s) is different from the default or has been manually set.

The possible values for this attribute are defined by the W3C XML Schema boolean datatype.
hidden (Hidden Columns) Flag indicating if the affected column(s) are hidden on this worksheet.

The possible values for this attribute are defined by the W3C XML Schema boolean datatype.
max (Maximum Column) Last column affected by this 'column info' record.

The possible values for this attribute are defined by the W3C XML Schema unsignedInt datatype.
min (Minimum Column) First column affected by this 'column info' record.

The possible values for this attribute are defined by the W3C XML Schema unsignedInt datatype.
outlineLevel (Outline Level) Outline level of affected column(s). Range is 0 to 7. See description of outlinePr element's summaryBelow and summaryRight attributes for detailed information.

The possible values for this attribute are defined by the W3C XML Schema unsignedByte datatype.
phonetic (Show Phonetic Information) Flag indicating if the phonetic information should be displayed by default for the affected column(s) of the worksheet.

The possible values for this attribute are defined by the W3C XML Schema boolean datatype.
style (Style) Default style for the affected column(s). Affects cells not yet allocated in the column(s). In other words, this style applies to new columns.

The possible values for this attribute are defined by the W3C XML Schema unsignedInt datatype.
width (Column Width) Column width measured as the number of characters of the maximum digit width of the numbers 0, 1, 2, …, 9 as rendered in the normal style's font. There are 4 pixels of margin padding (two on each side), plus 1 pixel padding for the gridlines.

width = Truncate([{Number of Characters} * {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256

[Example: Using the Calibri font as an example, the maximum digit width of 11 point font size is 7 pixels (at 96 dpi). In fact, each digit is the same width for this font. Therefore, if the cell width is 8 characters wide, the value of this attribute must be Truncate([8*7+5]/7*256)/256 = 8.7109375. end example]

To translate the value of width in the file into the column width value at runtime (expressed in terms of pixels), use this calculation:

=Truncate(((256 * {width} + Truncate(128/{Maximum Digit Width}))/256)*{Maximum Digit Width})

[Example: Using the same example as above, the calculation would be Truncate(((256*8.7109375+Truncate(128/7))/256)*7) = 61 pixels. end example]

To translate from pixels to character width, use this calculation:

=Truncate(({pixels}-5)/{Maximum Digit Width} * 100+0.5)/100

[Example: Using the example above, the calculation would be Truncate((61-5)/7*100+0.5)/100 = 8 characters. end example]

[Note: when wide borders are applied, part of the left/right border must overlap with the 2 pixel padding on each side. Wide borders do not affect the width calculation of the column. end note]

[Note: When the sheet is in the mode to view formulas instead of values, the pixel width of the column is doubled. end note]

The possible values for this attribute are defined by the W3C XML Schema double datatype.

[Note: The W3C XML Schema definition of this element’s content model (CT_Col) is located in §A.2. end note]

� ISO/IEC29500: 2008.

Constructors

Column()

Initializes a new instance of the Column class.

Properties

BestFit

Best Fit Column Width

Represents the following attribute in the schema: bestFit

ChildElements

Gets all the child nodes of the current element.

(Inherited from OpenXmlElement)
Collapsed

Collapsed

Represents the following attribute in the schema: collapsed

CustomWidth

Custom Width

Represents the following attribute in the schema: customWidth

ExtendedAttributes

Gets all extended attributes (attributes not defined in the schema) of the current element.

(Inherited from OpenXmlElement)
Features

Gets a IFeatureCollection for the current element. This feature collection will be read-only, but will inherit features from its parent part and package if available.

(Inherited from OpenXmlElement)
FirstChild

Gets the first child of the OpenXmlElement element. Returns null (Nothing in Visual Basic) if there is no such OpenXmlElement element.

(Inherited from OpenXmlElement)
HasAttributes

Gets a value indicating whether the current element has any attributes.

(Inherited from OpenXmlElement)
HasChildren

Gets a value that indicates whether the current element has any child elements.

(Inherited from OpenXmlLeafElement)
Hidden

Hidden Columns

Represents the following attribute in the schema: hidden

InnerText

Gets or sets the concatenated values of the node and all of its children.

(Inherited from OpenXmlElement)
InnerXml

Gets or sets the markup that only represents child elements of the current element.

(Inherited from OpenXmlLeafElement)
LastChild

Gets the last child of the OpenXmlElement element. Returns null (Nothing in Visual Basic) if there is no such OpenXmlElement element.

(Inherited from OpenXmlElement)
LocalName
LocalName

Gets the local name of the current element.

(Inherited from OpenXmlElement)
Max

Maximum Column

Represents the following attribute in the schema: max

MCAttributes

Gets or sets the markup compatibility attributes. Returns null if no markup compatibility attributes are defined for the current element.

(Inherited from OpenXmlElement)
Min

Minimum Column

Represents the following attribute in the schema: min

NamespaceDeclarations

Gets all the namespace declarations defined in the current element. Returns an empty enumerator if there is no namespace declaration.

(Inherited from OpenXmlElement)
NamespaceUri

Gets the namespace URI of the current element.

(Inherited from OpenXmlElement)
OpenXmlElementContext

Gets the OpenXmlElementContext of the current element.

(Inherited from OpenXmlElement)
OuterXml

Gets the markup that represents the current element and all of its child elements.

(Inherited from OpenXmlElement)
OutlineLevel

Outline Level

Represents the following attribute in the schema: outlineLevel

Parent

Gets the parent element of the current element.

(Inherited from OpenXmlElement)
Phonetic

Show Phonetic Information

Represents the following attribute in the schema: phonetic

Prefix

Gets the namespace prefix of current element.

(Inherited from OpenXmlElement)
Style

Style

Represents the following attribute in the schema: style

Width

Column Width

Represents the following attribute in the schema: width

XmlQualifiedName

Gets the qualified name of the current element.

(Inherited from OpenXmlElement)
XName

Gets the qualified name of the current element.

(Inherited from OpenXmlElement)

Methods

AddAnnotation(Object)

Adds an object to the current OpenXmlElement element's list of annotations.

(Inherited from OpenXmlElement)
AddNamespaceDeclaration(String, String)

Adds a namespace declaration to the current node.

(Inherited from OpenXmlElement)
Ancestors()

Enumerates all of the current element's ancestors.

(Inherited from OpenXmlElement)
Ancestors<T>()

Enumerates only the current element's ancestors that have the specified type.

(Inherited from OpenXmlElement)
Annotation(Type)

Get the first annotation object of the specified type from the current OpenXmlElement element.

(Inherited from OpenXmlElement)
Annotation<T>()

Get the first annotation object of the specified type from the current OpenXmlElement element.

(Inherited from OpenXmlElement)
Annotations(Type)

Gets a collection of annotations with the specified type for the current OpenXmlElement element.

(Inherited from OpenXmlElement)
Annotations<T>()

Gets a collection of annotations with the specified type for the current OpenXmlElement element.

(Inherited from OpenXmlElement)
Append(IEnumerable<OpenXmlElement>)

Appends each element from a list of elements to the end of the current element's list of child elements.

(Inherited from OpenXmlElement)
Append(OpenXmlElement[])

Appends each element from an array of elements to the end of the current element's list of child elements.

(Inherited from OpenXmlElement)
AppendChild<T>(T)

Appends the specified element to the end of the current element's list of child nodes.

(Inherited from OpenXmlElement)
ClearAllAttributes()

Clears all of the attributes, including both known attributes and extended attributes.

(Inherited from OpenXmlElement)
Clone()

Creates a duplicate of the current node.

(Inherited from OpenXmlElement)
CloneNode(Boolean)

Creates a duplicate of this node.

Descendants()

Enumerates all of the current element's descendants.

(Inherited from OpenXmlElement)
Descendants<T>()

Enumerate all of the current element's descendants of type T.

(Inherited from OpenXmlElement)
Elements()

Enumerates all of the current element's children.

(Inherited from OpenXmlElement)
Elements<T>()

Enumerates only the current element's children that have the specified type.

(Inherited from OpenXmlElement)
ElementsAfter()

Enumerates all of the sibling elements that follow the current element and have the same parent as the current element.

(Inherited from OpenXmlElement)
ElementsBefore()

Enumerates all of the sibling elements that precede the current element and have the same parent as the current element.

(Inherited from OpenXmlElement)
GetAttribute(String, String)

Gets an Open XML attribute with the specified tag name and namespace URI.

(Inherited from OpenXmlElement)
GetAttributes()

Gets a list that contains a copy of all the attributes.

(Inherited from OpenXmlElement)
GetEnumerator()

Returns an enumerator that iterates through the child collection.

(Inherited from OpenXmlElement)
GetFirstChild<T>()

Finds the first child element in type T.

(Inherited from OpenXmlElement)
InsertAfter<T>(T, OpenXmlElement)

Inserts the specified element immediately after the specified reference element.

(Inherited from OpenXmlElement)
InsertAfterSelf<T>(T)

Inserts the specified element immediately after the current element.

(Inherited from OpenXmlElement)
InsertAt<T>(T, Int32)

Inserts the specified element at the specified index in the current element's list of child elements.

(Inherited from OpenXmlElement)
InsertBefore<T>(T, OpenXmlElement)

Inserts the specified element immediately before the specified reference element.

(Inherited from OpenXmlElement)
InsertBeforeSelf<T>(T)

Inserts the specified element immediately before the current element.

(Inherited from OpenXmlElement)
IsAfter(OpenXmlElement)

Determines if the current element appears after a specified element in document order.

(Inherited from OpenXmlElement)
IsBefore(OpenXmlElement)

Determines if the current element appears before a specified element in document order.

(Inherited from OpenXmlElement)
LookupNamespace(String)

Resolves the namespace prefix in the context of the current node.

(Inherited from OpenXmlElement)
LookupPrefix(String)

Finds the corresponding prefix for a namespace uri in the current element scope.

(Inherited from OpenXmlElement)
NextSibling()

Gets the OpenXmlElement element that immediately follows the current OpenXmlElement element. Returns null (Nothing in Visual Basic) if there is no next OpenXmlElement element.

(Inherited from OpenXmlElement)
NextSibling<T>()

Gets the OpenXmlElement element with the specified type that follows the current OpenXmlElement element. Returns null (Nothing in Visual Basic) if there is no next OpenXmlElement.

(Inherited from OpenXmlElement)
PrependChild<T>(T)

Inserts the specified element at the beginning of the current element's list of child elements.

(Inherited from OpenXmlElement)
PreviousSibling()

Gets the OpenXmlElement element that immediately precedes the current OpenXmlElement element. Returns null (Nothing in Visual Basic ) if there is no preceding OpenXmlElement element.

(Inherited from OpenXmlElement)
PreviousSibling<T>()

Gets the OpenXmlElement element with the specified type that precedes the current OpenXmlElement. Returns null (Nothing in Visual Basic) if there is no preceding OpenXmlElement element.

(Inherited from OpenXmlElement)
Remove()

Removes the current element from its parent.

(Inherited from OpenXmlElement)
RemoveAllChildren()

Removes all child elements of the current element.

(Inherited from OpenXmlLeafElement)
RemoveAllChildren<T>()

Remove all of the current element's child elements that are of type T.

(Inherited from OpenXmlElement)
RemoveAnnotations(Type)

Removes the annotations of the specified type from the current OpenXmlElement element.

(Inherited from OpenXmlElement)
RemoveAnnotations<T>()

Removes the annotations with the specified type from the current OpenXmlElement element.

(Inherited from OpenXmlElement)
RemoveAttribute(String, String)

Removes the attribute from the current element.

(Inherited from OpenXmlElement)
RemoveChild<T>(T)

Removes the specified child element from the current element's list of child elements.

(Inherited from OpenXmlElement)
RemoveNamespaceDeclaration(String)

Removes the namespace declaration for the specified prefix. Removes nothing if there is no prefix.

(Inherited from OpenXmlElement)
ReplaceChild<T>(OpenXmlElement, T)

Replaces a child element with another child element in the current element's list of child elements.

(Inherited from OpenXmlElement)
SetAttribute(OpenXmlAttribute)

Sets an attribute to the specified element. If the attribute is a known attribute, the value of the attribute is set. If the attribute is an extended attribute, the 'openxmlAttribute' is added to the extended attributes list.

(Inherited from OpenXmlElement)
SetAttributes(IEnumerable<OpenXmlAttribute>)

Sets a number of attributes to the element. If an attribute is a known attribute, the value of the attribute is set. If an attribute is an extended attribute, the 'openxmlAttribute' is added to the extended attributes list.

(Inherited from OpenXmlElement)
WriteTo(XmlWriter)

Saves the current node to the specified XmlWriter.

(Inherited from OpenXmlElement)

Explicit Interface Implementations

IEnumerable.GetEnumerator() (Inherited from OpenXmlElement)
IEnumerable<OpenXmlElement>.GetEnumerator()

Returns an enumerator that iterates through the child collection.

(Inherited from OpenXmlElement)

Applies to