Öffnen verschiedener Microsoft Excel Dateien mit JavaScript über C++
Wie man Dateien verschiedener Microsoft Excel-Versionen öffnet
Eine Anwendung muss oft in der Lage sein, Microsoft Excel-Dateien, die in verschiedenen Versionen erstellt wurden, zu öffnen, z.B. Microsoft Excel 95, 97 oder Microsoft Excel 2007/2010/2013/2016/2019 und Office 365. Es könnte notwendig sein, eine Datei in einem der vielen Formate zu laden, einschließlich XLS, XLSX, XLSM, XLSB, SpreadsheetML, TabDelimited oder TSV, CSV, ODS usw. Verwenden Sie den Konstruktor oder geben Sie die Klasse Workbook mit dem Attribut fileFormat des Typs an, das das Format unter Verwendung der Aufzählung FileFormatType angibt.
Die Aufzählung FileFormatType enthält viele vordefinierte Dateiformate, von denen einige unten aufgeführt sind.
| Dateiformat-Typen | Beschreibung |
|---|---|
| Csv | Stellt eine CSV-Datei dar |
| Excel97To2003 | Stellt eine Excel 97 - 2003-Datei dar |
| Xlsx | Stellt eine Excel 2007/2010/2013/2016/2019 und Office 365 XLSX-Datei dar |
| Xlsm | Stellt eine Excel 2007/2010/2013/2016/2019 und Office 365 XLSM-Datei dar |
| Xltx | Stellt eine Excel 2007/2010/2013/2016/2019 und Office 365-Vorlage XLTX-Datei dar |
| Xltm | Stellt eine Excel 2007/2010/2013/2016/2019 und Office 365 makrofähige XLTM-Datei dar |
| Xlsb | Stellt eine Excel 2007/2010/2013/2016/2019 und Office 365 binäre XLSB-Datei dar |
| SpreadsheetML | Stellt eine SpreadsheetML-Datei dar |
| Tsv | Stellt eine Tabstoppwerte-Datei dar |
| TabDelimited | Stellt eine tabulatorgetrennte Textdatei dar |
| Ods | Stellt eine ODS-Datei dar |
| Html | Stellt eine HTML-Datei dar |
| Mhtml | Stellt eine MHTML-Datei dar |
Öffnen von Microsoft Excel 95/5.0-Dateien
Um eine Microsoft Excel 95/5.0-Datei zu öffnen, verwenden Sie LoadOptions und setzen Sie das zugehörige Attribut für die LoadOptions-Klasse für die zu ladende Vorlage-Datei.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Open Excel95_5.0.xls Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Open Workbook</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, 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');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate LoadOptions specified by the LoadFormat.
const loadOptions1 = new LoadOptions(LoadFormat.Auto);
// Create a Workbook object and opening the file from the stream
const wbExcel95 = new Workbook(new Uint8Array(arrayBuffer), loadOptions1);
console.log("Microsoft Excel 95/5.0 workbook opened successfully!");
document.getElementById('result').innerHTML = '<p style="color: green;">Microsoft Excel 95/5.0 workbook opened successfully!</p>';
});
</script>
</html>
Öffnen von Microsoft Excel 97 - 2003-Dateien
Um eine Microsoft Excel 97 - 2003-Datei zu öffnen, verwenden Sie LoadOptions und setzen Sie das zugehörige Attribut für die LoadOptions-Klasse für die zu ladende Vorlage-Datei.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Open Excel 97-2003 Workbook Example</h1>
<input type="file" id="fileInput" accept=".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, 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 an Excel 97-2003 (.xls) file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
const loadOptions1 = new LoadOptions(LoadFormat.Excel97To2003);
const wbExcel97 = new Workbook(new Uint8Array(arrayBuffer), loadOptions1);
document.getElementById('result').innerHTML = '<p style="color: green;">Microsoft Excel 97 - 2003 workbook opened successfully!</p>';
const outputData = wbExcel97.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData], { type: 'application/vnd.ms-excel' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
});
</script>
</html>
Öffnen von Microsoft Excel 2007/2010/2013/2016/2019- und Office 365 XLSX-Dateien
Um ein Microsoft Excel 2007/2010/2013/2016/2019 und Office 365 Format zu öffnen, das XLSX oder XLSB ist, geben Sie den Dateipfad an. Sie können auch LoadOptions verwenden und das zugehörige Attribut/Optionen der LoadOptions-Klasse für die zu ladende Vorlage-Datei setzen.
<!DOCTYPE html>
<html>
<head>
<title>Open Excel 2007 Xlsx Example</title>
</head>
<body>
<h1>Open Excel 2007 Xlsx Example</h1>
<input type="file" id="fileInput" accept=".xlsx" />
<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, 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 Excel .xlsx file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate LoadOptions specified by the LoadFormat.
const loadOptions = new LoadOptions(LoadFormat.Xlsx);
// 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;">Microsoft Excel 2007 - Office365 workbook opened successfully!</p>';
// Save the workbook back to a downloadable file (unchanged content)
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'Book_Excel2007_output.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Opened Workbook';
});
</script>
</html>
Verschlüsselte Excel-Dateien öffnen
Es ist möglich, verschlüsselte Excel-Dateien mit Microsoft Excel zu erstellen. Um eine verschlüsselte Datei zu öffnen, verwenden Sie LoadOptions und setzen Sie seine Attributen und Optionen (z.B. Passwort) für die zu ladende Vorlage-Datei. Eine Beispieldatei zum Testen dieses Features kann über folgenden Link heruntergeladen werden:
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Open Encrypted Workbook Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Open Encrypted Workbook</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, 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 encrypted Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate LoadOptions
const loadOptions = new LoadOptions();
// Specify the password (converted from setPassword to property assignment)
loadOptions.password = "1234";
// Create a Workbook object opening the file from the uploaded bytes with loadOptions
const wbEncrypted = new Workbook(new Uint8Array(arrayBuffer), loadOptions);
console.log("Encrypted excel file opened successfully!");
// Save the workbook so user can download it (using Excel97To2003 format for .xls)
const outputData = wbEncrypted.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
// Use original name with a prefix to indicate it's been opened
const originalName = file.name || 'output.xls';
downloadLink.download = 'opened_' + originalName;
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Opened Workbook';
document.getElementById('result').innerHTML = '<p style="color: green;">Encrypted Excel file opened successfully! Click the download link to get the file.</p>';
});
</script>
</html>
Aspose.Cells unterstützt auch das Öffnen von passwortgeschützten Microsoft Excel 2007, 2010, 2013, 2016, 2019, Office 365-Dateien.