บทช่วยสอนสั้นๆ นี้จะแนะนำคุณเกี่ยวกับ วิธีแทรกตารางในสไลด์โดยใช้ Java โดยจะแบ่งปันรายละเอียดที่จำเป็นในการตั้งค่า IDE สำหรับการเขียนแอปพลิเคชันและขั้นตอนโดยละเอียดเพื่อดำเนินการงานนี้ ในตอนท้าย คุณจะได้รับโค้ดตัวอย่างที่รันได้ซึ่งสาธิต วิธีสร้างตารางใน PowerPoint โดยใช้ Java และบันทึกงานนำเสนอที่เป็นผลลัพธ์ลงในดิสก์เป็น PPTX, PPT หรืออื่นๆ รองรับรูปแบบโดยไม่ต้องติดตั้ง MS PowerPoint หรือเครื่องมืออื่น ๆ ของบุคคลที่สาม
ขั้นตอนการเพิ่มตารางใน PowerPoint โดยใช้ Java
- ตั้งค่า IDE ให้ใช้ Aspose.Slides เพื่อแทรกตาราง
- สร้างงานนำเสนอใหม่โดยใช้คลาส Presentation และเข้าถึงสไลด์แรก
- เพิ่มตารางใหม่ในสไลด์ที่เลือกโดยใช้เมธอด addTable() และระบุตำแหน่งข้อความ ตลอดจนความสูงและความกว้างของเซลล์
- วนซ้ำทุกแถวและตั้งค่าข้อความพร้อมกับการตั้งค่าแบบอักษร
- บันทึกงานนำเสนอบนดิสก์ที่มีตารางอยู่
ขั้นตอนข้างต้นอธิบาย วิธีสร้างตารางใน PowerPoint โดยใช้ Java มีการระบุคลาสและเมธอดที่จำเป็นทั้งหมดซึ่งจำเป็นสำหรับการทำงานให้สำเร็จ กระบวนการนี้ง่ายมากโดยเพิ่มตารางลงในสไลด์แล้วเติมเซลล์ด้วยข้อความตัวอย่าง
รหัสเพื่อสร้างตารางใน PowerPoint โดยใช้ 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"); | |
} | |
} |
รหัสนี้สาธิต วิธีสร้างตารางใน PowerPoint โดยใช้ Java คลาสอินเตอร์เฟสหลายคลาสใช้สำหรับใช้คุณลักษณะต่างๆ เช่น ISlide ใช้เพื่อเข้าถึงสไลด์ คลาส ITable ใช้สำหรับทำงานกับตาราง และคลาส IRow และ ICell ใช้เพื่อทำงานกับแต่ละเซลล์ คลาส ITextFrame เป็นอินเทอร์เฟซหลักที่ใช้ในการตั้งค่าข้อความในเซลล์และตั้งค่าการจัดรูปแบบของเซลล์
บทช่วยสอนนี้สอนเรา วิธีสร้างตารางในงานนำเสนอ PowerPoint โดยใช้ Java หากคุณต้องการเรียนรู้ขั้นตอนการสร้าง HTML จากงานนำเสนอ โปรดดูบทความใน วิธีสร้างสไลด์ PowerPoint ใน HTML โดยใช้ Java