Abrir diferentes archivos de versiones de Microsoft Excel con JavaScript a través de C++
Cómo Abrir Archivos de Diferentes Versiones de Microsoft Excel
Una aplicación a menudo debe poder abrir archivos de Microsoft Excel creados en diferentes versiones, por ejemplo, Microsoft Excel 95, 97, o Microsoft Excel 2007/2010/2013/2016/2019 y Office 365. Es posible que tenga que cargar un archivo en cualquiera de varios formatos, incluyendo XLS, XLSX, XLSM, XLSB, SpreadsheetML, TabDelimited o TSV, CSV, ODS, entre otros. Use el constructor, o especifique el atributo de tipo fileFormat de la clase Workbook que indica el formato usando la enumeración FileFormatType.
La enumeración FileFormatType contiene muchos formatos de archivo predefinidos, algunos de los cuales se muestran a continuación.
| Tipos de formato de archivo | Descripción |
|---|---|
| Csv | Representa un archivo CSV |
| Excel97To2003 | Representa un archivo de Excel 97 - 2003 |
| Xlsx | Representa un archivo de Excel 2007/2010/2013/2016/2019 y Office 365 XLSX |
| Xlsm | Representa un archivo de Excel 2007/2010/2013/2016/2019 y Office 365 XLSM |
| Xltx | Representa una plantilla de Excel 2007/2010/2013/2016/2019 y Office 365 XLTX |
| Xltm | Representa un archivo de Excel 2007/2010/2013/2016/2019 y Office 365 habilitado para macros XLTM |
| Xlsb | Representa un archivo binario XLSB de Excel 2007/2010/2013/2016/2019 y Office 365 |
| SpreadsheetML | Representa un archivo de SpreadsheetML |
| Tsv | Representa un archivo de valores separados por tabulaciones |
| TabDelimited | Representa un archivo de texto delimitado por tabulaciones |
| Ods | Representa un archivo ODS |
| Html | Representa un archivo HTML |
| Mhtml | Representa un archivo MHTML |
Abrir archivos de Microsoft Excel 95/5.0
Para abrir un archivo de Microsoft Excel 95/5.0, use LoadOptions y configure el atributo relacionado para la clase LoadOptions para que se cargue el archivo de plantilla. Un archivo de ejemplo para probar esta función se puede descargar desde el siguiente enlace:
<!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>
Abrir archivos de Microsoft Excel 97 - 2003
Para abrir un archivo de Microsoft Excel 97 - 2003, use LoadOptions y configure el atributo relacionado para la clase LoadOptions para que se cargue el archivo de plantilla.
<!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>
Abrir archivos de Microsoft Excel 2007/2010/2013/2016/2019 y Office 365 XLSX
Para abrir un formato de Microsoft Excel 2007/2010/2013/2016/2019 y Office 365, es decir, XLSX o XLSB, especifique la ruta del archivo. También puede usar LoadOptions y configurar el atributo/opciones relacionados de la clase LoadOptions para que se cargue el archivo de plantilla.
<!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>
Abrir archivos de Excel encriptados
Es posible crear archivos de Excel encriptados utilizando Microsoft Excel. Para abrir un archivo encriptado, use LoadOptions y establezca sus atributos y opciones (por ejemplo, una contraseña) para que se cargue el archivo de plantilla. Se puede descargar un archivo de muestra para probar esta función desde el siguiente enlace:
<!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 también admite la apertura de archivos protegidos con contraseña de Microsoft Excel 2007, 2010, 2013, 2016, 2019 y Office 365.