تحميل أو استيراد ملف CSV مع صيغ via JavaScript
Contents
[
Hide
]
غالبًا ما تحتوي ملفات CSV على بيانات نصية ولا تحتوي على أي صيغ. ومع ذلك، أحيانًا تحتوي ملفات CSV أيضًا على صيغ. يجب تحميل مثل هذه الملفات عن طريق تعيين TxtLoadOptions.hasFormula إلى true. بمجرد تعيين هذه الخاصية إلى true، لن تعامل Aspose.Cells الصيغ كنص بسيط. ستعامل كصيغ، وسيقوم محرك حساب الصيغ في Aspose.Cells بمعالجتها كما هو معتاد.
يوضح الكود التالي كيف يمكنك تحميل واستيراد ملف CSV مع الصيغ. يمكنك استخدام أي ملف CSV. لأغراض التوضيح، نستخدم ملف CSV البسيط الذي يحتوي على بيانات مثل هذه. كما ترى فإنه يحتوي على صيغة.
<!DOCTYPE html>
<html>
<head>
<title>Load CSV with Formulas and Save as XLSX</title>
</head>
<body>
<h1>Load CSV with Formulas and Save as XLSX</h1>
<input type="file" id="fileInput" accept=".csv" />
<button id="convertToXlsx">Convert to XLSX</button>
<a id="downloadLink" style="display: none;">Download XLSX</a>
<div id="result"></div>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, TxtLoadOptions, SaveFormat } = AsposeCells;
AsposeCells.onReady().then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('convertToXlsx').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();
const loadOptions = new TxtLoadOptions();
loadOptions.hasFormula = true;
const workbook = new Workbook(new Uint8Array(arrayBuffer), loadOptions);
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = file.name.replace(/\.csv$/i, '.xlsx');
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download XLSX File';
document.getElementById('result').innerHTML = '<p style="color: green;">Conversion completed! Click the download link to get the XLSX file.</p>';
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells CSV Load Example</title>
</head>
<body>
<h1>Load CSV with Formulas and Save as XLSX</h1>
<input type="file" id="fileInput" accept=".csv" />
<button id="runExample">Run Example</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();
const bytes = new Uint8Array(arrayBuffer);
// TxtLoadOptions configuration
const opts = new TxtLoadOptions();
opts.separator = ',';
opts.hasFormula = true;
// Load your CSV file with formulas in a Workbook object
const workbook = new Workbook(bytes, opts);
// You can also import your CSV file like this
// The code below is importing CSV file starting from cell D4 (rowIndex=3, colIndex=3)
const worksheet = workbook.worksheets.get(0);
worksheet.cells.importCSV(bytes, opts, 3, 3);
// Save your workbook in Xlsx format
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output_out.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the converted file.</p>';
});
</script>
</html>
يتم أولاً تحميل ملف CSV، ثم استيراده مرة أخرى في الخلية D4. وأخيراً، يتم حفظ مصنف العمل بصيغة XLSX. يبدو ملف XLSX الناتج هكذا. كما ترى، الخلية C3 و F4 تحتويان على صيغ ونتيجتهما 800.
![]() |
|---|
