List of usage examples for org.apache.poi.xwpf.usermodel XWPFTable getCTTbl
@Internal
public CTTbl getCTTbl()
From source file:AkashApplications.src.GenerateInvoice.java
public boolean printInvoice(boolean printCommand) throws FileNotFoundException, InvalidFormatException, IOException { XWPFDocument document = new XWPFDocument(); CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); CTPageMar pageMar = sectPr.addNewPgMar(); pageMar.setLeft(BigInteger.valueOf(720L)); pageMar.setTop(BigInteger.valueOf(460L)); pageMar.setRight(BigInteger.valueOf(720L)); pageMar.setBottom(BigInteger.valueOf(460L)); XWPFParagraph dateP = document.createParagraph(); XWPFRun dateRun = dateP.createRun(); dateRun.addBreak();//w w w . j a va 2s .co m dateRun.setText(copyType); dateRun.setFontSize(8); dateRun.setItalic(true); dateP.setAlignment(ParagraphAlignment.RIGHT); dateRun.addBreak(); XWPFParagraph title = document.createParagraph(); XWPFRun titleRun = title.createRun(); titleRun.addBreak(); titleRun.setText("Delivery Challan"); titleRun.setBold(true); title.setAlignment(ParagraphAlignment.CENTER); titleRun.addBreak(); //header table XWPFTable productTable = document.createTable(); productTable.setCellMargins(50, 50, 50, 50); productTable.getCTTbl().getTblPr().unsetTblBorders(); productTable.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(11000)); XWPFTableRow row1 = productTable.getRow(0); XWPFTableCell cellSellerBuyer = row1.getCell(0); cellSellerBuyer.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(5000)); CTTc ctTc = cellSellerBuyer.getCTTc(); CTTcPr tcPr = ctTc.getTcPr(); CTTcBorders border = tcPr.addNewTcBorders(); border.addNewBottom().setVal(STBorder.SINGLE); // border.addNewRight().setVal(STBorder.SINGLE); border.addNewLeft().setVal(STBorder.SINGLE); border.addNewTop().setVal(STBorder.SINGLE); XWPFParagraph seller = cellSellerBuyer.addParagraph(); XWPFRun sellerRun = seller.createRun(); seller.setAlignment(ParagraphAlignment.LEFT); sellerRun.setText("Buyer :"); sellerRun.addBreak(); String[] ar = buyerAddress.split("\n"); for (String s : ar) { sellerRun.setText(s); sellerRun.addBreak(); } XWPFTableCell cellProductDesc = row1.createCell(); cellProductDesc.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(6000)); ctTc = cellProductDesc.getCTTc(); tcPr = ctTc.getTcPr(); border = tcPr.addNewTcBorders(); border.addNewBottom().setVal(STBorder.SINGLE); border.addNewRight().setVal(STBorder.SINGLE); //border.addNewLeft().setVal(STBorder.SINGLE); border.addNewTop().setVal(STBorder.SINGLE); XWPFParagraph productDesc = cellProductDesc.addParagraph(); productDesc.setAlignment(ParagraphAlignment.LEFT); XWPFRun runInvoice = productDesc.createRun(); // if(table.getRowCount() == 1) // { // runInvoice.addTab(); // runInvoice.addTab(); // runInvoice.addTab(); // String imgLoc = "Barcodes/"+ dtm.getValueAt(0, 6) +".jpg"; // FileInputStream image = new FileInputStream(imgLoc); // runInvoice.addPicture(image, XWPFDocument.PICTURE_TYPE_JPEG,imgLoc, Units.toEMU(200), Units.toEMU(50)); // runInvoice.addBreak(); // runInvoice.addBreak(); // } runInvoice.setText("Delivery Note : "); runInvoice.addTab(); runInvoice.setText(invoiceNote); runInvoice.addBreak(); runInvoice.addBreak(); runInvoice.addBreak(); runInvoice.setText("Date : "); runInvoice.addTab(); runInvoice.setText(date); runInvoice.addBreak(); runInvoice.addBreak(); //document.createParagraph().createRun().addBreak(); XWPFTable productDetails = document.createTable(); productDetails.setCellMargins(50, 50, 50, 50); //productDetails.getCTTbl().getTblPr().addNewJc().setVal(STJc.RIGHT); productDetails.getCTTbl().getTblPr().unsetTblBorders(); productDetails.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(11000)); XWPFTableRow pDetailHeader = productDetails.getRow(0); XWPFTableCell header0 = pDetailHeader.getCell(0); header0.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1000)); XWPFParagraph headerText = header0.addParagraph(); XWPFRun run = headerText.createRun(); run.setText("Sl. No."); run.setBold(true); ctTc = header0.getCTTc(); tcPr = ctTc.addNewTcPr(); border = tcPr.addNewTcBorders(); border.addNewBottom().setVal(STBorder.SINGLE); border.addNewRight().setVal(STBorder.SINGLE); border.addNewLeft().setVal(STBorder.SINGLE); border.addNewTop().setVal(STBorder.SINGLE); XWPFTableCell header1 = pDetailHeader.createCell(); header1.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(8000)); XWPFParagraph headerText1 = header1.addParagraph(); XWPFRun run1 = headerText1.createRun(); run1.setText("Description of Goods"); run1.setBold(true); ctTc = header1.getCTTc(); tcPr = ctTc.addNewTcPr(); border = tcPr.addNewTcBorders(); border.addNewBottom().setVal(STBorder.SINGLE); border.addNewRight().setVal(STBorder.SINGLE); border.addNewLeft().setVal(STBorder.SINGLE); border.addNewTop().setVal(STBorder.SINGLE); XWPFTableCell header4 = pDetailHeader.createCell(); header4.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000)); XWPFParagraph headerText4 = header4.addParagraph(); XWPFRun run4 = headerText4.createRun(); run4.setText("Quantity"); run4.setBold(true); ctTc = header4.getCTTc(); tcPr = ctTc.addNewTcPr(); border = tcPr.addNewTcBorders(); border.addNewBottom().setVal(STBorder.SINGLE); border.addNewRight().setVal(STBorder.SINGLE); border.addNewLeft().setVal(STBorder.SINGLE); border.addNewTop().setVal(STBorder.SINGLE); for (int i = 0; i < 1; i++) { XWPFTableRow fTableRow = productDetails.createRow(); for (int j = 0; j < 4; j++) { XWPFTableCell cell = fTableRow.getCell(j); try { ctTc = cell.getCTTc(); tcPr = ctTc.addNewTcPr(); border = tcPr.addNewTcBorders(); //border.addNewBottom().setVal(STBorder.SINGLE); border.addNewRight().setVal(STBorder.SINGLE); border.addNewLeft().setVal(STBorder.SINGLE); if (i == 0) border.addNewTop().setVal(STBorder.SINGLE); } catch (Exception e) { System.err.println(e.getMessage()); } switch (j) { case 0: cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1000)); break; case 1: cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(8000)); break; case 2: cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000)); break; } } } int qty = 0; for (int i = 0; i < table.getRowCount(); i++) { XWPFTableRow fTableRow = productDetails.createRow(); for (int j = 0; j < table.getColumnCount() - 1; j++) { XWPFTableCell cell = fTableRow.getCell(j); try { ctTc = cell.getCTTc(); tcPr = ctTc.addNewTcPr(); border = tcPr.addNewTcBorders(); if (i == table.getRowCount() - 1) border.addNewBottom().setVal(STBorder.SINGLE); border.addNewRight().setVal(STBorder.SINGLE); border.addNewLeft().setVal(STBorder.SINGLE); // border.addNewTop().setVal(STBorder.SINGLE); } catch (Exception e) { System.err.println(e.getMessage()); } switch (j) { case 0: cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1000)); cell.setText(String.valueOf(dtm.getValueAt(i, j))); break; case 1: cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(8000)); cell.setText(String.valueOf(dtm.getValueAt(i, j))); break; case 2: cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000)); cell.setText(String.valueOf(dtm.getValueAt(i, j))); qty += Integer.parseInt(String.valueOf(dtm.getValueAt(i, j))); break; } } } XWPFTableRow finalRow = productDetails.createRow(); XWPFTableCell fc0 = finalRow.getCell(0); fc0.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1000)); XWPFTableCell fc3 = finalRow.getCell(1); fc3.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(8000)); XWPFParagraph fp1 = fc3.addParagraph(); XWPFRun fRun1 = fp1.createRun(); fRun1.setBold(true); fRun1.setText("TOTAL"); XWPFTableCell fc4 = finalRow.getCell(2); fc4.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000)); XWPFParagraph fp2 = fc4.addParagraph(); XWPFRun fRun2 = fp2.createRun(); fRun2.setBold(true); fRun2.setText(String.valueOf(qty)); ctTc = fc0.getCTTc(); tcPr = ctTc.getTcPr(); border = tcPr.addNewTcBorders(); border.addNewBottom().setVal(STBorder.SINGLE); //border.addNewRight().setVal(STBorder.SINGLE); border.addNewLeft().setVal(STBorder.SINGLE); border.addNewTop().setVal(STBorder.SINGLE); ctTc = fc3.getCTTc(); tcPr = ctTc.getTcPr(); border = tcPr.addNewTcBorders(); border.addNewBottom().setVal(STBorder.SINGLE); border.addNewRight().setVal(STBorder.SINGLE); //border.addNewLeft().setVal(STBorder.SINGLE); border.addNewTop().setVal(STBorder.SINGLE); ctTc = fc4.getCTTc(); tcPr = ctTc.getTcPr(); border = tcPr.addNewTcBorders(); border.addNewBottom().setVal(STBorder.SINGLE); border.addNewRight().setVal(STBorder.SINGLE); border.addNewLeft().setVal(STBorder.SINGLE); border.addNewTop().setVal(STBorder.SINGLE); XWPFParagraph netPMethod = document.createParagraph(); XWPFRun pMethodRun = netPMethod.createRun(); pMethodRun.addBreak(); pMethodRun.addBreak(); pMethodRun.setItalic(true); pMethodRun.setFontSize(10); pMethodRun.setText("NB. - Goods sold to the above buyer is in " + paymentType + "."); try { File f = new File(filePath); if (!f.exists()) { f.mkdir(); } FileOutputStream outputStream = new FileOutputStream(filePath + File.separator + "Challan" + invoiceNote.replace("/", "_") + copyType.replace("/", "_") + ".docx"); document.write(outputStream); outputStream.close(); new InvoiceNoteManager().setProperty(); if (printCommand) printDocument(); } catch (Exception e) { System.err.println(e.getMessage()); } return true; }
From source file:br.com.techne.gluonsoft.eowexport.builder.WordBuilder.java
License:Apache License
/** * mtodo cria bytes de documento Excel/* www. j a va 2s . 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:com.bxf.hradmin.testgen.service.impl.DocxTestGenerator.java
License:Open Source License
private void setBorders(XWPFTable table) { // /*from ww w. j a va 2 s . c o m*/ CTTblPr tblPr = table.getCTTbl().getTblPr(); CTTblBorders borders = tblPr.addNewTblBorders(); borders.addNewBottom().setVal(STBorder.SINGLE); borders.addNewLeft().setVal(STBorder.SINGLE); borders.addNewRight().setVal(STBorder.SINGLE); borders.addNewTop().setVal(STBorder.SINGLE); borders.addNewInsideH().setVal(STBorder.SINGLE); borders.addNewInsideV().setVal(STBorder.SINGLE); }
From source file:com.crimelab.service.PolygraphServiceImpl.java
@Override public XWPFDocument create(Polygraph polygraph, HttpSession session) { XWPFDocument document = null;/*from w w w . j a va 2 s . co m*/ //Insert into dbase // polygraphDAO.polygraphInfo(polygraph); try { //Blank Document InputStream inpDocx = session.getServletContext() .getResourceAsStream("/WEB-INF/templates/Polygraph.docx"); document = new XWPFDocument(inpDocx); //create Paragraph XWPFParagraph subjectNo = document.createParagraph(); XWPFRun r1 = subjectNo.createRun(); r1.setText("POLYGRAPH SUBJECT NO: " + polygraph.getSubjectNo()); subjectNo.setAlignment(ParagraphAlignment.CENTER); r1.setBold(true); ; //create table XWPFTable table = document.createTable(); //width CTTbl tableFix = table.getCTTbl(); CTTblPr pr = tableFix.getTblPr(); CTTblWidth tblW = pr.getTblW(); tblW.setW(BigInteger.valueOf(4800)); tblW.setType(STTblWidth.PCT); pr.setTblW(tblW); tableFix.setTblPr(pr); //create first row XWPFTableRow tableRowOne = table.getRow(0); XWPFTableCell headerCell = tableRowOne.getCell(0); XWPFParagraph headerParagraph = headerCell.getParagraphs().get(0); XWPFRun hRun = headerParagraph.createRun(); headerCell.setColor("CDCDB4"); hRun.setText("PERSONAL INFORMATION"); tableRowOne.addNewTableCell().setText(null); headerParagraph.setAlignment(ParagraphAlignment.CENTER); XWPFTableCell photoHeaderCell = tableRowOne.getCell(1); XWPFParagraph photoHeaderParagraph = photoHeaderCell.getParagraphs().get(0); XWPFRun pRun = photoHeaderParagraph.createRun(); photoHeaderCell.setColor("CDCDB4"); pRun.setText("PHOTO"); photoHeaderParagraph.setAlignment(ParagraphAlignment.CENTER); XWPFTableRow tableRowTwo = table.createRow(); XWPFTableCell cell = tableRowTwo.getCell(0); XWPFParagraph personalInfo = cell.getParagraphs().get(0); XWPFRun r2 = personalInfo.createRun(); r2.setText("Name"); r2.addTab(); r2.addTab(); r2.setText(": " + polygraph.getName()); r2.addBreak(); r2.setText("Gender"); r2.addTab(); r2.addTab(); r2.setText(": " + polygraph.getGender()); r2.addBreak(); r2.setText("Age"); r2.addTab(); r2.addTab(); r2.setText(": " + polygraph.getAge()); r2.addBreak(); r2.setText("Date of Birth"); r2.addTab(); r2.setText(": " + polygraph.getBirthdate()); r2.addBreak(); r2.setText("Civil Status"); r2.addTab(); r2.setText(": " + polygraph.getCivilStatus()); r2.addBreak(); r2.setText("ID Presented"); r2.addTab(); r2.setText(": " + polygraph.getIdPresented()); r2.addBreak(); r2.setText("Address"); r2.addTab(); r2.setText(": " + polygraph.getAddress()); //Adding the picture XWPFTableCell pictureCell = tableRowTwo.getCell(1); XWPFParagraph pictureHolder = pictureCell.getParagraphs().get(0); XWPFRun pictureRun = pictureHolder.createRun(); FileInputStream getPhoto = new FileInputStream(polygraph.getPhotoLocation()); FileInputStream getImage = new FileInputStream(polygraph.getPhotoLocation()); ImageInputStream imageInput = ImageIO.createImageInputStream(getPhoto); BufferedImage bi = ImageIO.read(imageInput); pictureHolder.setAlignment(ParagraphAlignment.RIGHT); pictureRun.addPicture(getImage, XWPFDocument.PICTURE_TYPE_JPEG, null, Units.toEMU(120), Units.toEMU(120)); XWPFParagraph spacing = document.createParagraph(); XWPFRun spacingRun = spacing.createRun(); //create table XWPFTable otherTable = document.createTable(); //width CTTbl tableFixTwo = otherTable.getCTTbl(); CTTblPr prTwo = tableFixTwo.getTblPr(); CTTblWidth tblWTwo = prTwo.getTblW(); tblWTwo.setW(BigInteger.valueOf(4800)); tblWTwo.setType(STTblWidth.PCT); prTwo.setTblW(tblWTwo); tableFixTwo.setTblPr(prTwo); XWPFTableRow examInfoHeader = otherTable.createRow(); XWPFTableCell cellInfo = examInfoHeader.getCell(0); XWPFParagraph examInfo = cellInfo.getParagraphs().get(0); XWPFRun r3 = examInfo.createRun(); cellInfo.setColor("CDCDB4"); r3.setText("EXAM INFORMATION"); examInfo.setAlignment(ParagraphAlignment.CENTER); XWPFTableRow examInfoRow = otherTable.createRow(); XWPFTableCell cellRowInfo = examInfoRow.getCell(0); XWPFParagraph examInfoRowP = cellRowInfo.getParagraphs().get(0); XWPFRun examRun = examInfoRowP.createRun(); examRun.setText("Case Number"); examRun.addTab(); examRun.addTab(); examRun.setText(": " + polygraph.getCaseNo()); examRun.addBreak(); examRun.setText("Requesting Party"); examRun.addTab(); examRun.setText(": " + polygraph.getRequestingParty()); examRun.addBreak(); examRun.setText("Time/Date Received"); examRun.addTab(); examRun.setText(": " + polygraph.getTimeDateReceived()); examRun.addBreak(); examRun.setText("Nature of Case"); examRun.addTab(); examRun.addTab(); examRun.setText(": " + polygraph.getNatureOfCase()); examRun.addBreak(); examRun.setText("Exam Location"); examRun.addTab(); examRun.addTab(); examRun.setText(": " + polygraph.getExamLocation()); examRun.addBreak(); examRun.setText("Exam Date"); examRun.addTab(); examRun.addTab(); examRun.setText(": " + polygraph.getExamDate()); otherTable.removeRow(0); XWPFParagraph purposeOfExamination = document.createParagraph(); XWPFRun r4 = purposeOfExamination.createRun(); r4.setUnderline(UnderlinePatterns.SINGLE); r4.addBreak(); r4.setText("SECTION 1: PURPOSE OF EXAMINATION"); r4.addTab(); r4.addTab(); r4.addTab(); r4.addTab(); r4.addTab(); r4.addTab(); r4.addTab(); r4.addTab(); r4.addTab(); XWPFParagraph purposeOfExaminationContents = document.createParagraph(); XWPFRun r4Contents = purposeOfExaminationContents.createRun(); r4Contents.setText(polygraph.getPurpose()); XWPFParagraph preTestInterview = document.createParagraph(); XWPFRun r5 = preTestInterview.createRun(); r5.setUnderline(UnderlinePatterns.SINGLE); r5.setText("SECTION 2: PRE-TEST INTERVIEW"); r5.addTab(); r5.addTab(); r5.addTab(); r5.addTab(); r5.addTab(); r5.addTab(); r5.addTab(); r5.addTab(); r5.addTab(); XWPFParagraph preTestInterviewContents = document.createParagraph(); XWPFRun r5Contents = preTestInterviewContents.createRun(); r5Contents.setText(polygraph.getPreTest()); XWPFParagraph inTestPhase = document.createParagraph(); XWPFRun r6 = inTestPhase.createRun(); r6.setUnderline(UnderlinePatterns.SINGLE); r6.setText("SECTION 3: IN-TEST PHASE"); r6.addTab(); r6.addTab(); r6.addTab(); r6.addTab(); r6.addTab(); r6.addTab(); r6.addTab(); r6.addTab(); r6.addTab(); r6.addTab(); XWPFParagraph inTestPhaseContents = document.createParagraph(); XWPFRun r6Contents = inTestPhaseContents.createRun(); r6Contents.setText(polygraph.getInTest()); XWPFParagraph result = document.createParagraph(); XWPFRun r7 = result.createRun(); r7.setUnderline(UnderlinePatterns.SINGLE); r7.setText("SECTION 4: RESULT"); r7.addTab(); r7.addTab(); r7.addTab(); r7.addTab(); r7.addTab(); r7.addTab(); r7.addTab(); r7.addTab(); r7.addTab(); r7.addTab(); r7.addTab(); XWPFParagraph resultContents = document.createParagraph(); XWPFRun r7Contents = resultContents.createRun(); r7Contents.setText(polygraph.getResult()); XWPFParagraph postTestInterview = document.createParagraph(); XWPFRun r8 = postTestInterview.createRun(); r8.setUnderline(UnderlinePatterns.SINGLE); r8.setText("SECTION 5: POST-TEST INTERVIEW"); r8.addTab(); r8.addTab(); r8.addTab(); r8.addTab(); r8.addTab(); r8.addTab(); r8.addTab(); r8.addTab(); r8.addTab(); XWPFParagraph postTestInterviewContents = document.createParagraph(); XWPFRun r8Contents = postTestInterviewContents.createRun(); r8Contents.setText(polygraph.getPostTest()); XWPFParagraph remarks = document.createParagraph(); XWPFRun r9 = remarks.createRun(); r9.setUnderline(UnderlinePatterns.SINGLE); r9.setText("REMARKS:"); r9.addTab(); r9.addTab(); r9.addTab(); r9.addTab(); r9.addTab(); r9.addTab(); r9.addTab(); r9.addTab(); r9.addTab(); r9.addTab(); r9.addTab(); r9.addTab(); XWPFParagraph remarksContents = document.createParagraph(); XWPFRun r9Contents = remarksContents.createRun(); r9Contents.setText(polygraph.getRemarks()); XWPFParagraph timeDateCompleted = document.createParagraph(); XWPFRun r10 = timeDateCompleted.createRun(); r10.setUnderline(UnderlinePatterns.SINGLE); r10.setText("TIME AND DATE COMPLETED:"); r10.addTab(); r10.addTab(); r10.addTab(); r10.addTab(); r10.addTab(); r10.addTab(); r10.addTab(); r10.addTab(); r10.addTab(); r10.addTab(); XWPFParagraph timeDateCompletedContents = document.createParagraph(); XWPFRun r10Contents = timeDateCompletedContents.createRun(); r10Contents.setText(polygraph.getTimeDateCompleted()); XWPFParagraph examinedBy = document.createParagraph(); XWPFRun r11 = examinedBy.createRun(); r11.setUnderline(UnderlinePatterns.SINGLE); r11.setText("EXAMINED BY:"); r11.addTab(); r11.addTab(); r11.addTab(); r11.addTab(); r11.addTab(); r11.addTab(); r11.addTab(); r11.addTab(); r11.addTab(); r11.addTab(); r11.addTab(); r11.addTab(); XWPFParagraph examinedByContents = document.createParagraph(); XWPFRun r11Contents = examinedByContents.createRun(); r11Contents.setText(polygraph.getExaminerName()); r11Contents.addBreak(); r11Contents.setText(polygraph.getExaminerRank()); r11Contents.addBreak(); r11Contents.setText(polygraph.getExaminerPosition()); XWPFParagraph approvedBy = document.createParagraph(); XWPFRun r12 = approvedBy.createRun(); r12.setUnderline(UnderlinePatterns.SINGLE); r12.setText("APPROVED BY:"); r12.addTab(); r12.addTab(); r12.addTab(); r12.addTab(); r12.addTab(); r12.addTab(); r12.addTab(); r12.addTab(); r12.addTab(); r12.addTab(); r12.addTab(); r12.addTab(); XWPFParagraph approvedByContents = document.createParagraph(); XWPFRun r12Contents = approvedByContents.createRun(); // r12Contents.setText(polygraph.getApprovedName()); // r12Contents.addBreak(); // r12Contents.setText(polygraph.getApprovedRank()); // r12Contents.addBreak(); // r12Contents.setText(polygraph.getApprovedPosition()); // r12Contents.addBreak(); XWPFParagraph notedBy = document.createParagraph(); XWPFRun r13 = notedBy.createRun(); r13.setUnderline(UnderlinePatterns.SINGLE); r13.setText("NOTED BY:"); r13.addTab(); r13.addTab(); r13.addTab(); r13.addTab(); r13.addTab(); r13.addTab(); r13.addTab(); r13.addTab(); r13.addTab(); r13.addTab(); r13.addTab(); r13.addTab(); XWPFParagraph notedByContents = document.createParagraph(); XWPFRun r13Contents = notedByContents.createRun(); r13Contents.setText(polygraph.getNotedName()); r13Contents.addBreak(); r13Contents.setText(polygraph.getNotedRank()); r13Contents.addBreak(); r13Contents.setText(polygraph.getNotedPosition()); r13Contents.addBreak(); table.setInsideVBorder(XWPFTable.XWPFBorderType.NIL, 0, 0, "white"); document.getXWPFDocument(); } catch (Exception e) { e.printStackTrace(); } return document; }
From source file:com.deepoove.poi.policy.SimpleTableRenderPolicy.java
License:Apache License
@Override public void render(ElementTemplate runTemplateP, Object data, XWPFTemplate template) { NiceXWPFDocument doc = template.getXWPFDocument(); RunTemplate runTemplate = (RunTemplate) runTemplateP; XWPFRun run = runTemplate.getRun();// ww w . j a v a 2 s . c om if (null == data) return; TableRenderData tableData = (TableRenderData) data; List<RenderData> headers = tableData.getHeaders(); List<Object> datas = tableData.getDatas(); if (datas == null || datas.isEmpty()) { //XWPFTable table = doc.createTable(2, headers.size()); XWPFTable table = doc.insertNewTable(run, 2, headers.size()); if (null == table) { logger.warn("cannot insert table."); return; } CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW(); width.setW(BigInteger.valueOf(tableData.getWidth())); //width.setType(STTblWidth.); createHeader(table, headers); doc.mergeCellsHorizonal(table, 1, 0, 2); XWPFTableCell cell = table.getRow(1).getCell(0); cell.setText(tableData.getNoDatadesc()); } else { int size = datas.size(); XWPFTable table = doc.insertNewTable(run, 1 + size, headers.size()); CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW(); width.setW(BigInteger.valueOf(tableData.getWidth())); createHeader(table, headers); int i = 1; for (Object obj : datas) { String str = obj.toString(); String[] split = str.split(";"); int length = split.length; for (int m = 0; m < length; m++) { table.getRow(i).getCell(m).setText(split[m]); } i++; } } //doc.getDocument().getBody().insertNewTbl(arg0) runTemplate.getRun().setText("", 0); //XWPFTable table = new XWPFTable(doc.getDocument().getBody().addNewTbl(), doc, 2, headers.size()); //doc.insertTable(0, table); // XmlCursor newCursor = // doc.getDocument().getBody().getPArray(1).newCursor(); // XWPFParagraph paragraph = run.getParagraph(); // XmlCursor cursor = paragraph.getCTP().newCursor(); // XWPFTable t1 = doc.insertNewTbl(cursor); // t1.getRow(0).getCell(0).setText("are you sure?"); // XWPFTableCell cell = t1.getRow(0).addNewTableCell(); // cell.setText("what are you?"); //XWPFTable tableOne = run.getParagraph().getBody().insertNewTbl(c); // run.getCTR().newCursor(); }
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. /*from w w w. j a v a2s .co 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.vaadin.gui.mswordGenerator.FocMSWordLayout.java
License:Apache License
public void applyBorderAttribute(XWPFTable newTable) { String borderAtt = getXmlAttribute().getValue(FXML.ATT_BORDER); if (borderAtt != null && (borderAtt.toLowerCase().equals("true") || borderAtt.equals("1"))) { } else {/*from w w w.j av a 2 s . co m*/ newTable.getCTTbl().getTblPr().unsetTblBorders(); newTable.setInsideHBorder(XWPFBorderType.NONE, 0, 0, null); newTable.setInsideVBorder(XWPFBorderType.NONE, 0, 0, null); newTable.setRowBandSize(200); } newTable.setCellMargins(0, 0, 0, 0); }
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. // 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(); } }
From source file:com.hotaviano.tableexporter.docx.DOCXExporter.java
License:Open Source License
@Override public byte[] export(String htmlTable) throws DocumentException { XWPFDocument doc = new XWPFDocument(); Document document = createDocument(htmlTable); int cols = getHeaderSize(document); int rows = getHeaderSize(document) + 1; XWPFTable table = doc.createTable(rows, cols); createHeaderTable(table, document);/* w w w.j a va2 s . com*/ createBodyTable(table, document); CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW(); width.setType(STTblWidth.DXA); width.setW(BigInteger.valueOf(9072)); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { doc.write(out); out.close(); } catch (IOException ex) { throw new DocumentException(ex); } return out.toByteArray(); }
From source file:csv2docxconverter.DocumentGenerator.java
/** * Generate DocX element from list of the rows containing account information * @param columnNames a list of columns for data parsing * @param contents list of data rows for parsing * @return an XWPFDocument representing a DocX file *///from w w w . jav a 2s .co m public XWPFDocument generateDocx(String[] columnNames, List contents) { XWPFDocument document = new XWPFDocument(); // create title XWPFParagraph title = document.createParagraph(); title.setAlignment(ParagraphAlignment.CENTER); XWPFRun titleRun = title.createRun(); titleRun.setText("G SUITE created accounts"); titleRun.setFontSize(18); for (int k = 0; k < contents.size(); k++) { List tableContent = (List) contents.get(k); // create title title = document.createParagraph(); title.setAlignment(ParagraphAlignment.CENTER); titleRun = title.createRun(); titleRun.setText("Table " + (k + 1)); titleRun.setFontSize(18); // create account table XWPFTable table = document.createTable(); // set "justified" alignment table.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(10000)); //create header for table setHeader(table, columnNames); // if account list is empty if (tableContent == null || tableContent.size() == 0) { // add empty row to the table XWPFTableRow emptyRow = table.createRow(); } else { String[] headerRow = null; // create rows in table for (int i = 0; i < tableContent.size(); i++) { if (i == 0) { headerRow = (String[]) tableContent.get(i); continue; } String[] csvRow = (String[]) tableContent.get(i); XWPFTableRow row = table.createRow(); //create cells in a row for (int j = 0; j < columnNames.length; j++) { int number = getColumnNumber(columnNames[j], headerRow); XWPFTableCell cell = row.getCell(j); if (cell != null) { XWPFRun run = setBodyCell(cell); if (number >= 0 && number < csvRow.length) { run.setText(csvRow[number]); } } } } } } return document; }