List of usage examples for org.apache.poi.xwpf.usermodel XWPFTable getRows
public List<XWPFTableRow> getRows()
From source file:biz.webgate.dominoext.poi.component.kernel.DocumentProcessor.java
License:Apache License
private void processBookmarks2Table(List<IDocumentBookmark> arrBookmarks, XWPFTable tabCurrent) { for (XWPFTableRow tabRow : tabCurrent.getRows()) { for (XWPFTableCell tabCell : tabRow.getTableCells()) { for (XWPFParagraph paraCurrent : tabCell.getParagraphs()) { processBookmarks2Paragraph(arrBookmarks, paraCurrent); }/*from ww w. j av a 2 s . c o m*/ } } }
From source file:biz.webgate.dominoext.poi.component.kernel.DocumentProcessor.java
License:Apache License
public static void setDocCellValue(XWPFTable dxTable, int nRow, int nCol, Object objValue, int maxRow, boolean isHeader) { try {//from w ww . j a v a 2 s .c om if (dxTable.getRow(nRow) == null) { // DEFINIE MAX VALUE! while (dxTable.getRow(nRow) == null && dxTable.getRows().size() < maxRow) { dxTable.createRow(); // rowHasChanged = true; } } if (dxTable.getRow(nRow) != null) { if (dxTable.getRow(nRow).getCell(nCol) == null) { // CHECK MAX COL while (dxTable.getRow(nRow).getCell(nCol) == null && dxTable.getRow(nRow).getTableCells().size() < 50) { dxTable.getRow(nRow).addNewTableCell(); } } // dxTable.getRow(nRow).getCell(nCol).setText("" + // objValue.toString()); for (XWPFParagraph paraCurrent : dxTable.getRow(nRow).getCell(nCol).getParagraphs()) { if (paraCurrent.getRuns().size() == 0) { XWPFRun runCurrent = paraCurrent.createRun(); if (isHeader) runCurrent.setBold(true); runCurrent.setText("" + objValue.toString()); } else { for (XWPFRun runCurrent : paraCurrent.getRuns()) { if (isHeader) runCurrent.setBold(true); runCurrent.setText("" + objValue.toString()); } } } } else { System.out.println("Still null: " + nRow + " MaxRow = " + maxRow); } } catch (Exception e) { e.printStackTrace(); } }
From source file:br.com.techne.gluonsoft.eowexport.builder.WordBuilder.java
License:Apache License
/** * mtodo cria bytes de documento Excel/*from w ww.jav a 2 s. c o m*/ * @param titles * @param columnIndex * @param dataRows * @param locale * @return * @throws Exception */ public static byte[] createStyledTable(String[] titles, String[] columnIndex, List<HashMap<String, Object>> dataRows, Locale locale) throws Exception { // Create a new document from scratch XWPFDocument doc = new XWPFDocument(); byte[] outBytes; try { int nRows = dataRows.size() + 1; int nCols = columnIndex.length == 0 ? dataRows.get(0).keySet().size() : columnIndex.length; XWPFTable table = doc.createTable(nRows, nCols); // Set the table style. If the style is not defined, the table style // will become "Normal". CTTblPr tblPr = table.getCTTbl().getTblPr(); CTString styleStr = tblPr.addNewTblStyle(); styleStr.setVal("StyledTable"); // Get a list of the rows in the table List<XWPFTableRow> rows = table.getRows(); int rowCt = 1; addTitle(rows, titles); ValueCellUtil vcutil = new ValueCellUtil(locale); for (HashMap<String, Object> dataRow : dataRows) { addRow(rows, dataRow, rowCt, vcutil, columnIndex); rowCt++; } // write for return byte[] ByteArrayOutputStream out = new ByteArrayOutputStream(); try { doc.write(out); outBytes = out.toByteArray(); } finally { out.close(); } } finally { doc.close(); } return outBytes; }
From source file:ch.admin.isb.hermes5.business.word.Docx4jWordDocumentCustomizerTest.java
License:Apache License
private Map<String, String> getTableMap(byte[] result) throws IOException { Map<String, String> map = new HashMap<String, String>(); XWPFDocument document = new XWPFDocument(new ByteArrayInputStream(result)); List<XWPFTable> tables = new ArrayList<XWPFTable>(); tables.addAll(document.getTables()); List<XWPFHeader> headers = document.getHeaderList(); for (XWPFHeader header : headers) { tables.addAll(header.getTables()); }/*w w w .ja v a 2 s . co m*/ for (XWPFTable xwpfTable : tables) { List<XWPFTableRow> rows = xwpfTable.getRows(); for (XWPFTableRow xwpfTableRow : rows) { List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells(); if (tableCells.size() == 2) { map.put(tableCells.get(0).getText(), tableCells.get(1).getText()); } if (tableCells.size() == 1) { map.put(tableCells.get(0).getText(), ""); } } } return map; }
From source file:ch.admin.searchreplace.SearchReplaceTerms.java
License:Apache License
/** * Ersetzt eine Liste von Ausdruecken in einer Datei und schreibt das Resultat nach Output * @param input Inputdatei//from w ww . j a v a 2 s .c o m * @param output Outputdatei * @param srTerms Begriff * @throws IOException I/O Fehler * @throws InvalidFormatException OpenXML Document korrupt * @throws FileNotFoundException Datei nicht vorhanden */ private static void searchReplaceInFile(String input, String output, HashMap<String, String> srTerms) throws IOException, InvalidFormatException, FileNotFoundException { XWPFDocument doc = new XWPFDocument(OPCPackage.open(input)); // Header List<XWPFHeader> header = doc.getHeaderList(); for (Iterator<XWPFHeader> e = header.iterator(); e.hasNext();) { XWPFHeader h = e.next(); for (XWPFParagraph p : h.getParagraphs()) { XWPFRun r = consolidateRuns(p); if (r != null) searchReplace(srTerms, r); } for (XWPFTable tbl : h.getTables()) for (XWPFTableRow row : tbl.getRows()) for (XWPFTableCell cell : row.getTableCells()) for (XWPFParagraph p : cell.getParagraphs()) { XWPFRun r = consolidateRuns(p); if (r != null) searchReplace(srTerms, r); } } // Document for (XWPFParagraph p : doc.getParagraphs()) { XWPFRun r = consolidateRuns(p); if (r != null) searchReplace(srTerms, r); } for (XWPFTable tbl : doc.getTables()) for (XWPFTableRow row : tbl.getRows()) for (XWPFTableCell cell : row.getTableCells()) for (XWPFParagraph p : cell.getParagraphs()) { XWPFRun r = consolidateRuns(p); if (r != null) searchReplace(srTerms, r); } doc.write(new FileOutputStream(output)); }
From source file:com.anphat.customer.controller.ExportContractToDocController.java
public void buildContractDetails(CustomerDTO customerDTO) { for (XWPFTable tbl : lstTable) { for (XWPFTableRow rowTbl : tbl.getRows()) { for (XWPFTableCell cellTbl : rowTbl.getTableCells()) { for (XWPFParagraph p : cellTbl.getParagraphs()) { for (XWPFRun r : p.getRuns()) { String text = r.getText(0); // //Thoi gian // if (text != null && DataUtil.isStringContainDateTime(text)) { // text = DataUtil.replaceDateTime(text); // r.setText(text, 0); // } //Ben giao if (text != null && text.contains(Constants.REPORT.NAME)) { text = text.replace(Constants.REPORT.NAME, DataUtil.getStringNullOrZero(customerDTO.getName().toUpperCase())); r.setText(text, 0); }//from w ww . j av a2 s . c o m //Ma so thue if (text != null && text.contains(Constants.REPORT.TAX_CODE)) { text = text.replace(Constants.REPORT.TAX_CODE, DataUtil.getStringNullOrZero(customerDTO.getTaxCode())); r.setText(text, 0); } //So dien thoai if (text != null && text.contains(Constants.REPORT.TEL_NUMBER)) { text = text.replace(Constants.REPORT.TEL_NUMBER, DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.TEL_NUMBER))); r.setText(text, 0); } //Fax if (text != null && text.contains(Constants.REPORT.FAX)) { text = text.replace(Constants.REPORT.FAX, DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.FAX))); r.setText(text, 0); } //Email if (text != null && text.contains(Constants.REPORT.EMAIL)) { text = text.replace(Constants.REPORT.EMAIL, DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.EMAIL))); r.setText(text, 0); } //Dia chi tru so if (text != null && text.contains(Constants.REPORT.OFFICE_ADDRESS)) { text = text.replace(Constants.REPORT.OFFICE_ADDRESS, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.OFFICE_ADDRESS))); r.setText(text, 0); } //Dia chi giao dich if (text != null && text.contains(Constants.REPORT.DEPLOY_ADDRESS)) { text = text.replace(Constants.REPORT.DEPLOY_ADDRESS, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.DEPLOY_ADDRESS))); r.setText(text, 0); } //Co quan thue if (text != null && text.contains(Constants.REPORT.TAX_DEPARTMENT)) { text = text.replace(Constants.REPORT.TAX_DEPARTMENT, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.TAX_DEPARTMENT))); r.setText(text, 0); } //CMND if (text != null && text.contains(Constants.REPORT.CMND)) { text = text.replace(Constants.REPORT.CMND, DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.CMND))); r.setText(text, 0); } //CMND if (text != null && text.contains(Constants.REPORT.NGAY_CAP_CMND)) { text = text.replace(Constants.REPORT.NGAY_CAP_CMND, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.NGAY_CAP_CMND))); r.setText(text, 0); } //Nguoi dai dien if (text != null && text.contains(Constants.REPORT.NGUOI_DAIDIEN)) { text = text.replace(Constants.REPORT.NGUOI_DAIDIEN, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.NGUOI_DAIDIEN))); r.setText(text, 0); } //Chuc vu Nguoi dai dien if (text != null && text.contains(Constants.REPORT.CHUVU_NGUOI_DAIDIEN)) { text = text.replace(Constants.REPORT.CHUVU_NGUOI_DAIDIEN, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.CHUVU_NGUOI_DAIDIEN))); r.setText(text, 0); } //SDT Nguoi dai dien if (text != null && text.contains(Constants.REPORT.SDT_NGUOI_DAIDIEN)) { text = text.replace(Constants.REPORT.SDT_NGUOI_DAIDIEN, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.SDT_NGUOI_DAIDIEN))); r.setText(text, 0); } //Email Nguoi dai dien if (text != null && text.contains(Constants.REPORT.EMAIL_NGUOI_DAIDIEN)) { text = text.replace(Constants.REPORT.EMAIL_NGUOI_DAIDIEN, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.EMAIL_NGUOI_DAIDIEN))); r.setText(text, 0); } //Nguoi dai dien if (text != null && text.contains(Constants.REPORT.NGUOI_LIENHE)) { text = text.replace(Constants.REPORT.NGUOI_LIENHE, DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.NGUOI_LIENHE))); r.setText(text, 0); } //Chuc vu Nguoi lien he if (text != null && text.contains(Constants.REPORT.CHUCVU_NGUOI_LIENHE)) { text = text.replace(Constants.REPORT.CHUCVU_NGUOI_LIENHE, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.CHUCVU_NGUOI_LIENHE))); r.setText(text, 0); } //SDT Nguoi lien he if (text != null && text.contains(Constants.REPORT.SDT_NGUOI_LIENHE)) { text = text.replace(Constants.REPORT.SDT_NGUOI_LIENHE, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.SDT_NGUOI_LIENHE))); r.setText(text, 0); } //Email Nguoi lien he if (text != null && text.contains(Constants.REPORT.EMAIL_NGUOI_LIENHE)) { text = text.replace(Constants.REPORT.EMAIL_NGUOI_LIENHE, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.EMAIL_NGUOI_LIENHE))); r.setText(text, 0); } } } } } } }
From source file:com.deepoove.poi.resolver.TemplateResolver.java
License:Apache License
public static List<ElementTemplate> parseTable(XWPFTable table) { if (null == table) return null; List<ElementTemplate> rts = new ArrayList<ElementTemplate>(); List<XWPFTableRow> rows = table.getRows(); if (null != rows) { for (XWPFTableRow row : rows) { List<XWPFTableCell> cells = row.getTableCells(); if (null != cells) { for (XWPFTableCell cell : cells) { // cell? // CellTemplate parseCell = parseCell(cell); // if (null != parseCell) { // rts.add(parseCell); // } else { rts.addAll(parseParagraph(cell.getParagraphs())); rts.addAll(parseTable(cell.getTables())); }//ww w . jav a 2s . co m } } } return rts; }
From source file:com.deepoove.poi.tl.SimpleTable.java
License:Apache License
/** * Create a table with some row and column styling. I "manually" add the * style name to the table, but don't check to see if the style actually * exists in the document. Since I'm creating it from scratch, it obviously * won't exist. When opened in MS Word, the table style becomes "Normal". * I manually set alternating row colors. This could be done using Themes, * but that's left as an exercise for the reader. The cells in the last * column of the table have 10pt. "Courier" font. * I make no claims that this is the "right" way to do it, but it worked * for me. Given the scarcity of XWPF examples, I thought this may prove * instructive and give you ideas for your own solutions. //w w w .j a va 2s . c o m * @throws Exception */ public static void createStyledTable() throws Exception { // Create a new document from scratch XWPFDocument doc = new XWPFDocument(); // -- OR -- // open an existing empty document with styles already defined //XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx")); // Create a new table with 6 rows and 3 columns int nRows = 6; int nCols = 3; XWPFTable table = doc.createTable(nRows, nCols); // Set the table style. If the style is not defined, the table style // will become "Normal". CTTblPr tblPr = table.getCTTbl().getTblPr(); CTString styleStr = tblPr.addNewTblStyle(); styleStr.setVal("StyledTable"); // Get a list of the rows in the table List<XWPFTableRow> rows = table.getRows(); int rowCt = 0; int colCt = 0; for (XWPFTableRow row : rows) { // get table row properties (trPr) CTTrPr trPr = row.getCtRow().addNewTrPr(); // set row height; units = twentieth of a point, 360 = 0.25" CTHeight ht = trPr.addNewTrHeight(); ht.setVal(BigInteger.valueOf(360)); // get the cells in this row List<XWPFTableCell> cells = row.getTableCells(); // add content to each cell for (XWPFTableCell cell : cells) { // get a table cell properties element (tcPr) CTTcPr tcpr = cell.getCTTc().addNewTcPr(); // set vertical alignment to "center" CTVerticalJc va = tcpr.addNewVAlign(); va.setVal(STVerticalJc.CENTER); // create cell color element CTShd ctshd = tcpr.addNewShd(); ctshd.setColor("auto"); ctshd.setVal(STShd.CLEAR); if (rowCt == 0) { // header row ctshd.setFill("A7BFDE"); } else if (rowCt % 2 == 0) { // even row ctshd.setFill("D3DFEE"); } else { // odd row ctshd.setFill("EDF2F8"); } // get 1st paragraph in cell's paragraph list XWPFParagraph para = cell.getParagraphs().get(0); // create a run to contain the content XWPFRun rh = para.createRun(); // style cell as desired if (colCt == nCols - 1) { // last column is 10pt Courier rh.setFontSize(10); rh.setFontFamily("Courier"); } if (rowCt == 0) { // header row rh.setText("header row, col " + colCt); rh.setBold(true); para.setAlignment(ParagraphAlignment.CENTER); } else if (rowCt % 2 == 0) { // even row rh.setText("row " + rowCt + ", col " + colCt); para.setAlignment(ParagraphAlignment.LEFT); } else { // odd row rh.setText("row " + rowCt + ", col " + colCt); para.setAlignment(ParagraphAlignment.LEFT); } colCt++; } // for cell colCt = 0; rowCt++; } // for row // write the file FileOutputStream out = new FileOutputStream("styledTable.docx"); doc.write(out); out.close(); }
From source file:com.foc.msword.WordTemplateFillerResource.java
License:Apache License
private void fillTable(ExtendedWordDocument xWord, XWPFTable xwpfTable) { List<XWPFTableRow> tableRows = xwpfTable.getRows(); XWPFTableRow rowToDuplicate = null;/*from w ww .j av a 2s .co m*/ String propertyName = null; FocList slaveList = null; //First we check if this row is to be duplicated [*] for (int r = 0; r < tableRows.size() && slaveList == null; r++) { XWPFTableRow xwpfTableRow = tableRows.get(r); List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells(); for (XWPFTableCell xwpfTableCell : tableCells) { List<XWPFParagraph> paragraphs = xwpfTableCell.getParagraphs(); if (paragraphs != null) { for (int p = 0; p < paragraphs.size() && slaveList == null; p++) { XWPFParagraph para = paragraphs.get(p); String paragraphText = para.getParagraphText(); int listStartIndex = paragraphText.indexOf("_LIST[*]."); int dollarIndex = paragraphText.indexOf("$F{"); if (dollarIndex >= 0 && listStartIndex > dollarIndex) { propertyName = paragraphText.substring(dollarIndex + 3, listStartIndex + 5); Object res = getFocData().iFocData_getDataByPath(propertyName); if (res instanceof FocList) { slaveList = (FocList) res; rowToDuplicate = xwpfTableRow; } } } } } } if (slaveList != null) { for (int i = 1; i < slaveList.size(); i++) { // Copying a existing table row? CTRow ctRow = CTRow.Factory.newInstance(); ctRow.set(rowToDuplicate.getCtRow()); XWPFTableRow newRow = new XWPFTableRow(ctRow, xwpfTable); replaceCellTextStartWithIndex(xWord, rowToDuplicate, newRow, propertyName, i); xwpfTable.addRow(newRow); } replaceCellTextStartWithIndex(xWord, rowToDuplicate, rowToDuplicate, propertyName, 0); } else { tableRows = xwpfTable.getRows(); for (int r = 0; r < tableRows.size(); r++) { XWPFTableRow xwpfTableRow = tableRows.get(r); List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells(); for (XWPFTableCell xwpfTableCell : tableCells) { List<XWPFParagraph> paragraphs = xwpfTableCell.getParagraphs(); if (paragraphs != null) { for (XWPFParagraph para : paragraphs) { fillParagraph(xWord, para); } } } } } }
From source file:com.glodon.tika.SimpleTable.java
License:Apache License
/** * Create a table with some row and column styling. I "manually" add the * style name to the table, but don't check to see if the style actually * exists in the document. Since I'm creating it from scratch, it obviously * won't exist. When opened in MS Word, the table style becomes "Normal". * I manually set alternating row colors. This could be done using Themes, * but that's left as an exercise for the reader. The cells in the last * column of the table have 10pt. "Courier" font. * I make no claims that this is the "right" way to do it, but it worked * for me. Given the scarcity of XWPF examples, I thought this may prove * instructive and give you ideas for your own solutions. //from w w w . j av a 2 s . c o m * @throws Exception */ public static void createStyledTable() throws Exception { // Create a new document from scratch XWPFDocument doc = new XWPFDocument(); try { // -- OR -- // open an existing empty document with styles already defined //XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx")); // Create a new table with 6 rows and 3 columns int nRows = 6; int nCols = 3; XWPFTable table = doc.createTable(nRows, nCols); // Set the table style. If the style is not defined, the table style // will become "Normal". CTTblPr tblPr = table.getCTTbl().getTblPr(); CTString styleStr = tblPr.addNewTblStyle(); styleStr.setVal("StyledTable"); // Get a list of the rows in the table List<XWPFTableRow> rows = table.getRows(); int rowCt = 0; int colCt = 0; for (XWPFTableRow row : rows) { // get table row properties (trPr) CTTrPr trPr = row.getCtRow().addNewTrPr(); // set row height; units = twentieth of a point, 360 = 0.25" CTHeight ht = trPr.addNewTrHeight(); ht.setVal(BigInteger.valueOf(360)); // get the cells in this row List<XWPFTableCell> cells = row.getTableCells(); // add content to each cell for (XWPFTableCell cell : cells) { // get a table cell properties element (tcPr) CTTcPr tcpr = cell.getCTTc().addNewTcPr(); // set vertical alignment to "center" CTVerticalJc va = tcpr.addNewVAlign(); va.setVal(STVerticalJc.CENTER); // create cell color element CTShd ctshd = tcpr.addNewShd(); ctshd.setColor("auto"); ctshd.setVal(STShd.CLEAR); if (rowCt == 0) { // header row ctshd.setFill("A7BFDE"); } else if (rowCt % 2 == 0) { // even row ctshd.setFill("D3DFEE"); } else { // odd row ctshd.setFill("EDF2F8"); } // get 1st paragraph in cell's paragraph list XWPFParagraph para = cell.getParagraphs().get(0); // create a run to contain the content XWPFRun rh = para.createRun(); // style cell as desired if (colCt == nCols - 1) { // last column is 10pt Courier rh.setFontSize(10); rh.setFontFamily("Courier"); } if (rowCt == 0) { // header row rh.setText("header row, col " + colCt); rh.setBold(true); para.setAlignment(ParagraphAlignment.CENTER); } else { // other rows rh.setText("row " + rowCt + ", col " + colCt); para.setAlignment(ParagraphAlignment.LEFT); } colCt++; } // for cell colCt = 0; rowCt++; } // for row // write the file OutputStream out = new FileOutputStream("styledTable.docx"); try { doc.write(out); } finally { out.close(); } } finally { doc.close(); } }