Cómo insertar una tabla en una diapositiva usando Java

Este breve tutorial lo guía sobre cómo insertar una tabla en una diapositiva usando Java. Comparte los detalles necesarios para configurar el IDE para escribir la aplicación y los pasos detallados para realizar esta tarea. Al final, obtendrá un código de muestra ejecutable que demuestra cómo hacer una tabla en PowerPoint usando Java y guardar la presentación resultante en el disco como PPTX, PPT, o en cualquier otro formato compatible sin instalar MS PowerPoint o cualquier otra herramienta de terceros.

Pasos para agregar una tabla en PowerPoint usando Java

  1. Configure el IDE para usar Aspose.Slides para insertar una tabla
  2. Cree una nueva presentación usando la clase Presentation y obtenga acceso a la primera diapositiva
  3. Agrega una nueva tabla en la diapositiva seleccionada usando el método addTable() y proporcionando la posición del texto y la altura y el ancho de las celdas.
  4. Iterar a través de todas las filas y establecer el texto junto con la configuración de fuente
  5. Guarde la presentación en el disco que tiene una tabla.

Los pasos antes mencionados explican cómo crear tablas en PowerPoint usando Java. Se identifican todas las clases y métodos necesarios para realizar la tarea. El proceso es simple cuando se agrega una tabla a una diapositiva y luego sus celdas se llenan con un texto de muestra.

Código para Crear una Tabla en PowerPoint usando Java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testslides;
import com.aspose.slides.BulletType;
import com.aspose.slides.ICell;
import com.aspose.slides.IRow;
import com.aspose.slides.ISlide;
import com.aspose.slides.ITable;
import com.aspose.slides.ITextFrame;
import com.aspose.slides.License;
import com.aspose.slides.Presentation;
public class AddTable {
public static void main(String[] args) throws Exception {//handle exception during adding any tablle
String path = "/Users/mudassirkhan/Documents/KnowledgeBase/TestData/";
// Load the product license to create a table
License slidesTablelic = new License();
slidesTablelic.setLicense(path + "Conholdate.Total.Product.Family.lic");
// Create a new presentation
Presentation presentationForTable = new Presentation();
// Access the first slide in the presentation
ISlide slide = presentationForTable.getSlides().get_Item(0);
// Define the arrays containing column widths and row heights
double[] dColumnsWidths = { 55, 55, 55 };
double[] dRowsHeights = { 55, 36, 36, 36, 36 };
// Add a new table with desired rows and columns
ITable newTable = slide.getShapes().addTable(60, 60, dColumnsWidths, dRowsHeights);
// Traverse through all the rows
for (IRow tableRow : newTable.getRows())
{
// Traverse through all the cells in the current row
for (ICell tableCell : tableRow)
{
// Access the respective text frame of the cell
ITextFrame txtFormat = tableCell.getTextFrame();
// Add some text to the cell
int iFirstRowIndex = tableCell.getFirstRowIndex();
int iFirstColumnIndex = tableCell.getFirstColumnIndex();
txtFormat.setText( "Text " + iFirstRowIndex + " " + iFirstColumnIndex);
// Set the font related property for the newly added text
txtFormat.getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat().setFontHeight(10);
txtFormat.getParagraphs().get_Item(0).getParagraphFormat().getBullet().setType(BulletType.None);
}
}
// Save the presentation with table
presentationForTable.save("PresentationTable.pptx", com.aspose.slides.SaveFormat.Pptx);
System.out.println("Done");
}
}

Este código demuestra cómo crear una tabla en PowerPoint usando Java. Se usan múltiples clases de interfaz para usar diferentes funciones, por ejemplo, ISlide se usa para acceder a la diapositiva, la clase ITable se usa para trabajar con tablas y las clases IRow e ICell se usan para trabajar con celdas individuales. La clase ITextFrame es la interfaz principal que se utiliza para establecer el texto en una celda y establecer el formato de las celdas.

Este tutorial nos ha enseñado cómo hacer una tabla en una presentación de PowerPoint usando Java. Si desea aprender el proceso para crear un HTML a partir de una presentación, consulte el artículo sobre cómo crear diapositivas de PowerPoint en HTML usando Java.

 Español