共用方式為


How to: Programmatically Open Text Files as Workbooks

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 application-level projects for Excel 2013 and Excel 2010. For more information, see Features Available by Office Application and Project Type.

Example

Me.Application.Workbooks.OpenText("C:\Test.txt", _
    StartRow:=3, _
    DataType:=Excel.XlTextParsingType.xlDelimited, _
    TextQualifier:=Excel.XlTextQualifier.xlTextQualifierNone, _
    Comma:=True)
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);

Compiling 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

Tasks

How to: Programmatically Open Workbooks

How to: Programmatically Create New Workbooks

How to: Programmatically Save Workbooks

How to: Programmatically Close Workbooks

Concepts

Working with Workbooks

Optional Parameters in Office Solutions