Excel AppNote: Opening and Saving Text Files (ME0802)Last reviewed: February 2, 1998Article ID: Q102142 |
The information in this article applies to:
SUMMARYThe Application Note "Opening and Saving Text Files" (ME0802) discusses how Microsoft Excel for the Macintosh parses text and formats numbers and alphanumeric characters in text files. The disk included with this appnote includes the text reader add-in macro, which you can use to import text files. You can obtain this Application Note from the following sources: You can obtain this Application Note from the following sources:
THE TEXT OF ME0802
Microsoft(R) Product Support Services Application Note (Text File) ME0802: OPENING AND SAVING TEXT FILES Revision Date: 7/93 1 disk includedThe following information applies to Microsoft Excel, version 4.0.
--------------------------------------------------------------------- | Information provided in this document and any software that may | | accompany this document (collectively referred to as an Application| | Note) is provided "as is" without warranty of any kind, either | | expressed or implied, including but not limited to the implied | | warranties of merchantability and/or fitness for a particular | | purpose. The user assumes the entire risk as to the accuracy and | | the use of this Application Note. This Application Note may be | | copied and distributed subject to the following conditions: 1) All | | text must be copied without modification and all pages must be | | included; 2) If software is included, all files on the disk(s) must| | be copied without modification; 3) All components of this | | Application Note must be distributed together; and 4) This | | Application Note may not be distributed for profit. | | | | Copyright (C) 1993 Microsoft Corporation. All Rights Reserved. | | Microsoft is a registered trademark of Microsoft Corporation. | |Macintosh is a registered trademark of Apple Computer, Incorporated.| --------------------------------------------------------------------- OVERVIEW ========This Application Note discusses how Microsoft Excel parses text and formats numbers and alphanumeric characters in text files. The ME0802 disk included with this Application Note contains the Text Reader add- in macro, which you can use to import text files. This macro parses your text according to a delimiter you select and prevents Microsoft Excel from applying automatic number formatting to the entries in your file.
INTRODUCTION ============In Microsoft Excel version 4.0, you can open and save files in text file format. Your text file can consist of rows of text in a single column or rows and columns of text. When you open a text file in Micro soft Excel, the way the text is broken into columns depends on the character you choose to delimit your fields or columns of text. You can also choose no delimiter and parse your text after the file is opened. The most common column delimiters are commas, tabs, and spaces. When you open or save text files in Microsoft Excel, you may find that some of the text entries in your file do not appear as expected. These unexpected results occur due to the way Microsoft Excel parses text and applies formatting to numbers and alphanumeric characters.
OPENING AND PARSING TEXT FILES ============================== HOW TEXT IS PARSEDWhen you open a text file, Microsoft Excel checks the file for the column delimiter you selected in the Text File Options dialog box (from the File menu, choose Open and then choose the Text button). In this dialog box, the default Column Delimiter option is Tab; you can also select the Comma, Space, Semicolon, None, or Custom option. (When you select Custom, you can specify a custom delimiter.) Once you choose a column delimiter, Microsoft Excel places the text following each instance of the specified delimiter in a separate cell. For example, if your text is delimited by spaces and if you select the Space delimiter, Microsoft Excel places the text following each space in a separate cell. This process is called parsing and occurs automatically when you specify a delimiter for your text file. If you do not select the Space delimiter before you open this space- delimited file, or if you select the None (no delimiter) option, the text in your file will not be parsed; that is, each line of text will appear in a separate row in column A only. In this case, after you open the file in Microsoft Excel, you can break the text into separate columns using one of the parsing commands. The parsing commands, located on the Data menu, are:
HOW TEXT ENTRIES ARE FORMATTEDThe following example illustrates how Microsoft Excel treats different types of entries when you open a text file with a specified delimiter or when you parse a text file after it's been opened. If you have a text file containing the following data, where each entry is separated by a tab
09876 00000001 1/93 12-31-92 2E9 12345 67-890 01234-5678 alpha text stringand you select the Tab delimiter in the Text File Options dialog box, the entry following each tab is placed in a separate column and each line is placed on a separate row. Each text string is evaluated when it is placed in its respective cell. If the contents can be interpreted as a value, Microsoft Excel attempts to apply a built-in number format to that value. If there is no corresponding number format, the contents are interpreted as text and placed in the cell as is. When you open this file in Microsoft Excel, it resembles the following example:
| A | B | C | D | E --|---------|---------|------------|------------|------------- 1 | 9876 | 1 | Jan-93 | 12/31/92 | 2.00E+09 --|---------|---------|------------|------------|------------- 2 | 12345 | 67-890 | 01234-5678 |alpha |text stringNote that the appearance of some text entries changes. Entries that are interpreted as values are right-aligned and those that are interpreted as text are left-aligned--these are the alignment defaults for Microsoft Excel. The following table explains how each entry from the sample data is formatted in Microsoft Excel.
How entry is formatted Original entry In Microsoft Excel in Microsoft Excel 09876 and 00000001 9876 and 1 These entries are interpreted as values in the General number format. Because leading zeros in a number are insignificant, Microsoft Excel drops the zeros. 1/93 Jan-93 If there are any hyphens or slash marks separating values, Microsoft Excel examines the individual values to see if they can be construed as a day and/or a month and/or a year. If so, a date format is applied. Because the 1 can be interpreted as a month value and 93 is a valid year, this entry matches the mmm-yy built-in number format. 12-31-92 12/31/92 Because the individual values separated by the hyphens can be interpreted as a valid month, day, and year, Microsoft Excel applies the closest matching date format, m/d/yy. Note that when the format is applied, the hyphens are replaced with slash marks. 2E9 2.00E+09 This entry is interpreted to match the 0.00E+00 built-in number format. NOTE: If the item had been 2x9, where x is any letter other than e or E, the item would have remained a text string and would not have changed. 12345 12345 This entry matches the General built-in number format. The application of this format does not alter the value's appearance because there are no leading zeros. 67-890 and 67-890 and These entries are interpreted as 01234-5678 01234-5678 text because hyphens separate the numbers and because the numbers on either side of the hyphens cannot be interpreted as a month, day, or year. alpha and alpha and These entries are not changed text string text string because they are text. USING CUSTOM NUMBER FORMATS TO CHANGE TEXT ENTRIESYou can use custom number formats to return entries to their original appearance (that is, the way they looked before you opened the text file in Microsoft Excel). For example, to change 9876 to 09876, do the following:
IMPORTANT: If you apply custom number formats or make other formatting changes to font size, borders, shading, row height, or column width, you must save the file in the Normal file format to retain the formatting. (From the File menu, choose Save As and choose the Options button. Under File Format, select the Normal format and choose OK.)Some entries cannot be returned to their original appearance with a custom number format. For example, there is no custom number format that will restore 2.00E+09 to 2E9. To retain the format 2E9, you can either add a text character to the string using a word processor or other text editor, or you can use the Text Reader add-in macro to import the file. For information on how to use the Text Reader add-in macro, see "Importing Text Files with the Text Reader Add-in Macro" on page 5 of this Application Note. For more information about custom number formats, see pages 221-227 of "User's Guide 1" or see the "Creating or Deleting Custom Number Formats" topic under "Number Formats" in Microsoft Excel Help.
SAVING AND EXPORTING TEXT FILES ===============================In Microsoft Excel, you can save worksheets as text files in a comma- delimited (CSV) or tab-delimited (Text) text format. You can select either one of these options by choosing Save As from the File menu, choosing the Options button, and selecting the desired format from the File Format list. You can also export data from your worksheet to a text file that is space delimited using the Flat File add-in macro discussed on page 4 of this Application Note. After you load this add- in macro, the Export command will be available on the Data menu. For additional information on using the Flat File add-in macro, see pages 151-152 of "User's Guide 2".
HOW TEXT ENTRIES ARE FORMATTEDIn Microsoft Excel, when you save a file in CSV or Text file format, each entry in the file retains its number format. Note: In the following example, the value 2E9 in cell E1 must be entered as a text value (by preceding the entry with an apostrophe) to prevent it from being converted to the scientific notation format.For example, if you have a worksheet that resembles the following
| A | B | C | D | E--|---------|-------------|--------------|--------------|------------ 1 | 09876 | 00000001 | 1/93 | 12-31-92 | 2E9--|---------|-------------|--------- ---|--------------|------------ 2 | 12345 | 67-890 | 01234-5678 | alpha | text stringwhen you open the file in a text editor, the table will resemble one of the following examples, depending on how you saved the file in Microsoft Excel:
USING THE FLAT FILE ADD-IN MACRO TO EXPORT DATAWhen you export a text file, you can use the export feature of the Flat File add-in macro to retain not only the built-in and custom number formats of your selection, but the alignment of the cell contents as well. The resulting text file will contain evenly aligned columns. When you load the Flat File add-in macro (located in the Macro Library folder in the folder in which you installed Microsoft Excel), two commands are added to the Data menu:
If you export the sample data with the Retain Cell Formats option enabled, the result will be:
09876 00000001 1/93 12-31-92 2E9 12345 67-890 01234-5678 alpha text stringIf you want evenly aligned output, where number formats are retained and alignments are the same, do the following before you export your data:
09876 00000001 1/93 12-31-92 2E9 12345 67-890 01234-5678 alpha text stringIf you do not retain cell formats, the numbers will revert to the General number format. For example, dates will be converted to their appropriate serial numbers and all entries will be left- aligned-regardless of whether a cell contains text or numbers. For example, if you export this sample data when the Retain Formats option is not enabled, your data will resemble the following:
9876 1 33970 33969 2E9 12345 67-890 01234-5678 alpha text string Note: When cells are formatted using the Justify, Fill, or Center Across Selection alignment options, choosing the Export command will result in the error message ''Error writing to disk.''For more information about the Flat File add-in macro, see pages 151- 152 of "User's Guide 2".
IMPORTING TEXT FILES WITH THE TEXT READER ADD-IN MACRO ======================================================You can use the Text Reader add-in macro provided with this Application Note to import a delimited text file into Microsoft Excel version 4.0 without changing the format or appearance of any entries in the file. For example, 09876 will still appear as 09876, 12-31-92 will still appear as 12-31-92, and so on. When you import a file using the Text Reader add-in macro, the file itself is not opened. Instead, the data is imported and each entry is read from the source file and placed in a new worksheet. The Text Reader add-in macro reads in each text entry individually and adds an apostrophe to the beginning of the entry. For example, the Text Reader add-in macro turns 09876 into '09876. Microsoft Excel interprets any value that is preceded by an apostrophe as a text value. As a result, when the entry is placed in a cell, it is displayed as it appeared in the original text file. Once the data has been imported, you can save the new worksheet in any available file format.
Note: This method of importing a text file may take longer than opening the file by choosing the Open command from the File menu. To Use the Text Reader Add-in Macro with Microsoft Excel
To Import Your Text File
CONVERTING TEXT ENTRIES TO VALUESWhen you use the Text Reader add-in macro, each imported entry is formatted in text format and placed in a cell. If some of the columns contain entries that you want to use in calculations, you must convert these text entries to values. If you attempt to perform calculations on these cells without first converting them to values, you will receive a 0 (zero) or a #VALUE! error value. For example, if you import the following text
| A | B | C | D --|------------|-----------|-------------|----------- 1 | Product No.| Month-Year| Units Sold | Revenue --|------------|-----------|-------------|----------- 2 | 2e190 | 1-93 | 1000 | 5000 --|------------|-----------|-------------|----------- 3 | 2e190 | 2-93 | 2000 | 10000 --|------------|-----------|-------------|----------- 4 | 2e190 | 3-93 | 3000 | 15000and want to calculate totals for the entries in the Units Sold and Revenue columns, do the following to convert these entries to values:
CONVERTING FRACTIONS TO VALUESThe exception to using the method described in the previous section is when you have fractions that could be interpreted as dates. For example, if you import the following fractions
| A ---|---- 1 | 1/4 ---|---- 2 | 1/3 ---|---- 3 | 1/2 ---|---- 4 | 3/4you need to format a blank range of cells with a Fraction number format and then use a formula to correctly convert them. If you use the procedure outlined in the previous section (choosing the Paste Special command and selecting the Multiply option), these fractions would be converted to dates. To convert these text-formatted fractions to values:
TO OBTAIN THIS APPLICATION NOTEThe following file is available for download from the Microsoft Software Library:
~ ME0802.HQX (size: 53612 bytes)For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q119591 TITLE : How to Obtain Microsoft Support Files from Online ServicesYou can have this Application Note mailed to you from the automated Microsoft FastTips Technical Library, which you can call 24 hours a day, 7 days a week at (800) 936-4100. NOTE: The FastTips Technical Library is available only to customers within the U.S. and Canada. If you are unable to access the source(s) listed above, you can have this Application Note mailed or faxed to you by calling Microsoft Product Support Services Monday through Friday, 6:00 A.M. to 6:00 P.M. Pacific time at (425) 635-7080. If you are outside the United States, contact the Microsoft subsidiary for your area To locate your subsidiary, see the Microsoft World Wide Offices Web site at:
http://www.microsoft.com/worldwide/default.htm |
Additional query words: 4.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |