فتح ملفات بصيغ مختلفة باستخدام JavaScript عبر C++
باستخدام Aspose.Cells، يمكنك فتح ملفات بصيغ مختلفة. يمكن لـ Aspose.Cells فتح مجموعة من تنسيقات الملفات مثل جداول بيانات Microsoft Excel (XLS، XLSX، XLSM، XLSB)، SpreadsheetML، القيم مفصولة بفواصل (CSV)، ملفات مفصولة بعلامة تبويب (TSV)، وغيرها.
إذا كنت بحاجة إلى معرفة جميع تنسيقات الملفات المدعومة، يرجى الرجوع إلى الصفحات التالية: أنواع الملفات المدعومة
فتح الملفات بتنسيقات مختلفة
تتيح Aspose.Cells للمطورين فتح ملفات جداول بيانات بتنسيقات مختلفة مثل SpreadsheetML، قيم منفصلة بواسطة الفاصلة (CSV)، قيم منفصلة بواسطة الفاصلة أو الفواصل (TSV)، ملفات ODS. يمكن للمطورين استخدام نفس المنهجية التي يستخدمونها لفتح ملفات إصدارات مختلفة من Microsoft Excel لفتح مثل تلك الملفات.
فتح ملفات SpreadsheetML
ملفات SpreadsheetML هي تمثيلات XML لجداول البيانات تتضمن كافة المعلومات عنها، مثل التنسيق والصيغ، وغيرها. منذ إصدار Microsoft Excel XP، أُضيف خيار تصدير XML إلى Microsoft Excel الذي يصدر جداول البيانات إلى ملفات SpreadsheetML.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Open SpreadsheetML (Book3.xml)</h1>
<input type="file" id="fileInput" accept=".xml,.xls,.xlsx,.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, LoadOptions, LoadFormat, 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 an SpreadsheetML (.xml) file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate LoadOptions specified by the LoadFormat.
const loadOptions = new LoadOptions(LoadFormat.SpreadsheetML);
// Create a Workbook object and open the file from the uploaded data
const workbook = new Workbook(new Uint8Array(arrayBuffer), loadOptions);
document.getElementById('result').innerHTML = '<p style="color: green;">SpreadSheetML file opened successfully!</p>';
console.log("SpreadSheetML file opened successfully!");
});
</script>
</html>
فتح ملفات HTML
يسمح Aspose.Cells لك بفتح ملف HTML إلى كائن Workbook. يجب أن يكون ملف HTML موجهًا نحو Microsoft Excel، أي أن MS-Excel يجب أن يكون قادرًا على فتحه.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Convert HTML to XLSX Example</h1>
<input type="file" id="fileInput" accept=".html,.htm" />
<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, HtmlLoadOptions, LoadFormat } = 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');
const resultEl = document.getElementById('result');
if (!fileInput.files.length) {
resultEl.innerHTML = '<p style="color: red;">Please select an HTML file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate LoadOptions specified by the LoadFormat.
const loadOptions = new HtmlLoadOptions(LoadFormat.Html);
// Create a Workbook object and opening the file from the uploaded file data
const workbook = new Workbook(new Uint8Array(arrayBuffer), loadOptions);
// Save the XLSX file
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Converted XLSX File';
resultEl.innerHTML = '<p style="color: green;">Conversion completed successfully! Click the download link to get the converted file.</p>';
});
</script>
</html>
فتح ملفات CSV
ملفات القيم المفصولة بفواصل (CSV) تحتوي على سجلات تفصل بين القيم بواسطة الفاصلة. تُخزن البيانات في جدول حيث يتم فصل كل عمود بواسطة الحرف الفاصلة ويوضع بين علامتي اقتباس مزدوجتين. إذا احتوى حقل على علامة اقتباس مزدوجة، يتم الهروب منها بمزوج من علامتي اقتباس مزدوجتين. يمكنك أيضًا استخدام Microsoft Excel لتصدير بيانات الجدول إلى CSV.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells CSV Open Example</title>
</head>
<body>
<h1>Aspose.Cells CSV Open Example</h1>
<input type="file" id="fileInput" accept=".csv" />
<button id="runExample">Open CSV</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, LoadOptions, LoadFormat } = 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();
// Instantiate LoadOptions specified by the LoadFormat.
const loadOptions4 = new LoadOptions(LoadFormat.Csv);
// Create a Workbook object and open the file from the uploaded data
const wbCSV = new Workbook(new Uint8Array(arrayBuffer), loadOptions4);
document.getElementById('result').innerHTML = '<p style="color: green;">CSV file opened successfully!</p>';
});
</script>
</html>
فتح ملفات CSV واستبدال الأحرف غير الصحيحة
عند فتح ملف CSV يحتوي على أحرف خاصة في Excel، يتم استبدال الأحرف تلقائيًا. يتم ذلك أيضًا بواسطة API Aspose.Cells والذي يُظهر في مثال الكود أدناه.
<!DOCTYPE html>
<html>
<head>
<title>Load CSV with TxtLoadOptions Example</title>
</head>
<body>
<h1>Load CSV with TxtLoadOptions Example</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, TxtLoadOptions, LoadFilter, LoadDataFilterOptions, 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 loadOptions = new TxtLoadOptions();
loadOptions.separator = ';';
loadOptions.loadFilter = new LoadFilter(LoadDataFilterOptions.CellData);
loadOptions.checkExcelRestriction = false;
loadOptions.convertNumericData = false;
loadOptions.convertDateTimeData = false;
const workbook = new Workbook(new Uint8Array(arrayBuffer), loadOptions);
const worksheet = workbook.worksheets.get(0);
const sheetName = worksheet.name;
const nameLength = sheetName.length;
console.log(sheetName);
console.log(nameLength);
console.log("CSV file opened successfully!");
document.getElementById('result').innerHTML = `<p>Worksheet Name: ${sheetName}</p><p>Name Length: ${nameLength}</p><p style="color: green;">CSV file opened successfully!</p>`;
});
});
</script>
</html>
فتح ملفات النصوص بفاصل مخصص
تُستخدم ملفات النصوص لاحتواء البيانات الجدولية بدون تنسيق. هذا النوع من الملفات هو نوعٌ من ملفات النصوص البسيطة، وقد تحتوي على بعض محددات التجزئة المخصصة.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells CSV to Text Example</title>
</head>
<body>
<h1>Convert CSV to Text Example</h1>
<input type="file" id="fileInput" accept=".csv,.txt" />
<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 } = 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();
// Instantiate Text File's LoadOptions
const txtLoadOptions = new TxtLoadOptions();
// Specify the separator
txtLoadOptions.separator = ",";
// Specify the encoding type
txtLoadOptions.encoding = AsposeCells.EncodingType.UTF8;
// Create a Workbook object and open the file from the uploaded data
const wb = new Workbook(new Uint8Array(arrayBuffer), txtLoadOptions);
// Save file as text and provide download link
const outputData = wb.save(SaveFormat.Text);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.txt';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Text File';
document.getElementById('result').innerHTML = '<p style="color: green;">Conversion completed successfully! Click the download link to get the converted file.</p>';
});
</script>
</html>
فتح ملفات النصوص المفصولة بواسطة الألسنة
ملفات النص مفصولة بعلامة تبويب (Text) تحتوي على بيانات جدول بيانات ولكن بدون تنسيق. تُرتب البيانات في صفوف وأعمدة كما في الجداول وجداول البيانات. بشكل أساسي، ملف المفصول بعلامة تبويب هو نوع خاص من ملفات النص العادي مع وجود علامة تبويب بين كل عمود.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Open Tab Delimited</title>
</head>
<body>
<h1>Open Tab Delimited Example</h1>
<input type="file" id="fileInput" accept=".txt,.csv,.tsv" />
<button id="runExample">Open File</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, LoadOptions, LoadFormat, SaveFormat, 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 tab-delimited (.txt/.tsv) file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate LoadOptions specified by the LoadFormat.
const loadOptions = new LoadOptions(LoadFormat.TabDelimited);
// Create a Workbook object and open the file from the uploaded file buffer
const wbTabDelimited = new Workbook(new Uint8Array(arrayBuffer), loadOptions);
document.getElementById('result').innerHTML = '<p style="color: green;">Tab delimited file opened successfully!</p>';
});
</script>
</html>
فتح ملفات القيم المفصولة بواسطة الألسنة (TSV)
ملف القيم المفصولة بعلامة تبويب (TSV) يحتوي على بيانات جدول بيانات ولكن بدون تنسيق. هو نفسه الملف المفصول بعلامة تبويب حيث تُرتب البيانات في صفوف وأعمدة كما في الجداول وجداول البيانات.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells TSV Load Example</title>
</head>
<body>
<h1>TSV Load Example</h1>
<input type="file" id="fileInput" accept=".tsv" />
<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, LoadOptions, LoadFormat, 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 TSV file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate LoadOptions specified by the LoadFormat.
const loadOptions = new LoadOptions(LoadFormat.Tsv);
// Create a Workbook object and opening the file from the uploaded file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer), loadOptions);
// Using the Sheet 1 in Workbook
const worksheet = workbook.worksheets.get(0);
// Accessing a cell using its name
const cell = worksheet.cells.get("C3");
// Display cell name and value
document.getElementById('result').innerHTML = `<p>Cell Name: ${cell.name} Value: ${cell.stringValue}</p>`;
});
</script>
</html>
فتح ملفات SXC
برنامج StarOffice Calc مشابه لـ Microsoft Excel ويدعم الصيغ والرسوم البيانية والوظائف والماكروز. تُحفظ جداول البيانات التي أنشئت باستخدام هذا البرنامج بملف بامتداد SXC. يُستخدم ملف SXC أيضًا لملفات جداول بيانات OpenOffice.org Calc. يمكن لـ Aspose.Cells قراءة ملفات SXC كما يظهر في عينة الكود التالية.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Read SXC Cell Example</h1>
<input type="file" id="fileInput" accept=".sxc" />
<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, LoadOptions, LoadFormat, SaveFormat, Worksheet, Cell, 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');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an SXC file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate LoadOptions specified by the LoadFormat.
const loadOptions = new LoadOptions(LoadFormat.Sxc);
// Create a Workbook object and open the file from the uploaded data
const workbook = new Workbook(new Uint8Array(arrayBuffer), loadOptions);
// Using the first worksheet in Workbook
const worksheet = workbook.worksheets.get(0);
// Accessing a cell using its name
const cell = worksheet.cells.get("C3");
// Display cell name and string value
resultDiv.innerHTML = `<p>Cell Name: ${cell.name} Value: ${cell.stringValue}</p>`;
});
</script>
</html>
فتح ملفات FODS
ملف FODS هو جدول بيانات محفوظ بصيغة OpenDocument XML بدون ضغط. يمكن لـ Aspose.Cells قراءة ملفات FODS كما يظهر في عينة الكود التالية.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Aspose.Cells Example - Open FODS</title>
</head>
<body>
<h1>Open FODS Example</h1>
<input type="file" id="fileInput" accept=".fods" />
<button id="runExample">Open FODS File</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, LoadOptions, LoadFormat } = 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');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select a FODS file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate LoadOptions specified by the LoadFormat.
const loadOptions = new LoadOptions(LoadFormat.Fods);
// Create a Workbook object and open the file from the uploaded data
const workbook = new Workbook(new Uint8Array(arrayBuffer), loadOptions);
document.getElementById('result').innerHTML = '<p style="color: green;">FODS file opened successfully!</p>';
});
</script>
</html>