List of usage examples for org.apache.poi.xwpf.usermodel XWPFTable setWidth
public void setWidth(String widthValue)
From source file:com.siemens.sw360.licenseinfo.outputGenerators.DocxUtils.java
License:Open Source License
private static void styleTable(XWPFTable table) { table.setRowBandSize(1);//from w w w. java 2s. c o m table.setWidth(1); table.setColBandSize(1); table.setCellMargins(1, 1, 100, 30); }
From source file:eremeykin.pete.reports.ui.ReportAction.java
@Override public void actionPerformed(ActionEvent e) { resultChanged(null);//w ww . ja v a 2s . co m if (model == null) { return; } XWPFDocument doc = new XWPFDocument(); XWPFParagraph p1 = doc.createParagraph(); p1.setAlignment(ParagraphAlignment.CENTER); p1.setVerticalAlignment(TextAlignment.TOP); XWPFRun r1 = p1.createRun(); r1.setBold(true); r1.setText(""); r1.setBold(true); r1.setFontFamily("Times New Roman"); r1.setFontSize(24); r1.setTextPosition(10); XWPFParagraph p2 = doc.createParagraph(); p2.setAlignment(ParagraphAlignment.LEFT); p2.setVerticalAlignment(TextAlignment.CENTER); XWPFRun r2 = p2.createRun(); r2.setText(" ? : "); r2.setBold(false); r2.setFontFamily("Times New Roman"); r2.setFontSize(14); r2.setTextPosition(10); XWPFTable table = doc.createTable(1, 2); table.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(9000)); ModelParameter root = model.getRoot(); int row = 1; Map.Entry<ModelParameter, Integer> kv = model.getParameterAndLevelByID(root, 0); ModelParameter parameter = kv.getKey(); Integer level = kv.getValue(); ArrayList<Integer> ids = new ArrayList(model.asMap().keySet()); Collections.sort(ids); for (Integer each : ids) { table.createRow(); String text = ""; kv = model.getParameterAndLevelByID(root, each); parameter = kv.getKey(); level = kv.getValue(); for (int c = 0; c < level; c++) { text += " "; } table.getRow(row - 1).getCell(0).setText(text + parameter.toString()); table.getRow(row - 1).getCell(1).setText(parameter.getValue()); row++; } table.setWidth(80); XWPFParagraph p3 = doc.createParagraph(); p3.setAlignment(ParagraphAlignment.LEFT); p3.setVerticalAlignment(TextAlignment.CENTER); XWPFRun r3 = p3.createRun(); r3.addBreak(); r3.setText("\n : "); r3.setBold(false); r3.setFontFamily("Times New Roman"); r3.setFontSize(14); File uPlotFile = new File(WorkspaceManager.INSTANCE.getWorkspace().getAbsolutePath() + "/uplot.png"); try { byte[] picbytes = IOUtils.toByteArray(new FileInputStream(uPlotFile)); doc.addPictureData(picbytes, XWPFDocument.PICTURE_TYPE_PNG); XWPFRun pr = doc.createParagraph().createRun(); pr.addPicture(new FileInputStream(uPlotFile), Document.PICTURE_TYPE_PNG, "plot.png", Units.toEMU(450), Units.toEMU(337)); pr.addCarriageReturn(); pr.addBreak(BreakType.PAGE); pr.addBreak(BreakType.TEXT_WRAPPING); } catch (Exception ex) { Exceptions.printStackTrace(ex); } XWPFParagraph p4 = doc.createParagraph(); p4.setAlignment(ParagraphAlignment.LEFT); p4.setVerticalAlignment(TextAlignment.CENTER); XWPFRun r4 = p4.createRun(); r4.addBreak(); r4.setText("\n ?: "); r4.setBold(false); r4.setFontFamily("Times New Roman"); r4.setFontSize(14); File sPlotFile = new File(WorkspaceManager.INSTANCE.getWorkspace().getAbsolutePath() + "/splot.png"); try { byte[] picbytes = IOUtils.toByteArray(new FileInputStream(sPlotFile)); doc.addPictureData(picbytes, XWPFDocument.PICTURE_TYPE_PNG); XWPFParagraph pp = doc.createParagraph(); pp.createRun().addPicture(new FileInputStream(sPlotFile), Document.PICTURE_TYPE_PNG, "plot.png", Units.toEMU(450), Units.toEMU(337)); } catch (Exception ex) { Exceptions.printStackTrace(ex); } File reportFile = new File("report.docx"); try (FileOutputStream out = new FileOutputStream(reportFile)) { doc.write(out); if (Desktop.isDesktopSupported()) { Desktop.getDesktop().edit(reportFile); } else { } } catch (IOException ex) { Exceptions.printStackTrace(ex); } }
From source file:service.Read_Write_File.java
public void SetDOCX_DT(List<Bordereau> bordereaus, List<CorpDetat> corpDetats, List<ArticleChp1> articlechp1s, List<ArticleChp2> articlechp2s) throws Exception { // XWPFDocument document = new XWPFDocument(); //creete a new documment XWPFDocument doc = new XWPFDocument(); //FileInputStream is = new FileInputStream("D:\\a.jpg"); // doc.addPictureData(IOUtils.toByteArray(fs), doc.PICTURE_TYPE_JPEG); //doc.create(id,doc.getNextPicNameNumber(doc.PICTURE_TYPE_JPEG), 64, 64); XWPFParagraph para2 = doc.createParagraph(); para2.setAlignment(ParagraphAlignment.CENTER); XWPFRun run = para2.createRun();//w ww . ja v a2s. co m run.setBold(true); run.setFontSize(40); // run.addPicture(pic, doc.PICTURE_TYPE_JPEG, "3", 0, 0); // run.addPicture(fs, doc.PICTURE_TYPE_JPEG, blipId, 100, 100); // run.addPicture(is,2, "kamal", 20, 20); //run.addPicture(IOUtils.toByteArray(is), doc.PICTURE_TYPE_JPEG); run.setText("\n \t Descriptif technique \n "); // pour article d projet XWPFParagraph para3 = doc.createParagraph(); // para2.setAlignment(ParagraphAlignment.CENTER); run = para3.createRun(); run.setText(" I- article de votre projet :"); run.setFontSize(18); run.setBold(true); XWPFParagraph para4 = doc.createParagraph();// pour les article chp 1 run = para4.createRun(); run.setText(" I-1 article chapitre1 : \n"); run.setFontSize(14); run.setBold(true); run.addBreak(); int i = 1; for (ArticleChp1 loadarticlechp1 : articlechp1s) {// kan3amar text1 dyal articlchp1 run = para4.createRun(); run.setText(" " + i + ")- " + loadarticlechp1.getTitre()); run.addBreak(); i++; } XWPFParagraph para5 = doc.createParagraph(); // pour les articles chp2 run = para5.createRun(); run.setText(" I-2 article chapitre2 : \n"); run.setFontSize(14); run.setBold(true); run.addBreak(); i = 1; for (ArticleChp2 loadarticlechp2 : articlechp2s) {// text2 dyla articlechp2 run = para5.createRun(); run.setText(" " + i + ")- " + loadarticlechp2.getTitre()); run.addBreak(); i++; } // corpdetat de projet XWPFParagraph para6 = doc.createParagraph(); // para2.setAlignment(ParagraphAlignment.CENTER); run = para6.createRun(); run.setText("\t II- corps dtat :"); run.setFontSize(18); run.setBold(true); run.addBreak(); // run.setColor("#999"); i = 1; // bouclage pourles coprs et posts dyal kola corps for (CorpDetat loaDetat : corpDetats) { run = para6.createRun(); run.setText(" II-" + i + "- " + loaDetat.getTitre() + " : "); run.setFontSize(16); run.addBreak(); int j = 1; for (Post loadpost : loaDetat.getPosts()) { run = para6.createRun(); run.setText(" " + i + "-" + j + ") " + loadpost.getTitre() + " "); run.addBreak(); j++; } i++; } // // bordeauraux XWPFParagraph para = doc.createParagraph(); run = para.createRun(); run.setBold(true); run.setFontSize(18); run.setText(" bordereau de prix "); run.addBreak(); run = para.createRun(); //Creates a table XWPFTable tab = doc.createTable(); tab.setWidth(500); tab.setCellMargins(50, 200, 50, 200); XWPFTableRow row1 = tab.getRow(0); row1.getCell(0).setText("designation"); row1.getCell(0).setColor("FF9900"); row1.addNewTableCell().setText("Unite"); row1.getCell(1).setColor("FF9900"); row1.addNewTableCell().setText("Quantite"); row1.getCell(2).setColor("FF9900"); row1.addNewTableCell().setText("prix"); row1.getCell(3).setColor("FF9900"); row1.addNewTableCell().setText("montant"); row1.getCell(4).setColor("FF9900"); i = 1; for (Bordereau loadbordereau : bordereaus) { XWPFTableRow loadRow = null; loadRow = tab.createRow(); loadRow.getCell(0).setText(loadbordereau.getDesignation() + ""); loadRow.getCell(1).setText(loadbordereau.getUnite()); if (loadbordereau.getQuanite() == 0) { loadRow.getCell(2).setText(""); } else { loadRow.getCell(2).setText(loadbordereau.getQuanite() + ""); } if (loadbordereau.getPrix() == 0) { } else { loadRow.getCell(3).setText(loadbordereau.getPrix() + ""); } if (loadbordereau.getMontant() == 0.0) { } else { loadRow.getCell(4).setText(loadbordereau.getMontant() + ""); } } XWPFTableRow row3 = null; row3 = tab.createRow(); row3.getCell(0).setText("TVA"); row3.getCell(0).setColor("FF9900"); XWPFTableRow row4 = null; row4 = tab.createRow(); row4.getCell(0).setText("HTTC"); row4.getCell(0).setColor("FF9900"); try { doc.write(new FileOutputStream("D:\\test2.docx")); // InputStream pica = new FileInputStream("D:\\test2.docx"); //FileInputStream fis = new FileInputStream("D:\\test2.docx"); if (Desktop.isDesktopSupported()) { Desktop.getDesktop().open(new File("D:\\test2.docx")); } } catch (IOException ex) { } }