Zdieľať cez


How to: Programmatically open text files as workbooks

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can open a text file as a workbook. You must pass in the name of the text file you want to open. You can specify several optional parameters, such as which row number to start parsing on and the column format of the data in the file.

Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Excel. For more information, see Features available by Office application and project type.

Example

this.Application.Workbooks.OpenText(@"C:\Test.txt",
    missing, 3,
    Excel.XlTextParsingType.xlDelimited,
    Excel.XlTextQualifier.xlTextQualifierNone,
    missing, missing, missing, true, missing, missing, missing, 
    missing, missing, missing, missing, missing, missing);
Me.Application.Workbooks.OpenText("C:\Test.txt", _
    StartRow:=3, _
    DataType:=Excel.XlTextParsingType.xlDelimited, _
    TextQualifier:=Excel.XlTextQualifier.xlTextQualifierNone, _
    Comma:=True)

Compile the code

This example requires the following components:

  • A comma-delimited text file named Test.txt that contains at least three lines of text.

  • The text file Test.txt to be stored on drive C.

See also