Administrar hojas de trabajo de archivos de Microsoft Excel con JavaScript mediante C++
Aspose.Cells proporciona una clase, Workbook que representa un archivo de Excel. La clase Workbook contiene una colección worksheets que permite acceder a cada hoja de cálculo en el archivo de Excel.
Una hoja de cálculo está representada por la clase Worksheet. La clase Worksheet proporciona una amplia gama de propiedades y métodos para administrar las hojas.
Añadir hojas de cálculo a un nuevo archivo de Excel
Para crear un nuevo archivo de Excel programáticamente:
- Cree un objeto de la clase Workbook.
- Llame al método WorksheetCollection.add(SheetType) de la clase WorksheetCollection. Se añade automáticamente una hoja de trabajo vacía al archivo de Excel. Puede hacer referencia a ella pasando el índice de la hoja a la colección worksheets.
- Obtener una referencia a una hoja de trabajo.
- Realizar trabajos en las hojas de trabajo.
- Guardar el nuevo archivo de Excel con nuevas hojas de trabajo llamando al método Workbook.save(string, SaveFormat) de la clase Workbook.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells - Add Worksheet 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, SaveFormat } = 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 () => {
// Instantiate a new Workbook
const workbook = new Workbook();
// Get current worksheet count (converted from getWorksheets().getCount())
const i = workbook.worksheets.count;
// Add a new worksheet (converted from getWorksheets().add())
workbook.worksheets.add();
// Obtain the newly added worksheet by index (converted from getWorksheets().get(i))
const worksheet = workbook.worksheets.get(i);
// Set the name of the newly added worksheet (converted from setName)
worksheet.name = "My Worksheet";
// Save the workbook to XLS format and prepare download
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook created and saved. Click the download link to get the file.</p>';
});
</script>
</html>
Añadir hojas de cálculo a una hoja de cálculo de diseñador
El proceso de agregar hojas de cálculo a una hoja de cálculo de diseño es igual que el de agregar una nueva hoja, salvo que el archivo de Excel ya existe y debe abrirse antes de agregar hojas. Una hoja de cálculo de diseño puede abrirse utilizando la clase Workbook.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Add Worksheet</title>
</head>
<body>
<h1>Add Worksheet 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, SaveFormat, Worksheet, Cell } = 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();
// Opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Adding a new worksheet to the Workbook object
const i = workbook.worksheets.add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.worksheets.get(i);
// Setting the name of the newly added worksheet
worksheet.name = "My Worksheet";
// Saving the Excel 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 Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Worksheet added successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Acceso a las hojas de cálculo usando el nombre de la hoja
Acceda a cualquier hoja de cálculo especificando su nombre o índice
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example: Read Cell Value</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, SaveFormat, Worksheet, Cell } = 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();
// Instantiating a Workbook object by opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Accessing a worksheet using its sheet name
const worksheet = workbook.worksheets.get("Sheet1");
const cell = worksheet.cells.get("A1");
console.log(cell.value);
document.getElementById('result').innerHTML = `<p>Cell A1 value: ${cell.value}</p>`;
});
</script>
</html>
Eliminar hojas de cálculo utilizando el nombre de la hoja
Para eliminar hojas de cálculos de un archivo, llame al método WorksheetCollection.removeAt(string) de la clase WorksheetCollection. Pase el nombre de la hoja al método WorksheetCollection.removeAt(string) para eliminar una hoja específica.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells 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, SaveFormat } = 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();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Removing a worksheet using its sheet name
workbook.worksheets.removeAt("Sheet1");
// Save workbook
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Worksheet removed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Eliminar hojas de cálculo utilizando el índice de la hoja
Eliminar hojas de trabajo por nombre funciona bien cuando se conoce el nombre de la hoja. Si no conoces el nombre de la hoja, usa una versión sobrecargada del método WorksheetCollection.removeAt(string) que toma el índice de la hoja en lugar del nombre de la misma.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example - Remove First Worksheet</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, SaveFormat } = 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();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Removing a worksheet using its sheet index
workbook.worksheets.removeAt(0);
// Saving the modified Excel file
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Worksheet removed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Activar hojas y hacer que una celda sea activa en la hoja de cálculo
A veces, necesitas que una hoja de trabajo específica esté activa y visible cuando un usuario abre un archivo de Microsoft Excel en Excel. De manera similar, podrías querer activar una celda específica y configurar las barras de desplazamiento para mostrar la celda activa. Aspose.Cells es capaz de realizar todas estas tareas.
Una hoja activa es una hoja en la que está trabajando: el nombre de la hoja activa en la pestaña aparece en negrita de forma predeterminada
Una celda activa es una celda seleccionada, la celda en la que se ingresa datos cuando comienza a escribir. Solo una celda está activa a la vez. La celda activa se resalta con un borde grueso
Activar hojas y hacer que una celda sea activa
Aspose.Cells proporciona llamadas específicas a la API para activar una hoja y una celda. Por ejemplo, la propiedad WorksheetCollection.activeSheetIndex es útil para establecer la hoja activa en un libro. De manera similar, la propiedad Worksheet.activeCell se usa para establecer y obtener una celda activa en la hoja de cálculo.
Para asegurarte de que las barras de desplazamiento horizontales o verticales estén en la posición de fila y columna que deseas mostrar datos específicos, usa las propiedades Worksheet.firstVisibleRow y Worksheet.firstVisibleColumn.
El siguiente ejemplo muestra cómo activar una hoja de cálculo y hacer que una celda sea activa en ella. En la salida generada, las barras de desplazamiento se desplazarán para que la segunda fila y la segunda columna sean su primera fila y columna visibles
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Create 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, SaveFormat } = 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 () => {
// Instantiate a new Workbook.
const workbook = new Workbook();
// Add a worksheet if collection is empty
const worksheets = workbook.worksheets;
if (worksheets.count === 0) {
worksheets.add();
}
// Get the first worksheet in the workbook.
const worksheet1 = worksheets.get(0);
// Get the cells in the worksheet.
const cells = worksheet1.cells;
// Input data into B2 cell.
const cell = cells.get(1, 1);
cell.value = "Hello World!";
// Set the first sheet as an active sheet.
worksheets.activeSheetIndex = 0;
// Set B2 cell as an active cell in the worksheet.
worksheet1.activeCell = "B2";
// Set the B column as the first visible column in the worksheet.
worksheet1.firstVisibleColumn = 1;
// Set the 2nd row as the first visible row in the worksheet.
worksheet1.firstVisibleRow = 1;
// Save the excel file.
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook created successfully! Click the download link to get the file.</p>';
});
</script>
</html>
Temas avanzados
- Copiar y mover hojas de cálculo
- Contar el número de celdas en la hoja de cálculo
- Detectar hojas de cálculo vacías
- Buscar si la hoja de trabajo es una hoja de diálogo
- Obtener el ID único de la hoja de trabajo
- Crear, manipular o eliminar escenarios de hojas de trabajo
- Gestionar saltos de página
- Funciones de configuración de página
- Utilizar la propiedad SheetId de OpenXml usando Aspose.Cells
- Vistas de hojas de trabajo