How to Display Optional Thousands Separators with MSO Number Format
Question details
The user wants to format numbers in an HTML-generated Excel file so that thousands separators are shown, but decimals only appear when they actually exist (e.g., displaying 1,234.56 and 300).

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Generating or exporting an Excel file (.xls) using HTML markup and CSS, and needing precise control over how numbers and decimals are displayed.
- Observed behavior
- When specifying the number format in the text representation of the Excel file using the mso-number-format field, the formatting fails to generate a comma or leaves an unwanted decimal point on whole numbers unless properly escaped or conditionally formatted.
Ensure you have access to the raw HTML or text code generating your Excel file, or have your workbook open if you plan to apply native conditional formatting directly within the spreadsheet software.
Escape Commas in the HTML MSO Number Format Attribute
Use escape characters in your HTML markup's CSS to ensure the spreadsheet application correctly interprets the thousands separator.
When Excel parses an HTML-generated .xls file, standard commas in the mso-number-format CSS attribute can sometimes be ignored or misinterpreted. To resolve this, you must escape the comma using a backslash.
A format such as #\,##0.## tells the application to display thousands grouped by commas and up to two decimal places, dropping the decimals if the number is a whole integer.
Open the script or plain text file that generates your HTML-based Excel (.xls) file.
Find the CSS class or inline style applied to the cells containing the numbers you wish to format.
Add or modify the mso-number-format property to include the escaped comma, for example: style='mso-number-format:"#\,##0.##"'.
Save the file with an .xls extension, open it in your spreadsheet application, and verify that both the thousands separator and optional decimals display correctly.

Use Native Conditional Formatting
If you are working directly in a spreadsheet interface instead of HTML code, use conditional formatting to hide decimals for whole numbers.
Easily Manage Number Formats and Compatibility with WPS Office
WPS Office provides robust compatibility with HTML-generated Excel files (.xls) and fully supports advanced CSS styles like mso-number-format. Whether you are generating files programmatically or relying on native conditional formatting, WPS handles your data seamlessly.
- 1. Open your file in WPS Spreadsheet: Launch WPS Office and open your HTML-generated .xls file directly to verify formatting.
- 2. Access cell formatting: Select your numerical data, right-click, and choose 'Format Cells' or press Ctrl+1.
- 3. Apply Custom formats: Navigate to the 'Custom' category and input #,##0.## to easily apply native thousands separators with optional decimals.
- 4. Manage conditional rules: Go to the Home tab and click 'Conditional Formatting' to apply dynamic integer and decimal rules without writing code.

Frequently Asked Questions
Why does my mso-number-format ignore the comma in HTML exports?
In HTML-based Excel files, standard commas in the CSS mso-number-format property can be misinterpreted by the importing engine. You must escape the comma with a backslash (e.g., \,) to ensure the thousands separator is applied correctly.
Can I make decimals optional using standard Excel custom formatting?
You can use the hash symbol (#) in custom formatting (like #,##0.##) to make trailing decimals optional. However, this may still leave a trailing decimal point for whole numbers. Using conditional formatting is the most reliable way to hide the decimal point entirely for integers.
What exactly is an HTML-generated Excel file?
It is a plain text file containing standard HTML web tables and CSS styling (including Microsoft-specific styles like mso-number-format) that is saved with an .xls extension. Spreadsheet applications recognize the extension and parse the markup into a tabular grid.




