It seems like you're encountering an issue with SheetJS (also known as XLSX) where strings that consist entirely of numbers (e.g., "12345678901234567890") are being exported to Excel and automatically formatted as scientific notation (e.g., "1.23457E+19"). This happens because Excel tends to interpret large numeric strings as numbers and applies its default formatting.
The good news is that this can be resolved by explicitly telling SheetJS to treat these cells as text. In SheetJS, you can control cell formatting by setting the cell's type and number format. Specifically:
Set the cell type to 's' (string) to ensure it's treated as text.
Set the number format to '@' (text format) to override Excel's automatic formatting.
Below, I'll walk you through the solution step by step, including code examples.
Step 1: Understand the Problem
When you export data using SheetJS, strings are generally exported as strings (type 's').
However, if the string looks like a large number, Excel may still auto-format it as scientific notation upon opening the file.
To prevent this, we need to force the cell to be treated as text by adding a number format (z: '@' in the cell object).
Step 2: Modify Your Code to Handle Numeric Strings
You'll need to process your data before or after creating the worksheet. Here's how:
If you're using XLSX.utils.json_to_sheet or XLSX.utils.aoa_to_sheet:
These functions will initially set strings as type 's', but we need to loop through the cells and apply the text format to numeric strings.
Identify numeric strings:
A numeric string is one that consists only of digits (e.g., "12345"). You can use a regular expression like /^\d+$/ to detect this.
Apply the fix:
For cells matching this pattern, add z: '@' to the cell object. This sets the Excel number format to text.
Here's a complete example in JavaScript:
javascript
const XLSX = require('xlsx'); // Or import XLSX from 'xlsx' if using ES modules
// Sample data with a numeric string
const data = [
{ id: '12345678901234567890', name: 'John Doe' }, // This is the problematic string
{ id: '123', name: 'Jane Smith' },
];
// Step 1: Convert data to a worksheet
const worksheet = XLSX.utils.json_to_sheet(data);
// Step 2: Loop through the cells and fix numeric strings
for (let cellRef in worksheet) {
const cell = worksheet[cellRef];
if (cell && cell.t === 's' && /^\d+$/.test(cell.v)) {
// cell.t === 's' means it's a string
// /^\d+$/.test(cell.v) checks if the string consists only of digits
cell.z = '@'; // Set number format to text
}
}
// Step 3: Create a workbook and add the worksheet
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
// Step 4: Export the workbook to a file
XLSX.writeFile(workbook, 'output.xlsx');
Explanation of the Code
Line by Line:
XLSX.utils.json_to_sheet(data): Converts your array of objects into a worksheet. Strings in your data will initially be set as type 's'.
The for...in loop iterates over each cell in the worksheet.
if (cell && cell.t === 's' && /^\d+$/.test(cell.v)): This checks if the cell exists, is a string, and contains only digits.
cell.z = '@': Sets the number format to '@', which forces Excel to treat the cell as text.
XLSX.utils.book_new() and XLSX.utils.book_append_sheet(): Creates a new workbook and adds the worksheet.
XLSX.writeFile(workbook, 'output.xlsx'): Writes the workbook to an Excel file.
Why this works:
By setting z: '@', you're explicitly telling Excel to format the cell as text, which prevents scientific notation.
This approach only modifies cells that are pure numeric strings, leaving other strings (e.g., "John Doe") unchanged.
Step 3: Test the Output
After running the code, open the generated output.xlsx file in Excel.
The cell with the large numeric string (e.g., "12345678901234567890") should now display as plain text without scientific notation.
If you're still seeing issues, ensure you're using a recent version of SheetJS (e.g., via npm: npm install xlsx@latest).
Alternative Approaches
If the above doesn't work for your specific setup, here are a couple of other options:
Prepend a Single Quote ('Hack'):
In Excel, prefixing a number with a single quote (e.g., "'12345678901234567890") forces it to be treated as text.
You can modify your data before creating the worksheet:
This is simpler but alters the data slightly (the single quote will be visible in the cell if you edit it in Excel).
Use Custom Cell Objects:
If you're building the worksheet manually (e.g., not using json_to_sheet), you can directly set the cell properties:
javascript
const ws = {};
ws['A1'] = { v: '12345678901234567890', t: 's', z: '@' }; // v: value, t: type (string), z: number format
ws['A2'] = { v: 'Hello', t: 's' }; // Normal string, no change needed
Common Pitfalls and Tips
Excel Versions: This issue is more common in older versions of Excel or when the file is opened in a viewer. Test with Microsoft Excel for the best results.
Performance: If your dataset is very large, the loop might add some overhead. In that case, process the data before creating the worksheet.
Debugging: Use XLSX.writeFile with a temporary file and inspect the output in Excel. You can also use tools like Excel's "Format Cells" option to verify the cell type.
Edge Cases: If your strings include decimals (e.g., "123.45"), the regex /^\d+$/ won't match, so they won't be affected.
If you provide more details about your code or the exact data you're working with, I can refine this solution further!