Crear y gestionar tablas de archivos de Microsoft Excel con JavaScript vía C++
Crear Tabla
Una de las ventajas de las hojas de cálculo es que te permiten crear diferentes tipos de listas, por ejemplo, listas de teléfonos, listas de tareas, listas de transacciones, activos o pasivos. Varios usuarios pueden colaborar para usar, crear y mantener varias listas.
Aspose.Cells soporta la creación y gestión de listas.
Ventajas de un Objeto de Lista
Existen varias ventajas cuando conviertes una lista de datos en un Objeto de Lista real
- Se incluyen automáticamente nuevas filas y columnas.
- Se puede agregar fácilmente una fila total en la parte inferior de tu lista para mostrar SUMA, PROMEDIO, CONTAR, etc.
- Las columnas agregadas a la derecha se incorporan automáticamente en el Objeto de Lista.
- Los gráficos basados en filas y columnas se expandirán automáticamente.
- Los rangos nombrados asignados a filas y columnas se expandirán automáticamente.
- La lista está protegida contra la eliminación accidental de filas y columnas.
Creación de un Objeto de Lista utilizando Microsoft Excel
- Seleccionar el rango de datos para crear un objeto Lista
- Esto muestra el cuadro de diálogo Crear Lista.
- Implementar el objeto Lista para los datos y especificar la fila total (Seleccionar Datos, luego Lista, seguido de Fila Total).
Uso de la API de Aspose.Cells
Aspose.Cells proporciona una clase, Workbook, que representa un archivo de Microsoft Excel. La clase Workbook contiene una colección de worksheets que permite acceder a cada hoja de cálculo en un archivo de Excel.
Una hoja de cálculo está representada por la clase Worksheet. La clase Worksheet proporciona un amplio rango de propiedades y métodos para gestionar una hoja de cálculo. Para crear un ListObject en una hoja, usa la propiedad de colección listObjects de la clase Worksheet. Cada ListObject es, en realidad, un objeto de la clase ListObjectCollection, que además proporciona el método add(number, number, number, number, boolean) para agregar un objeto Lista y especificar un rango de celdas para la lista.
De acuerdo con el rango de celdas especificado, el objeto Lista es creado por Aspose.Cells. Usa atributos (por ejemplo, showTotals, listColumns, etc.) de la clase ListObject para controlar la lista.
En el ejemplo que se muestra a continuación, hemos creado la misma ListObject usando la API de Aspose.Cells como la que creamos usando Microsoft Excel en la sección anterior.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells ListObjects Example</title>
</head>
<body>
<h1>ListObjects 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, TotalsCalculation } = 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();
const workbook = new Workbook(new Uint8Array(arrayBuffer));
const listObjects = workbook.worksheets.get(0).listObjects;
listObjects.add(1, 1, 7, 5, true);
const firstList = listObjects.get(0);
firstList.showTotals = true;
firstList.listColumns.get(4).totalsCalculation = TotalsCalculation.Sum;
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 Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">List created and totals calculated successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Formato de una Tabla
Para gestionar y analizar un grupo de datos relacionados, es posible convertir un rango de celdas en un objeto de lista (también conocido como una tabla de Excel). Una tabla es una serie de filas y columnas que contienen datos relacionados administrados de forma independiente de los datos en otras filas y columnas. Por defecto, cada columna en la tabla tiene activada la filtración en la fila de encabezado para que puedas filtrar o ordenar tus datos de objeto de lista rápidamente. Puedes agregar una fila total (una fila especial en una lista que proporciona una selección de funciones de agregación útiles para trabajar con datos numéricos) al objeto de lista que ofrece una lista desplegable de funciones de agregación para cada celda de fila total. Aspose.Cells proporciona opciones para crear y gestionar listas (o tablas).
Formateando un Objeto de Lista
Aspose.Cells proporciona una clase, Workbook, que representa un archivo de Microsoft Excel. La clase Workbook contiene una colección de worksheets que permite acceder a cada hoja de cálculo en un archivo de Excel.
Una hoja de cálculo está representada por la clase Worksheet. La clase Worksheet proporciona un amplio rango de propiedades y métodos para gestionar hojas de cálculo. Para crear un ListObject en una hoja, usa la propiedad de colección listObjects de la clase Worksheet. Cada ListObject es, en realidad, un objeto de la clase ListObjectCollection, que además proporciona el método add(number, number, number, number, boolean) para agregar un objeto Lista y especificar el rango de celdas que debe abarcar. De acuerdo con el rango de celdas especificado, se crea un ListObject en la hoja de cálculo por Aspose.Cells. Usa atributos (por ejemplo, TableStyleType) de la clase ListObject para formatear la tabla según tus requisitos.
El ejemplo a continuación añade datos de ejemplo a una hoja, añade un ListObject y aplica estilos predeterminados. Los estilos ListObject son compatibles con Microsoft Excel 2007/2010.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells ListObject 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 () => {
// This example does not require an input file; it creates a new workbook
const workbook = new Workbook();
// Obtaining the reference of the default(first) worksheet
const sheet = workbook.worksheets.get(0);
// Obtaining Worksheet's cells collection
const cells = sheet.cells;
// Setting the value to the cells (converted putValue -> value)
cells.get(1, 1).value = "Employee";
cells.get(1, 2).value = "Quarter";
cells.get(1, 3).value = "Product";
cells.get(1, 4).value = "Continent";
cells.get(1, 5).value = "Country";
cells.get(1, 6).value = "Sale";
cells.get(2, 1).value = "David";
cells.get(3, 1).value = "David";
cells.get(4, 1).value = "David";
cells.get(5, 1).value = "David";
cells.get(6, 1).value = "James";
cells.get(7, 1).value = "James";
cells.get(8, 1).value = "James";
cells.get(9, 1).value = "James";
cells.get(10, 1).value = "James";
cells.get(11, 1).value = "Miya";
cells.get(12, 1).value = "Miya";
cells.get(13, 1).value = "Miya";
cells.get(14, 1).value = "Miya";
cells.get(15, 1).value = "Miya";
cells.get(2, 2).value = 1;
cells.get(3, 2).value = 2;
cells.get(4, 2).value = 3;
cells.get(5, 2).value = 4;
cells.get(6, 2).value = 1;
cells.get(7, 2).value = 2;
cells.get(8, 2).value = 3;
cells.get(9, 2).value = 4;
cells.get(10, 2).value = 4;
cells.get(11, 2).value = 1;
cells.get(12, 2).value = 1;
cells.get(13, 2).value = 2;
cells.get(14, 2).value = 2;
cells.get(15, 2).value = 2;
cells.get(2, 3).value = "Maxilaku";
cells.get(3, 3).value = "Maxilaku";
cells.get(4, 3).value = "Chai";
cells.get(5, 3).value = "Maxilaku";
cells.get(6, 3).value = "Chang";
cells.get(7, 3).value = "Chang";
cells.get(8, 3).value = "Chang";
cells.get(9, 3).value = "Chang";
cells.get(10, 3).value = "Chang";
cells.get(11, 3).value = "Geitost";
cells.get(12, 3).value = "Chai";
cells.get(13, 3).value = "Geitost";
cells.get(14, 3).value = "Geitost";
cells.get(15, 3).value = "Geitost";
cells.get(2, 4).value = "Asia";
cells.get(3, 4).value = "Asia";
cells.get(4, 4).value = "Asia";
cells.get(5, 4).value = "Asia";
cells.get(6, 4).value = "Europe";
cells.get(7, 4).value = "Europe";
cells.get(8, 4).value = "Europe";
cells.get(9, 4).value = "Europe";
cells.get(10, 4).value = "Europe";
cells.get(11, 4).value = "America";
cells.get(12, 4).value = "America";
cells.get(13, 4).value = "America";
cells.get(14, 4).value = "America";
cells.get(15, 4).value = "America";
cells.get(2, 5).value = "China";
cells.get(3, 5).value = "India";
cells.get(4, 5).value = "Korea";
cells.get(5, 5).value = "India";
cells.get(6, 5).value = "France";
cells.get(7, 5).value = "France";
cells.get(8, 5).value = "Germany";
cells.get(9, 5).value = "Italy";
cells.get(10, 5).value = "France";
cells.get(11, 5).value = "U.S.";
cells.get(12, 5).value = "U.S.";
cells.get(13, 5).value = "Brazil";
cells.get(14, 5).value = "U.S.";
cells.get(15, 5).value = "U.S.";
cells.get(2, 6).value = 2000;
cells.get(3, 6).value = 500;
cells.get(4, 6).value = 1200;
cells.get(5, 6).value = 1500;
cells.get(6, 6).value = 500;
cells.get(7, 6).value = 1500;
cells.get(8, 6).value = 800;
cells.get(9, 6).value = 900;
cells.get(10, 6).value = 500;
cells.get(11, 6).value = 1600;
cells.get(12, 6).value = 600;
cells.get(13, 6).value = 2000;
cells.get(14, 6).value = 500;
cells.get(15, 6).value = 900;
// Adding a new List Object to the worksheet
const index = sheet.listObjects.add("A1", "F15", true);
const listObject = sheet.listObjects.get(index);
// Adding Default Style to the table (converted setter -> property)
listObject.tableStyleType = AsposeCells.TableStyleType.TableStyleMedium10;
// Show Total
listObject.showTotals = true;
// Set the Quarter field's calculation type (converted getter/setter -> property)
listObject.listColumns.get(1).totalsCalculation = AsposeCells.TotalsCalculation.Count;
// 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 Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook created and table added successfully! Click the download link to get the file.</p>';
});
</script>
</html>