Questo breve tutorial ti guida su come inserire una tabella in una diapositiva utilizzando Java. Condivide i dettagli necessari per impostare l’IDE per la scrittura dell’applicazione e i passaggi dettagliati per eseguire questa attività. Alla fine, otterrai un codice di esempio eseguibile che dimostra come creare una tabella in PowerPoint utilizzando Java e salvare la presentazione risultante sul disco come PPTX, PPT o in qualsiasi altro formato supportato senza installare MS PowerPoint o qualsiasi altro strumento di terze parti.
Passaggi per aggiungere una tabella in PowerPoint utilizzando Java
- Imposta l’IDE per utilizzare Aspose.Slides per inserire una tabella
- Crea una nuova presentazione utilizzando la classe Presentation e accedi alla prima diapositiva
- Aggiungi una nuova tabella nella diapositiva selezionata utilizzando il metodo addTable() e fornendo la posizione del testo, l’altezza e la larghezza delle celle
- Scorri tutte le righe e imposta il testo insieme alle impostazioni del carattere
- Salva la presentazione sul disco contenente una tabella
I passaggi di cui sopra spiegano come creare tabelle in PowerPoint utilizzando Java. Vengono identificate tutte le classi e i metodi necessari per eseguire l’attività. Il processo è semplice in cui una tabella viene aggiunta a una diapositiva e quindi le sue celle vengono riempite con del testo di esempio.
Codice per creare una tabella in PowerPoint utilizzando 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"); | |
} | |
} |
Questo codice mostra come creare una tabella in PowerPoint utilizzando Java. Vengono utilizzate più classi di interfaccia per utilizzare diverse funzionalità, ad esempio ISlide viene utilizzata per accedere alla diapositiva, la classe ITable viene utilizzata per lavorare con le tabelle e le classi IRow e ICell vengono utilizzate per lavorare con singole celle. La classe ITextFrame è l’interfaccia principale utilizzata per impostare il testo in una cella e impostare la formattazione delle celle.
Questo tutorial ci ha insegnato come creare una tabella nella presentazione di PowerPoint usando Java. Se vuoi imparare il processo per creare un HTML da una presentazione, fai riferimento all’articolo su come creare diapositive PowerPoint in HTML utilizzando Java.