Barcode Generation

How to Keep Leading Zeros in Excel and CSV Barcode Data

Preserve barcode identifiers as text in Excel, check what your CSV actually contains, and prevent leading zeros from disappearing on import.

Share
Table of contents

To keep leading zeros in barcode identifiers, treat the identifiers as text before Excel converts them to numbers. Import the original data as text, check the actual values, and inspect the exported CSV before importing it again. A number format can make zeros appear in a worksheet without proving that the identifier passed to another application contains those zeros. [1]

Start with the original identifier

An identifier such as 00123 is a sequence of characters, even though every character happens to be a digit. It is not necessarily a quantity you need to calculate. If your source says 00123, the value sent to the barcode application should also be 00123, not 123.

Keep a copy of the original file or obtain the values again from the authoritative source before editing. For new entries, format the destination column as Text before typing or pasting. Formatting an already-converted number as text does not recreate missing characters. Excel stores only 15 significant digits of numeric precision, so a long identifier converted to a number can lose digits as well as leading zeros. [1]

These examples are internal identifiers for illustration; they are not assigned retail product numbers. The number of digits and the symbology required for your own application depend on the receiving system.

Import a CSV column as text

If the CSV already contains the correct characters, use Excel’s text import workflow rather than assuming that opening the file will preserve them:

  1. In a supported desktop version of Excel, choose Data > From Text/CSV and select the original file.
  2. Open the transformation or query editor from the preview. Menu labels can vary by version.
  3. Select the identifier column and set its data type to Text. If an earlier automatically added type-conversion step has already changed the values, remove or replace that step so the text type is applied to the original characters, then check the preview again.
  4. Load the data and compare several complete identifiers against the source, including values with zeros and the longest values. [1]

Microsoft also documents automatic data-conversion settings in Excel for Microsoft 365 and Excel 2024, including Mac versions. Availability depends on the version; check the setting on the computer doing the import. It does not repair an already damaged source. [1]

Distinguish the display from the data

What you see What it means for a barcode workflow
Text 00123 The five characters can be compared with the source and passed on as an identifier.
Numeric 123 displayed with a five-digit custom format The worksheet can show 00123, but the underlying number is still 123. Verify what the next application receives.
123 after zeros were removed The missing characters cannot be established from this value alone. Return to the trusted source.

A custom number format is useful when the goal is only a consistent display in a workbook. It is not evidence that an external system received the same character sequence. [1]

Export and check the actual handoff

Save the working spreadsheet separately, then export the required CSV. Open a copy in a plain-text editor and inspect the identifier fields, including their leading zeros and full length. A CSV is a text file, and opening it again in Excel can trigger another conversion; reimport the original exported file with the identifier column set to Text. [1][2]

For example, compare the source, the worksheet value, the text in the exported CSV, and the value shown after a text import. If any stage differs, stop and trace where the change occurred. Check the exact field that the receiving barcode application uses; a correct-looking neighboring label does not establish what was encoded.

After creating a barcode with your chosen tool, scan it with a suitable reader and compare the decoded value character by character with the trusted source. For production labels, also follow the receiving organization’s requirements and test with its equipment. This is a recommended check, not a claim that a particular label or workflow has been tested here.

Can you restore missing zeros?

Return to an authoritative source whenever possible. If a documented business rule establishes an exact fixed width, a text formula such as =TEXT(A2,"00000") can produce a five-character representation of a suitable numeric value. Compare the result with the source before use; a guessed width can produce the wrong identifier. A formula cannot recover significant digits already lost to numeric precision. [1]

References

[1] Microsoft Support. Keeping leading zeros and large numbers. https://support.microsoft.com/en-us/excel/keeping-leading-zeros-and-large-numbers

[2] Microsoft Support. Import or export text (.txt or .csv) files. https://support.microsoft.com/en-us/excel/get-started/import-or-export-text-txt-or-csv-files