CSV-ის CSV მონაცემების თანამედროვე XLSX სამუშაო წიგნაკში გადაყვანა ხშირად საჭიროა Node.JS აპლიკაციებისთვის, რომლებიც უნდა შექმნან Excel ანგარიშები, არ იყენებენ Microsoft Excel-ს. Aspose.HTML Cloud SDK for Node.JS სთავაზობს ძლიერი API-ს, რომელიც სრულად ღრუბელში აკეთებს გადაყვანას. ამ გიდში ნახავთ სრულ კოდის მაგალითს, გაიგებთ, როგორ დავიძახოთ REST API cURL-ით, და გაიგებთ, რა ნაბიჯებია საჭირო CSV-ის Excel XLSX ფორმატში გადაყვანისთვის Node.JS-ში ეფექტურად.
სრული კოდის მაგალითი: CSV-დან Excel XLSX გარდაქმნა Node.JS-ში
ეს მაგალითი აჩვენებს, როგორ გადაიტანოთ CSV ფაილი XLSX სამუშაო წიგნაკში, Aspose.HTML Cloud SDK for Node.JS-ის გამოყენებით.
const fs = require('fs');
const path = require('path');
const {
HtmlApi,
Configuration,
UploadFileRequest,
DownloadFileRequest,
ConvertDocumentRequest,
DeleteFileRequest
} = require('@asposecloud/aspose-html-cloud');
// Replace with your actual Aspose Cloud credentials
const config = new Configuration({
clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET'
});
const htmlApi = new HtmlApi(config);
async function convertCsvToXlsx() {
const localCsvPath = path.resolve(__dirname, 'input.csv');
const localXlsxPath = path.resolve(__dirname, 'output.xlsx');
const remoteCsvPath = 'input.csv';
const remoteXlsxPath = 'output.xlsx';
try {
// Upload CSV using a read stream (efficient for large files)
const uploadStream = fs.createReadStream(localCsvPath);
await htmlApi.uploadFile(new UploadFileRequest({
path: remoteCsvPath,
file: uploadStream
}));
// Convert CSV to XLSX
await htmlApi.convertDocument(new ConvertDocumentRequest({
inputPath: remoteCsvPath,
outputPath: remoteXlsxPath,
format: 'xlsx'
}));
// Download the resulting XLSX file
const downloadResponse = await htmlApi.downloadFile(new DownloadFileRequest({
path: remoteXlsxPath
}));
const writeStream = fs.createWriteStream(localXlsxPath);
await new Promise((resolve, reject) => {
downloadResponse.body.pipe(writeStream);
downloadResponse.body.on('end', resolve);
downloadResponse.body.on('error', reject);
});
console.log('Conversion completed successfully.');
} catch (error) {
console.error('Error during conversion:', error);
} finally {
// Cleanup remote files
try {
await htmlApi.deleteFile(new DeleteFileRequest({ path: remoteCsvPath }));
await htmlApi.deleteFile(new DeleteFileRequest({ path: remoteXlsxPath }));
} catch (cleanupError) {
// Ignore cleanup errors
}
}
}
convertCsvToXlsx();
შენიშვნა: ეს კოდის მაგალითი აჩვენებს ძირითად ფუნქციას. თქვენს პროექტში მისი გამოყენებამდე დარწმუნდით, რომ განაახლეთ ფაილთა ბილიკები (
input.csv,output.xlsx, ა.შ.) თქვენი რეალური ფაილების მდებარეობებთან, გადამოწმეთ, რომ ყველა საჭირო დამოკიდებულება სწორად დაყენებულია, და სრულად ტესტირეთ თქვენი განვითარების გარემოში. თუ რაიმე პრობლემის kanssa შეხვდეთ, გთხოვთ მიმართოთ ოფიციალურ დოკუმენტაციას ან დაუკავშირდეთ მხარდაჭერის გუნდს დახმარებისთვის.
CSV-დან XLSX-ში კონვერტირება cURL-ით და REST API-ით
თუ თქვენ უპირატესობას იძლევათ პირდაპირ REST მიდგომას, იგივე კონვერსია შეიძლება შესრულდეს cURL ბრძანებების საშუალებით. ქვემოთ მოცემული ნაბიჯები აჩვენებს, როგორ მიიღოთ წვდომის ტოკენი, ატვირთოთ CSV, გააქტიუროთ კონვერსია და ჩამოტვირთოთ XLSX ფაილი.
# 1. Get an access token
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"
# 2. Upload the source CSV file
curl -X PUT "https://api.aspose.cloud/v4.0/html/storage/file/input.csv" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: text/csv" \
--data-binary @input.csv
# 3. Convert CSV to XLSX
curl -X POST "https://api.aspose.cloud/v4.0/html/convert?format=xlsx&outPath=output.xlsx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"inputPath":"input.csv"}'
# 4. Download the generated XLSX file
curl -X GET "https://api.aspose.cloud/v4.0/html/storage/file/output.xlsx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o output.xlsx
<!--[CODE_SNIPPET_END]-->
For more details on request parameters and additional options, see the [official API documentation](https://reference.aspose.cloud/html/).
## Breaking Down CSV to Excel Conversion in Node.JS
Understanding how the code achieves CSV to Excel XLSX conversion in Node.JS helps you customize the process for your own projects.
1. **Configuration Setup** – The `Configuration` class stores your `clientId` and `clientSecret`.
<!--[CODE_SNIPPET_START]-->
```javascript
const [config](https://docs.fileformat.com/programming/config/) = new Configuration({
clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET'
});
-
API Initialization –
HtmlApiis instantiated with the configuration to access all conversion endpoints.const htmlApi = new HtmlApi(config); -
Uploading the CSV – A read stream (
fs.createReadStream) uploads the file efficiently, which is crucial for large CSV files.const uploadStream = fs.createReadStream(localCsvPath); await htmlApi.uploadFile(new UploadFileRequest({ path: remoteCsvPath, file: uploadStream })); -
Executing the Conversion –
convertDocumentis called withformat: 'xlsx'to perform the CSV to Excel XLSX conversion.await htmlApi.convertDocument(new ConvertDocumentRequest({ inputPath: remoteCsvPath, outputPath: remoteXlsxPath, format: 'xlsx' })); -
Downloading the Result – The SDK streams the generated XLSX back to the local file system.
const downloadResponse = await htmlApi.downloadFile(new DownloadFileRequest({ path: remoteXlsxPath })); const writeStream = fs.createWriteStream(localXlsxPath); await new Promise((resolve, reject) => { downloadResponse.body.pipe(writeStream); downloadResponse.body.on('end', resolve); downloadResponse.body.on('error', reject); });
These steps illustrate the end‑to‑end flow of CSV to Excel XLSX conversion in Node.JS using the Aspose.HTML Cloud SDK.
Prerequisites and Setup - Installing Aspose.HTML Cloud SDK for Node.JS
-
Node.js Runtime – Ensure you have Node.js 14 or higher installed.
-
Install the SDK – Run the following npm command (download URL: https://releases.aspose.cloud/html/nodejs/).
npm install @asposecloud/aspose-html-cloud --save -
Configure Credentials - Create a
.asposecloudconfiguration file or set environment variables with yourclientIdandclientSecretobtained from the Aspose Cloud dashboard. -
Verify Installation - Execute
node -e "require('@asposecloud/aspose-html-cloud')"to confirm the package loads without errors.
With the SDK installed and credentials configured, you are ready to run the conversion code.
Conclusion
This guide walked you through CSV to Excel XLSX conversion in Node.JS using Aspose.HTML Cloud SDK for Node.JS. You saw a complete working example, learned how to perform the same task with cURL, and set up the SDK on your development machine. The library eliminates the need for Microsoft Excel on the server, making large‑scale report generation lightweight and reliable. For production use, acquire a commercial license or use a temporary license from the temporary license page to stay compliant.
FAQs
-
What file formats are supported for conversion besides CSV and XLSX?
Aspose.HTML supports a wide range of formats including HTML, PDF, DOCX, and PPTX. Refer to the product documentation for the full list. -
How can I convert multiple CSV files in a single run?
Loop through your file list, calling the upload, convert, and download steps for each file. The SDK’s streaming approach works well for batch processing. -
Is it possible to customize the Excel output (e.g., column widths, styles)?
The basic conversion creates a standard workbook. For advanced styling, you can post‑process the XLSX using Aspose.Cells Cloud after conversion. -
Where can I find pricing details for the Aspose.HTML Cloud SDK?
Pricing information is available on the Aspose website. You can also start with a temporary license for evaluation before purchasing a full subscription.