Converting spreadsheet data to plain text is a frequent need when you want to feed information into legacy systems or simple parsers. Aspose.Cells Cloud SDK for Node.js provides a powerful library that handles the heavy lifting on the server side. In this guide you will see a complete working example, a cURL workflow for the REST API, installation steps, configuration options, and best‑practice tips to make CSV to TXT in Node.JS painless and reliable.
Full Working Example for CSV to TXT in Node.JS
This example demonstrates how to call the Cells API and save the converted TXT file locally.
const fs = require('fs');
const path = require('path');
const { CellsApi, ApiClient } = require('@aspose/cells-cloud');
async function convertCsvToTxt() {
// Aspose Cloud authentication
const clientId = 'YOUR_CLIENT_ID';
const clientSecret = 'YOUR_CLIENT_SECRET';
const config = new ApiClient();
config.clientId = clientId;
config.clientSecret = clientSecret;
config.basePath = 'https://api.aspose.cloud';
const cellsApi = new CellsApi(config);
// File paths
const inputFile = path.resolve(__dirname, 'input.csv');
const outputFile = path.resolve(__dirname, 'output.txt');
// Prepare request
const fileStream = fs.createReadStream(inputFile);
const request = {
file: fileStream,
format: 'txt' // target format
};
try {
// Convert CSV to TXT
const response = await cellsApi.postConvertWorkbook(request);
// response.body is a Buffer containing the TXT data
fs.writeFileSync(outputFile, response.body);
console.log(`Conversion completed. TXT saved to ${outputFile}`);
} catch (err) {
console.error('Conversion failed:', err);
}
}
convertCsvToTxt();
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Breaking Down CSV to TXT in Node.JS
The code works in a few logical steps:
-
Initialize the API client -
new ApiClient()is configured with your credentials and the base URL.const config = new ApiClient(); config.clientId = clientId; config.clientSecret = clientSecret; config.basePath = 'https://api.aspose.cloud'; -
Create a CellsApi instance - this object exposes the conversion method.
const cellsApi = new CellsApi(config); -
Read the CSV file as a stream -
fs.createReadStreamprovides the binary data required by the API.const fileStream = fs.createReadStream(inputFile); -
Build the request object - the
formatproperty tells the service to output TXT.const request = { file: fileStream, format: 'txt' }; -
Call
postConvertWorkbook- the API returns a Buffer containing the converted text, which is then written to disk.const response = await cellsApi.postConvertWorkbook(request); fs.writeFileSync(outputFile, response.body);
Understanding each piece helps you customize the flow, such as adding error handling or processing multiple files.
Getting the Environment Ready
- Install the SDK with npm:
npm install asposecellscloud
-
Ensure you have Node.js 14 or later installed.
-
Download the latest package from the official release page if you prefer a manual install: Aspose.Cells Cloud SDK for Node.js Download.
-
Create a free Aspose Cloud account to obtain your client ID and client secret.
With these prerequisites satisfied, you can run the conversion script immediately.
Convert CSV to TXT Using cURL and the REST API
You can achieve the same result without writing any code by calling the Aspose.Cells Cloud REST endpoints directly.
- Obtain an access token - exchange your client credentials for a JWT.
curl -X POST "https://api.aspose.cloud/connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
- Upload the source CSV file to the storage.
curl -X PUT "https://api.aspose.cloud/v3.0/cells/storage/file/input.csv" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: text/csv" \
--data-binary @input.csv
- Request the conversion to TXT format.
curl -X POST "https://api.aspose.cloud/v3.0/cells/convert?format=txt" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: multipart/form-data" \
-F "file=@input.csv"
- Download the resulting TXT file.
curl -X GET "https://api.aspose.cloud/v3.0/cells/storage/file/output.txt" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o output.txt
For more details on each endpoint, see the Aspose.Cells API Reference.
Conversion Settings: Options and Parameters
The example uses only two parameters, but the API supports additional settings:
format- specifies the target file type. In our case it is set to'txt'. Changing this value lets you export to other plain‑text formats supported by the service.file- a stream representing the source CSV. You can replace the stream with a Buffer or a base64 string if that fits your architecture.
For a full list of supported parameters, refer to the API Reference page for postConvertWorkbook.
Practical Tips for CSV to TXT Export
- Validate input files before sending them to the API to avoid unexpected conversion errors.
- Reuse the API client across multiple conversions to reduce authentication overhead.
- Stream large CSV files instead of loading them entirely into memory;
fs.createReadStreamalready does this. - Handle the response Buffer with proper encoding (
utf8) if you need to manipulate the text after conversion. - Secure your credentials by storing
clientIdandclientSecretin environment variables or a secret manager.
Following these practices will keep your application efficient and secure.
Conclusion
Converting CSV to TXT in Node.JS becomes straightforward with the Aspose.Cells Cloud SDK for Node.js. The library abstracts the low‑level details, letting you focus on business logic while delivering reliable results. Remember to acquire a valid license for production use; you can purchase a subscription or obtain a temporary license from the temporary license page. With the code sample, cURL workflow, and best‑practice guidance in this article, you are ready to integrate CSV to TXT export into any Node.JS solution.
FAQs
How do I perform CSV to TXT in Node.JS using Aspose.Cells Cloud?
Use the Aspose.Cells Cloud SDK for Node.js to call the postConvertWorkbook method. Provide the CSV file stream and set the format to 'txt'. See the full code example above.
Can I convert multiple CSV files to TXT in a single request?
The API processes one file per request. Loop through your files in Node.JS and call the conversion method for each file.
What authentication is required for CSV to TXT export in Node.JS?
You need a client ID and client secret from your Aspose Cloud account. The SDK exchanges them for an access token automatically.
Where can I find more details about the conversion options?
Refer to the official documentation at Aspose.Cells Cloud SDK for Node.js Documentation and the API reference at Aspose.Cells API Reference.