List of usage examples for org.apache.poi.xwpf.usermodel XWPFTableCell getParagraphs
public List<XWPFParagraph> getParagraphs()
From source file:javaapplication1.AnotherPOI.java
public static void replaceText(String findText, String replaceText) { try {// w ww . java 2s. c o m XWPFDocument doc = new XWPFDocument(OPCPackage.open("D:\\template.docx")); for (XWPFParagraph p : doc.getParagraphs()) { List<XWPFRun> runs = p.getRuns(); if (runs != null) { for (XWPFRun r : runs) { String text = r.getText(0); if (text != null && text.contains(findText)) { text = text.replace(findText, replaceText); r.setText(text, 0); } } } } for (XWPFTable tbl : doc.getTables()) { for (XWPFTableRow row : tbl.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { for (XWPFParagraph p : cell.getParagraphs()) { for (XWPFRun r : p.getRuns()) { String text = r.getText(0); if (text.contains(findText)) { text = text.replace(findText, replaceText); r.setText(text); } } } } } } doc.write(new FileOutputStream("D:\\result.docx")); } catch (IOException ex) { Logger.getLogger(AnotherPOI.class.getName()).log(Level.SEVERE, null, ex); } catch (InvalidFormatException ex) { Logger.getLogger(AnotherPOI.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:myexamples.WordDocsExamples.Test1.java
public static void simplepartsReading() throws IOException { JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { System.out.println(chooser.getSelectedFile().getName()); FileInputStream fis = new FileInputStream(chooser.getSelectedFile()); XWPFDocument doc = new XWPFDocument(fis); XWPFWordExtractor extract = new XWPFWordExtractor(doc); //System.out.println(extract.getText()); List<XWPFParagraph> pList = doc.getParagraphs(); List<XWPFTable> tList = doc.getTables(); System.out.println("Number of Paragraphs=" + pList.size()); System.out.println("Number of Tables=" + tList.size()); List<XWPFTableRow> rList; List<XWPFTableCell> cList; List<XWPFParagraph> rcpList; int tCount = 0, rCount = 0, cCount = 0, rcpCount = 0, dummCounter = 0; WordReference wordReference = new WordReference(); for (XWPFTable t : tList) { rList = t.getRows();// w ww .j a va2 s . c o m rCount = 0; cCount = 0; rcpCount = 0; System.out.println("Table Nr." + (tCount++)); for (XWPFTableRow r : rList) { cList = r.getTableCells(); cCount = 0; rcpCount = 0; System.out.println("Row Nr." + (rCount++)); for (XWPFTableCell c : cList) { rcpList = c.getParagraphs(); rcpCount = 0; System.out.println("Cell Nr." + (cCount++)); System.out.println("Cell Text: " + c.getText()); System.out.println("Nr of Tables: " + c.getTables().size()); for (XWPFParagraph rcp : rcpList) { System.out.println("Par Nr." + (rcpCount++) + " Paragraphtext=" + rcp.getText()); } for (XWPFTable t1 : c.getTables()) { for (XWPFTableRow r1 : t1.getRows()) { for (XWPFTableCell c1 : r1.getTableCells()) { System.out.println("DC Nr." + dummCounter + " Cell Text: " + c1.getText()); switch (dummCounter) { case 0: wordReference.kundenLogo = c1.getText(); break; case 1: wordReference.kundenprofil = c1.getText(); break; case 2: wordReference.ausgangslage = c1.getText(); break; case 3: wordReference.losung = c1.getText(); break; case 4: wordReference.ergebnis = c1.getText(); break; case 5: wordReference.kunde = c1.getText(); break; case 6: wordReference.projektname = c1.getText(); break; case 7: wordReference.kundenstatement = c1.getText(); break; case 8: wordReference.statementBei = c1.getText(); break; case 9: wordReference.flisstext = c1.getText(); break; default: ; } dummCounter++; } } } } } } System.out.println(wordReference.toString(1)); } }
From source file:myexamples.WordDocsExamples.Test1.java
public static WordReference getWordReference() throws IOException { WordReference wordReference = new WordReference(); JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { System.out.println(chooser.getSelectedFile().getName()); FileInputStream fis = new FileInputStream(chooser.getSelectedFile()); XWPFDocument doc = new XWPFDocument(fis); XWPFWordExtractor extract = new XWPFWordExtractor(doc); //System.out.println(extract.getText()); List<XWPFParagraph> pList = doc.getParagraphs(); List<XWPFTable> tList = doc.getTables(); // System.out.println("Number of Paragraphs="+pList.size()); // System.out.println("Number of Tables="+tList.size()); List<XWPFTableRow> rList; List<XWPFTableCell> cList; List<XWPFParagraph> rcpList; int tCount = 0, rCount = 0, cCount = 0, rcpCount = 0, dummCounter = 0; for (XWPFTable t : tList) { rList = t.getRows();/*from ww w . j a v a 2s .c o m*/ rCount = 0; cCount = 0; rcpCount = 0; // System.out.println("Table Nr."+(tCount++)); for (XWPFTableRow r : rList) { cList = r.getTableCells(); cCount = 0; rcpCount = 0; // System.out.println("Row Nr."+(rCount++)); for (XWPFTableCell c : cList) { rcpList = c.getParagraphs(); rcpCount = 0; // System.out.println("Cell Nr."+(cCount++)); // System.out.println("Cell Text: "+c.getText()); // System.out.println("Nr of Tables: "+c.getTables().size()); for (XWPFParagraph rcp : rcpList) { // System.out.println("Par Nr."+(rcpCount++)+" Paragraphtext="+ rcp.getText()); } for (XWPFTable t1 : c.getTables()) { for (XWPFTableRow r1 : t1.getRows()) { for (XWPFTableCell c1 : r1.getTableCells()) { //System.out.println("DC Nr."+dummCounter+" Cell Text: "+c1.getText()); switch (dummCounter) { case 0: wordReference.kundenLogo = c1.getText(); break; case 1: wordReference.kundenprofil = c1.getText(); break; case 2: wordReference.ausgangslage = c1.getText(); break; case 3: wordReference.losung = c1.getText(); break; case 4: wordReference.ergebnis = c1.getText(); break; case 5: wordReference.kunde = c1.getText(); break; case 6: wordReference.projektname = c1.getText(); break; case 7: wordReference.kundenstatement = c1.getText(); break; case 8: wordReference.statementBei = c1.getText(); break; case 9: wordReference.flisstext = c1.getText(); break; default: ; } dummCounter++; } } } } } } //System.out.println(wordReference.toString(1)); } return wordReference; }
From source file:offishell.word.Word.java
License:MIT License
/** * <p>/* ww w . ja va2 s.com*/ * Replace variable text. * </p> * * @param doc * @param object */ private void replace(XWPFDocument doc) { // for paragraph for (XWPFParagraph para : copy(doc.getParagraphs())) { replace(para); } // for table for (XWPFTable table : copy(doc.getTables())) { for (XWPFTableRow row : copy(table.getRows())) { for (XWPFTableCell cell : copy(row.getTableCells())) { for (XWPFParagraph para : copy(cell.getParagraphs())) { replace(para); } } } } // for header for (XWPFHeader header : copy(doc.getHeaderList())) { for (XWPFParagraph para : copy(header.getParagraphs())) { replace(para); } } // for footer for (XWPFFooter footer : copy(doc.getFooterList())) { for (XWPFParagraph para : copy(footer.getParagraphs())) { replace(para); } } }
From source file:offishell.word.Word.java
License:MIT License
/** * <p>//w w w . ja va2s. c om * Helper method to collect all paragraphs in this document. * </p> * * @param observer A list of {@link XWPFParagraph}. */ private void collectParagraph(IBodyElement element, Observer<? super XWPFParagraph> observer) { switch (element.getElementType()) { case PARAGRAPH: observer.accept((XWPFParagraph) element); break; case TABLE: XWPFTable table = (XWPFTable) element; for (XWPFTableRow row : table.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { for (XWPFParagraph para : cell.getParagraphs()) { collectParagraph(para, observer); } } } break; default: break; } }
From source file:offishell.word.WordHeleper.java
License:MIT License
/** * <p>//w w w . j a va2 s . c o m * Helper method to remove all comments from the specified document. * </p> * * @param document A target document. */ public static void clearComment(XWPFDocument document) { for (XWPFParagraph paragraph : document.getParagraphs()) { clearComment(paragraph); } // for table for (XWPFTable table : document.getTables()) { for (XWPFTableRow row : table.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { for (XWPFParagraph para : cell.getParagraphs()) { clearComment(para); } } } } }
From source file:offishell.word.WordHeleper.java
License:MIT License
/** * <p>//ww w.ja va 2 s. c om * Helper method to clone {@link XWPFTableCell}. * </p> * * @param in * @param out * @param converter */ public static void copy(XWPFTableCell in, XWPFTableCell out, UnaryOperator<String> converter) { // copy context CTTc outCTTc = out.getCTTc(); CTTcPr outPR = outCTTc.isSetTcPr() ? outCTTc.getTcPr() : outCTTc.addNewTcPr(); outPR.set(in.getCTTc().getTcPr()); // clear all elements from out cell for (int i = out.getParagraphs().size() - 1; 0 <= i; i--) { out.removeParagraph(i); } // copy children for (XWPFParagraph inPara : in.getParagraphs()) { copy(inPara, out.addParagraph(), converter); } }
From source file:org.kino.server.api.contractgenerator.java
static void writeDocxTemplate(InputStream src, OutputStream dststrem, Map<String, String> replacementMap) throws InvalidFormatException, IOException { XWPFDocument doc = new XWPFDocument(src); replaceInParagraphs(replacementMap, doc.getParagraphs()); for (XWPFTable tbl : doc.getTables()) { for (XWPFTableRow row : tbl.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { replaceInParagraphs(replacementMap, cell.getParagraphs()); }/*from w ww . j a v a 2s .c o m*/ } } doc.write(dststrem); }
From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java
License:Open Source License
/** * Fill a newly created word table with the data from an MTable. * /*w ww .j ava 2 s .c o m*/ * @param table * The newly created word table * @param mtable * The MTable that describes the data and styles to insert */ private void fillTable(XWPFTable table, MTable mtable) { List<MRow> rows = mtable.getRows(); // Iterate over the rows for (int rowIdx = 0; rowIdx < rows.size(); rowIdx++) { MRow mRow = rows.get(rowIdx); // Get or create XWPF row XWPFTableRow xwpfRow; if (table.getNumberOfRows() > rowIdx) { xwpfRow = table.getRow(rowIdx); } else { xwpfRow = table.createRow(); } // Iterate over the columns for (int colIdx = 0; colIdx < mtable.getColumnsCount(); colIdx++) { // Get or create XWPF cell XWPFTableCell xwpfCell; if (xwpfRow.getTableCells().size() > colIdx) { xwpfCell = xwpfRow.getCell(colIdx); } else { xwpfCell = xwpfRow.createCell(); } // Populate cell XWPFParagraph xwpfCellParagraph = xwpfCell.getParagraphs().get(0); xwpfCellParagraph.setSpacingBefore(0); xwpfCellParagraph.setSpacingAfter(0); MCell mCell = null; if (colIdx < mRow.getCells().size()) { mCell = mRow.getCells().get(colIdx); } setCellContent(xwpfCell, mCell); } } }
From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java
License:Open Source License
/** * Create a new run in the cell's paragraph and set this run's text, and apply the given style to the cell and its paragraph. * //from ww w .j a v a 2s .c o m * @param cell * Cell to fill in * @param mCell * the cell to read data from */ private void setCellContent(XWPFTableCell cell, MCell mCell) { XWPFParagraph cellParagraph = cell.getParagraphs().get(0); XWPFRun cellRun = cellParagraph.createRun(); if (mCell != null) { String label = mCell.getLabel(); if (label != null) { cellRun.setText(label); } MStyle style = mCell.getStyle(); if (style != null) { if (style.getBackgroundColor() != null) { cell.setColor(hexColor(style.getBackgroundColor())); } applyMStyle(cellRun, style); } } }