In daily office work, data often needs to be exchanged between Excel spreadsheets and plain text (TXT) files. Spire.XLS for JavaScript runs entirely in the browser via WebAssembly, using a virtual file system (VFS) to manage input and output files — no backend server is required. It provides flexible APIs for controlling conversion parameters such as delimiters and encoding formats.
With Spire.XLS for JavaScript, you can export Excel worksheet data as structured text files, or import delimited text files to create fully formatted Excel workbooks. This makes data migration between different applications more convenient and efficient.
This article covers two core features:
For installation and project setup, refer to Integrating Spire.XLS for JavaScript in a React Project. The examples below assume Spire.XLS is installed and the WebAssembly module is initialized.
Convert Excel Worksheet Data to TXT
Exporting Excel data as a plain text file makes it convenient for further processing or analysis in other applications. With Spire.XLS for JavaScript, you can save the contents of a specified worksheet as a TXT file, with flexible control over the field separator and character encoding. The steps are as follows:
- Create a
Workbookobject and load an existing Excel file. - Retrieve a specific worksheet via
workbook.Worksheets.get(index). - Call the worksheet's
SaveToFile()method, specifying the output filename, separator, and encoding. - Dispose of the workbook resources, read the result file from VFS, and trigger the download.
Below is a complete code example demonstrating how to convert Excel to TXT in React:
function App() {
const convertToText = async () => {
// Get the Spire.XLS WASM module
const xlsModule = window.wasmModule?.spirexls;
// Check if the module is ready
if (!xlsModule) {
alert('Spire.Xls is not ready yet');
return;
}
// Load the sample file into VFS
await window.spire.FetchFileToVFS('Sample.xlsx', '', `${process.env.PUBLIC_URL}data/`);
// Create a workbook object and load the Excel file
const workbook = new xlsModule.Workbook();
workbook.LoadFromFile({ fileName: 'Sample.xlsx' });
// Get the first worksheet
let sheet = workbook.Worksheets.get(0);
// Save the worksheet as a TXT file with space separator and UTF-8 encoding
const outputFileName = 'ExcelToTxt.txt';
sheet.SaveToFile({
fileName: outputFileName,
separator: " ",
encoding: xlsModule.Encoding.get_UTF8()
});
workbook.Dispose();
// Read the file from VFS and trigger download
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>Convert Excel to TXT</h1>
<button onClick={convertToText}>
Generate
</button>
</div>
);
}
export default App;
Excel converted to TXT with Spire.XLS for JavaScript

Convert TXT File to Excel Workbook
Importing a delimited text file into an Excel spreadsheet allows you to take full advantage of Excel's formatting, calculation, and charting capabilities. Spire.XLS for JavaScript supports TXT-to-Excel conversion by reading the text file content and writing data to the workbook cell by cell. Formatting such as bold headers can also be applied during the process. The steps are as follows:
- Load the font file and TXT sample file into the VFS.
- Read the TXT file content from VFS, split it by lines, and parse the cell data for each line.
- Create a
Workbookobject, iterate through the data array, and write data to worksheet cells row by row and column by column. - Apply bold styling to the header row and call
AllocatedRange.AutoFitColumns()to auto-fit column widths. - Save the workbook as an Excel file and trigger the download.
Below is a complete code example demonstrating how to convert TXT to Excel in React:
function App() {
const convertToExcel = async () => {
// Get the Spire.XLS WASM module
const xlsModule = window.wasmModule?.spirexls;
// Check if the module is ready
if (!xlsModule) {
alert('Spire.Xls is not ready yet');
return;
}
// Load the font file to ensure proper text rendering
await window.spire.FetchFileToVFS('ARIAL.TTF', '', `${process.env.PUBLIC_URL}font/`);
// Load the TXT file into VFS
await window.spire.FetchFileToVFS('Sample.txt', '', `${process.env.PUBLIC_URL}data/`);
// Read the text file content from VFS
const txtData = window.dotnetRuntime.Module.FS.readFile('Sample.txt');
const text = typeof txtData === 'string' ? txtData : new TextDecoder('utf-8').decode(txtData);
// Split by lines, compatible with \r\n and \n
const lines = text.trim().split(/\r?\n/);
// Parse each line of data (try tab delimiter first, then fallback to other delimiters)
const data = [];
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed) continue;
let cells = trimmed.split('\t');
if (cells.length === 1) {
cells = trimmed.split(/\s+/);
}
data.push(cells);
}
// Create a Workbook object
const workbook = new xlsModule.Workbook();
// Get the first worksheet
const sheet = workbook.Worksheets.get(0);
// Iterate through rows and columns in the data array and write to cells
for (let row = 0; row < data.length; row++) {
for (let col = 0; col < data[row].length; col++) {
const cell = sheet.get_Item(row + 1, col + 1);
cell.Value = data[row][col];
// Bold the header row
if (row === 0) {
cell.Style.Font.IsBold = true;
}
}
}
// Auto-fit column widths
sheet.AllocatedRange.AutoFitColumns();
// Save the workbook and release resources
const outputFileName = 'TxtToExcel.xlsx';
workbook.SaveToFile({ fileName: outputFileName, version: xlsModule.ExcelVersion.Version2010 });
workbook.Dispose();
// Read the file from VFS and trigger download
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>Convert TXT to Excel</h1>
<button onClick={convertToExcel}>
Generate
</button>
</div>
);
}
export default App;
TXT converted to Excel with Spire.XLS for JavaScript

FAQ
How to handle encoding issues during conversion?
Cause: The TXT file in this example uses UTF-8 encoding. If the TXT file uses a different encoding format (such as GBK, GB2312, etc.), decoding it directly with TextDecoder('utf-8') will result in garbled text.
Solution: Specify the corresponding encoding type in the TextDecoder constructor parameter based on the actual encoding of the TXT file:
// UTF-8 encoding
const text = new TextDecoder('utf-8').decode(txtData);
// GBK encoding
const text = new TextDecoder('gbk').decode(txtData);
// GB2312 encoding
const text = new TextDecoder('gb2312').decode(txtData);
// UTF-16 encoding
const text = new TextDecoder('utf-16').decode(txtData);
How to handle TXT files with different delimiters?
Cause: TXT files may use different delimiters such as tabs (\t), spaces, commas (,), semicolons (;), etc. Choosing the wrong delimiter can lead to data parsing errors.
Solution: In JavaScript, you can specify different delimiters by modifying the parameter of the split() method:
// Tab delimiter
let cells = trimmed.split('\t');
// Comma delimiter
let cells = trimmed.split(',');
// Semicolon delimiter
let cells = trimmed.split(';');
// Regular expression: split by one or more whitespace characters (spaces, tabs, etc.)
let cells = trimmed.split(/\s+/);
Get a Free License
Spire.XLS for JavaScript offers a 30-day full-featured free trial license with no functional limitations. Apply here to evaluate before purchasing.
