Šis trumpas žingsnis po žingsnio vadovas apima, kaip sukurti lentelę Word dokumente naudojant Java. Naudodami šią mokymo programą galite įterpti vieną ar daugiau lentelių, kad sutvarkytumėte informaciją pagal savo poreikius. Paskutiniame veiksme išvesties failas išsaugomas kaip DOCX failas, bet galite išsaugoti jį bet kokiu Word failo formatu.
Veiksmai, kaip sukurti lentelę Word dokumente naudojant Java.
- Pridėkite Aspose.Words for Java iš Maven saugyklos
- Inicijuokite tuščią Word dokumentą naudodami Document klasę
- Inicijuoti naują Table klasės egzempliorių
- Lentelėje sukurkite naują Row
- Sukurkite langelį ir į langelį įtraukite naują pastraipą
- Įdėkite lentelę dokumento pabaigoje
- Išsaugokite Word dokumentą su lentele kaip DOCX failą
Atlikdami šiuos veiksmus galime įterpti lentelę į Word dokumentą naudodami Java. Mes galime įtraukti informaciją į langelius ir tvarkyti dokumento turinį programiškai naudodami Java.
Kodas, skirtas sukurti lentelę Word dokumente su Java
import com.aspose.words.Cell; | |
import com.aspose.words.Document; | |
import com.aspose.words.License; | |
import com.aspose.words.Paragraph; | |
import com.aspose.words.Row; | |
import com.aspose.words.Run; | |
import com.aspose.words.Table; | |
public class CreateTableInWordUsingJava | |
{ | |
public static void main(String[] args) throws Exception { //main function for CreateTableInWordUsingJava class | |
// Initialize a license to avoid trial version watermark in the output Word file after adding comments | |
License license = new License(); | |
license.setLicense("Aspose.Words.lic"); | |
// Initialize a blank Word document using Document class | |
Document wordDocument = new Document(); | |
// Initialize a new Table class instance | |
Table wordTable = new Table(wordDocument); | |
// Create a new Row in the Table | |
wordTable.getRows().add(new Row(wordDocument)); | |
// Create single Cell in the Table Row | |
wordTable.getFirstRow().getCells().add(new Cell(wordDocument)); | |
// Add a new Paragraph inside Cell | |
wordTable.getFirstRow().getFirstCell().appendChild(new Paragraph(wordDocument)); | |
// Add text content inside the Table Cell | |
wordTable.getFirstRow().getFirstCell().getFirstParagraph().getRuns().add(new Run(wordDocument, "Text in Table Row 1 and Cell 1")); | |
// Insert a Table at the last portion of Word Document | |
wordDocument.getFirstSection().getBody().insertBefore(wordTable, wordDocument.getFirstSection().getBody().getLastParagraph()); | |
// Save Word document with Table in DOCX format | |
wordDocument.save("word_table.docx"); | |
} | |
} |
Šiame Java kode inicijavome tuščią Word dokumentą, tada įtraukėme lentelę ir įterpėme turinį į tam tikrą langelį. Tada lentelė įterpiama į pabaigą, kad parodytų, kaip sudaryti lentelę Word dokumente naudojant Java.
Šiame vadove mes sužinojome, kaip įterpti lentelę į Word dokumentą naudojant Java. Tačiau, jei norite pridėti eilutę prie Word dokumento lentelės, galite žiūrėti straipsnį kaip pridėti eilutes į lentelę DOCX naudojant Java.