Como inserir uma tabela em um slide usando Java

Este breve tutorial orienta você sobre como inserir uma tabela em um slide usando Java. Ele compartilha os detalhes necessários para definir o IDE para escrever o aplicativo e as etapas detalhadas para executar esta tarefa. No final, você obterá um código de exemplo executável que demonstra como criar tabelas no PowerPoint usando Java e salvar a apresentação resultante no disco como PPTX, PPT ou em qualquer outro formato suportado sem instalar o MS PowerPoint ou qualquer outra ferramenta de terceiros.

Etapas para adicionar tabela no PowerPoint usando Java

  1. Defina o IDE para usar Aspose.Slides para inserir uma tabela
  2. Crie uma nova apresentação usando a classe Presentation e tenha acesso ao primeiro slide
  3. Adicione uma nova tabela no slide selecionado usando o método addTable() e fornecendo a posição do texto e a altura e largura das células
  4. Percorra todas as linhas e defina o texto junto com as configurações de fonte
  5. Salve a apresentação no disco com uma tabela nele

As etapas mencionadas explicam como criar tabelas no PowerPoint usando Java. Todas as classes e métodos necessários são identificados para realizar a tarefa. O processo é simples, onde uma tabela é adicionada a um slide e, em seguida, suas células são preenchidas com algum texto de exemplo.

Código para criar uma tabela no 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 demonstra como criar uma tabela no PowerPoint usando Java. Múltiplas classes de interface são usadas para usar diferentes recursos, por exemplo, o ISlide é usado para acessar o slide, a classe ITable é usada para trabalhar com tabelas e as classes IRow e ICell são usadas para trabalhar com células individuais. A classe ITextFrame é a interface principal usada para definir o texto em uma célula e definir a formatação das células.

Este tutorial nos ensinou como fazer uma tabela na apresentação do PowerPoint usando Java. Se você quiser aprender o processo para criar um HTML a partir de uma apresentação, consulte o artigo em como criar slides do PowerPoint em HTML usando Java.

 Português