قراءة ملف CSV بترميزات متعددة باستخدام جافا سكريبت عبر C++
Contents
[
Hide
]
أحيانًا، يحتوي ملف CSV الخاص بك على ترميزات متعددة (Unicode، ANSI، UTF8، UTF7، وغيرها). تتيح لك Aspose.Cells تحميل مثل هذه الملفات وتحويلها إلى تنسيقات أخرى، مثل PDF أو XLSX.
توفر Aspose.Cells الخاصية TxtLoadOptions.isMultiEncoded()، والتي تحتاج إلى ضبطها على true لتحميل ملف CSV الخاص بك مع ترميزات متعددة بشكل صحيح.
يوضح اللقطة الشاشية التالية ملف CSV عينة يحتوي على سطرين. السطر الأول بترميز ANSI والسطر الثاني بترميز Unicode.
| ملف الإدخال |
|---|
![]() |
تظهر الصورة التالية ملف XLSX الذي تم تحويله من ملف CSV أعلاه دون ضبط الخاصية TxtLoadOptions.isMultiEncoded() على true. كما ترى، لم يتم تحويل النص Unicode بشكل صحيح.
| ملف الإخراج 1: لم يتم اتخاذ إجراءات للتعامل مع الترميز المتعدد |
|---|
![]() |
تظهر الصورة التالية ملف XLSX الذي تم تحويله من ملف CSV السابق بعد ضبط الخاصية TxtLoadOptions.isMultiEncoded() على true. كما ترى، النص الداخلي Unicode الآن تم تحويله بشكل صحيح.
| ملف الإخراج 2: تم تعيين IsMultiEncoded على true |
|---|
![]() |
أدناه الكود النموذجي الذي يحول ملف CSV أعلاه إلى صيغة XLSX بشكل صحيح.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells - Convert Multi-Encoded CSV to XLSX</h1>
<input type="file" id="fileInput" accept=".csv" />
<button id="runExample">Convert to XLSX</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, TxtLoadOptions, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select a CSV file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Create TxtLoadOptions and set isMultiEncoded property
const options = new TxtLoadOptions();
options.isMultiEncoded = true;
// Load the CSV into a Workbook using the options
const workbook = new Workbook(new Uint8Array(arrayBuffer), options);
// Save the workbook to XLSX format and provide a download link
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
const outName = file.name.replace(/(\.[^/.]+)$/, '$1.out.xlsx');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = outName;
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Converted Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">File converted successfully! Click the download link to save the XLSX file.</p>';
});
</script>
</html>


