Skip to main content

CSV files in Excel

How to keep leading zeros when opening a CSV in Excel

A customer export contains ID 00123, but Excel shows 123. Before you save, check the original CSV. If the zeros are still in the file, you can bring the exact ID into Excel without retyping it.

Published:

Updated:

Related tool: Inspect a CSV

Does the original file still say 00123?

Open the untouched export in a text editor or the local CSV viewer. In the sample below, 00123 and 123 belong to different customers. Excel can interpret both as the number 123 when you double-click the file. If the viewer shows both IDs correctly, close the Excel copy without saving and import the original.

Two customer IDs that look identical after Excel turns them into numbers
customer_id,quantity
00123,2
123,5

Import the ID as text and keep the quantity numeric

  1. Start with a blank workbook in desktop Excel. On the Data tab, choose From Text/CSV and select the original export.
  2. In the preview, choose Transform Data to open Power Query. Select the customer_id column and change its data type to Text.
  3. If Power Query offers to replace the current type conversion, choose Replace Current. If the preview still shows 123 twice, remove the automatic Changed Type step and set customer_id to Text directly from the original values.
  4. Set quantity to Whole Number. Once the preview shows 00123 next to 2 and 123 next to 5, choose Close & Load.

What if the zeros are already gone from the CSV?

If your customer IDs can have different lengths, there is no way to tell from 123 alone whether it started as 123, 0123 or 00123. Get a fresh export from the customer system or use a backup from before the file was overwritten.

You can rebuild a code when its official length is known and the remaining digits are intact. For example, if every branch code must have five digits, =TEXT(A2,"00000") turns 123 into the text 00123. That rule would merge the two customer IDs in the sample, so use it only on a column with a documented width.

Long identifiers need an extra check. Excel stores numbers to 15 significant digits. If a 16-digit identifier was rounded, adding zeros at the front cannot recover the missing digits at the end. Retrieve the original and import it as text.

Check the file you will actually send

Save your working copy as an .xlsx workbook to keep the column types. If the recipient needs CSV, export it under a new name and inspect that file in the viewer. Look for both 00123 and 123 in their original rows. When the recipient opens the CSV in Excel, they should import the ID column as text too.

More questions about leading zeros

Will enclosing 00123 in double quotes protect it?

CSV quotes mark the boundaries of a field. Excel can still interpret the characters inside as a number. Set the column to Text during import.

How do I enter a new code with a leading zero?

Format the blank cells as Text before typing or pasting the codes. For one new entry, typing an apostrophe before the digits also tells Excel to keep the entry as text.