Compartir a través de


Workbooks.OpenText Method

Loads and parses a text file as a new workbook with a single sheet that contains the parsed text-file data.

Namespace:  Microsoft.Office.Interop.Excel
Assembly:  Microsoft.Office.Interop.Excel (in Microsoft.Office.Interop.Excel.dll)

Syntax

'Declaration
Sub OpenText ( _
    Filename As String, _
    Origin As Object, _
    StartRow As Object, _
    DataType As Object, _
    TextQualifier As XlTextQualifier, _
    ConsecutiveDelimiter As Object, _
    Tab As Object, _
    Semicolon As Object, _
    Comma As Object, _
    Space As Object, _
    Other As Object, _
    OtherChar As Object, _
    FieldInfo As Object, _
    TextVisualLayout As Object, _
    DecimalSeparator As Object, _
    ThousandsSeparator As Object, _
    TrailingMinusNumbers As Object, _
    Local As Object _
)
'Usage
Dim instance As Workbooks
Dim Filename As String
Dim Origin As Object
Dim StartRow As Object
Dim DataType As Object
Dim TextQualifier As XlTextQualifier
Dim ConsecutiveDelimiter As Object
Dim Tab As Object
Dim Semicolon As Object
Dim Comma As Object
Dim Space As Object
Dim Other As Object
Dim OtherChar As Object
Dim FieldInfo As Object
Dim TextVisualLayout As Object
Dim DecimalSeparator As Object
Dim ThousandsSeparator As Object
Dim TrailingMinusNumbers As Object
Dim Local As Object

instance.OpenText(Filename, Origin, StartRow, _
    DataType, TextQualifier, ConsecutiveDelimiter, _
    Tab, Semicolon, Comma, Space, Other, _
    OtherChar, FieldInfo, TextVisualLayout, _
    DecimalSeparator, ThousandsSeparator, _
    TrailingMinusNumbers, Local)
void OpenText(
    string Filename,
    Object Origin,
    Object StartRow,
    Object DataType,
    XlTextQualifier TextQualifier,
    Object ConsecutiveDelimiter,
    Object Tab,
    Object Semicolon,
    Object Comma,
    Object Space,
    Object Other,
    Object OtherChar,
    Object FieldInfo,
    Object TextVisualLayout,
    Object DecimalSeparator,
    Object ThousandsSeparator,
    Object TrailingMinusNumbers,
    Object Local
)

Parameters

  • Filename
    Type: System.String

    Required String. Specifies the file name of the text file to be opened and parsed.

  • Origin
    Type: System.Object

    Optional Object. Specifies the origin of the text file. Can be one of the following XlPlatform constants: xlMacintosh, xlWindows, or xlMSDOS. Additionally, this could be an integer representing the code page number of the desired code page. For example, "1256" would specify that the encoding of the source text file is Arabic (Windows). If this argument is omitted, the method uses the current setting of the File Origin option in the Text Import Wizard.

  • StartRow
    Type: System.Object

    Optional Object. The row number at which to start parsing text. The default value is 1.

  • DataType
    Type: System.Object

    Optional Object. Specifies the column format of the data in the file. Can be one of the following XlTextParsingType constants: xlDelimited or xlFixedWidth. If this argument is not specified, Microsoft Excel attempts to determine the column format when it opens the file.

  • ConsecutiveDelimiter
    Type: System.Object

    Optional Object. True to have consecutive delimiters considered one delimiter. The default is False.

  • Tab
    Type: System.Object

    Optional Object. True to have the tab character be the delimiter (DataType must be xlDelimited). The default value is False.

  • Semicolon
    Type: System.Object

    Optional Object. True to have the semicolon character be the delimiter (DataType must be xlDelimited). The default value is False.

  • Comma
    Type: System.Object

    Optional Object. True to have the comma character be the delimiter (DataType must be xlDelimited). The default value is False.

  • Space
    Type: System.Object

    Optional Object. True to have the space character be the delimiter (DataType must be xlDelimited). The default value is False.

  • Other
    Type: System.Object

    Optional Object. True to have the character specified by the OtherChar argument be the delimiter (DataType must be xlDelimited). The default value is False.

  • OtherChar
    Type: System.Object

    Optional Object (required if Other is True). Specifies the delimiter character when Other is True. If more than one character is specified, only the first character of the string is used; the remaining characters are ignored.

  • FieldInfo
    Type: System.Object

    Optional XlColumnDataType. An array containing parse information for individual columns of data. The interpretation depends on the value of DataType. When the data is delimited, this argument is an array of two-element arrays, with each two-element array specifying the conversion options for a particular column. The first element is the column number (1-based), and the second element is one of theXlColumnDataType constants specifying how the column is parsed.

    xlGeneralFormat General

    xlTextFormat Text

    xlMDYFormat MDY date

    xlDMYFormat DMY date

    xlYMDFormat YMD date

    xlMYDFormat MYD date

    xlDYMFormat DYM date

    xlYDMFormat YDM date

    xlEMDFormat EMD date

    xlSkipColumn Skip Column

    You can use xlEMDFormat only if you have installed and selected Taiwanese language support. The xlEMDFormat constant specifies that Taiwanese era dates are being used.

    The column specifiers can be in any order. If there's no column specifier for a particular column in the input data, the column is parsed with the General setting.

    Notes

    If you specify that a column is to be skipped, you must explicitly state the type for all remaining columns or the data will not parse correctly.

    If there is a recognizable date in the data, the cell will be formatted as a date in the worksheet even if the setting for the column is General. Additionally, if you specify one of the above date formats for a column and the data does not contain a recognized date, then the cell format in the worksheet will be General.

    If the source data has fixed-width columns, the first element in each two-element array specifies the position of the starting character in the column (as an integer; character 0 (zero) is the first character). The second element in the two-element array specifies the parse option for the column as a number between 0 and 9, as listed in the preceding table.

  • TextVisualLayout
    Type: System.Object

    Optional Object. The visual layout of the text.

  • DecimalSeparator
    Type: System.Object

    Optional Object. The decimal separator that Microsoft Excel uses when recognizing numbers. The default setting is the system setting.

  • ThousandsSeparator
    Type: System.Object

    Optional Object. The thousands separator that Excel uses when recognizing numbers. The default setting is the system setting.

    The following table shows the results of importing text into Excel for various import settings. Numeric results are displayed in the rightmost column.

See Also

Reference

Workbooks Interface

Workbooks Members

Microsoft.Office.Interop.Excel Namespace