Bu kısa eğitim, Java kullanarak Word’de bir tabloya nasıl satır ekleneceğini açıklar. Bu öğreticinin yardımıyla, Java kullanarak Word tablosuna birden çok satır da ekleyebilirsiniz. Sonunda bu çıktı dosyası DOCX olarak kaydedilir, ancak herhangi bir Word dosya biçiminde kaydedebilirsiniz.
Java Kullanarak Word’de Tabloya Satır Ekleme Adımları
- Maven deposundan Aspose.Words for Java eklemek için projenizi yapılandırın
- Document nesnesini kullanarak içinde Table içeren bir kelime dosyası açın
- Word dosyasındaki tablonun referansını alın
- Yeni bir Row oluşturun ve sütunlara istediğiniz verileri ekleyin
- Bu satırı tablodaki ilk satırdan sonra ekle
- Mevcut bir satırı klonlayın ve içeriğini temizleyin
- Bazı verilerle birden çok satırı doldurun
- Sonunda Word’de mevcut tabloya satır ekleyin
- Mevcut tabloya satırlar ekledikten sonra dosyayı kaydedin
Bu adımları kullanarak bir tablo içeren Word dosyasını açıyoruz ve içine bir satır ekliyoruz. Benzer şekilde, Java* kullanarak Word’deki bir tabloya örnek verileri birden çok satıra doldurup bu satırları tablonun sonuna ekleyerek *birden çok satır ekleyebiliriz.
Java kullanarak Word’de Tabloya Yeni Satır Ekleme Kodu
import com.aspose.words.License; | |
import com.aspose.words.Paragraph; | |
import com.aspose.words.Row; | |
import com.aspose.words.Run; | |
import com.aspose.words.Cell; | |
import com.aspose.words.Document; | |
import com.aspose.words.Table; | |
public class HowToAddARowToATableInWordUsingJava { | |
public static void main() throws Exception { //main() function for HowToAddARowToATableInWordUsingJava | |
// Instantiate a license to remove trial version watermark in the output Word file | |
License license = new License(); | |
license.setLicense("Aspose.Words.lic"); | |
// Open Word Document having a table in it | |
Document WordDocumentWithTable = new Document("MS Word.docx"); | |
// Get the reference to the table by index | |
Table tableToAddRowsTo = WordDocumentWithTable.getFirstSection().getBody().getTables().get(0); | |
// Instantiate a new Row class object | |
Row row = new Row(WordDocumentWithTable); | |
// Add Cells to the collection of cells of the newly created row | |
for (int i = 0; i < tableToAddRowsTo.getRows().get(0).getCells().getCount(); i++) | |
{ | |
Cell cell = new Cell(WordDocumentWithTable); | |
cell.appendChild(new Paragraph(WordDocumentWithTable)); | |
cell.getFirstParagraph().getRuns().add(new Run(WordDocumentWithTable, "Text in Cell " + i)); | |
row.getCells().add(cell); | |
} | |
// Insert the new Row after the first Row in the table | |
tableToAddRowsTo.getRows().insert(1, row); | |
// Deep Clone an existing Row from the Table | |
Row cloneOfRow = (Row)tableToAddRowsTo.getFirstRow().deepClone(true); | |
// Remove all content from all Cells in the cloned row | |
for (Cell cell : cloneOfRow) | |
{ | |
cell.removeAllChildren(); | |
cell.ensureMinimum(); | |
} | |
// Add number of rows say 10 to the end of table | |
for (int iRowCount = 0; iRowCount < 10; iRowCount++)//You can set any number of rows instead of 10 | |
{ | |
Row emptyRow = (Row)cloneOfRow.deepClone(true); | |
tableToAddRowsTo.getRows().add(emptyRow); | |
} | |
WordDocumentWithTable.save("Added Rows to Table to MS Word.docx"); | |
} | |
} |
Bu Java kodunda, Word belgesindeki farklı öğelere erişmek için Document, Table ve Row sınıflarını kullandık ve Java kullanarak Word’de var olan bir tabloya bir satır ekledik. Kodun sonunda, Java kullanarak Word tablosuna birden çok satır eklemek için bir örnek sağlanır, böylece bir satır, gösterim amacıyla bir döngüdeki satır koleksiyonuna birden çok kez eklenir.
Bu öğreticide mevcut bir dosyayı açtık ancak yeni bir Word belgesi oluşturmak istiyorsanız Java kullanarak Word belgesi nasıl oluşturulur makalesine bakın. Yukarıdaki kodu çalıştırmak için sistemde MS Words veya Interop’un bulunmasına ihtiyacımız olmadığını unutmayın.