Kaip pridėti eilutę prie lentelės Word naudojant Java.

Šioje trumpoje pamokoje paaiškinama, kaip įterpti eilutę prie lentelės programoje Word naudojant Java. Naudodami šią pamoką taip pat galite įterpti kelias eilutes į Word lentelę naudodami Java. Pabaigoje šis išvesties failas išsaugomas kaip DOCX, tačiau galite išsaugoti jį bet kokiu Word failo formatu.

Veiksmai, kaip pridėti eilutę prie lentelės programoje Word naudojant Java.

  1. Sukonfigūruokite projektą, kad pridėtumėte Aspose.Words for Java iš Maven saugyklos
  2. Atidarykite Word failą, kuriame yra Table, naudodami objektą Document
  3. Gaukite nuorodą į lentelę Word faile
  4. Sukurkite naują Row ir pridėkite norimus duomenis į stulpelius
  5. Įterpkite šią eilutę po pirmosios lentelės eilutės
  6. Klonuokite esamą eilutę ir išvalykite jos turinį
  7. Užpildykite kelias eilutes tam tikrais duomenimis
  8. Pabaigoje pridėkite eilutes prie esamos Word lentelės
  9. Į esamą lentelę įtraukę eilutes išsaugokite failą

Atlikdami šiuos veiksmus atidarome Word failą su lentele ir įterpiame eilutę. Panašiai galime pridėti kelias eilutes prie lentelės programoje Word naudodami Java, užpildydami pavyzdinius duomenis į kelias eilutes ir pridėdami šias eilutes lentelės pabaigoje.

Kodas, skirtas pridėti naują eilutę prie lentelės programoje Word naudojant Java.

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");
}
}

Šiame Java kode naudojome klases “Dokumentas”, “Lentelė” ir “Eilutė”, kad pasiektume skirtingus Word dokumento elementus, ir * naudodami Java * įtraukėme eilutę prie esamos Word lentelės. Kodo pabaigoje pateikiamas pavyzdys, kaip pridėti kelias eilutes Word lentelėje naudojant Java, kad eilutė būtų kelis kartus pridėta prie eilučių rinkinio cikle demonstravimo tikslais.

Šioje mokymo programoje atidarėme esamą failą, tačiau jei norite sukurti naują Word dokumentą, skaitykite straipsnį Kaip sukurti Word dokumentą naudojant Java. Atkreipkite dėmesį, kad nereikalaujame, kad sistemoje būtų MS Words arba Interop, kad būtų paleistas aukščiau nurodytas kodas.

 Latviski