Example usage for org.apache.poi.xwpf.usermodel XWPFRun addBreak

List of usage examples for org.apache.poi.xwpf.usermodel XWPFRun addBreak

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel XWPFRun addBreak.

Prototype

public void addBreak() 

Source Link

Document

Specifies that a break shall be placed at the current location in the run content.

Usage

From source file:pe.gob.onpe.rae.controller.registro.registroController.java

@RequestMapping(value = "generateFVDoc/{codExpediente}", method = RequestMethod.GET)
public void generateFVDoc(HttpServletRequest request, @PathVariable("codExpediente") int codExpediente,
        HttpServletResponse response) {/*from ww w  .j av  a  2  s  . co m*/
    try {
        ServletContext sc = request.getSession().getServletContext();

        Expediente expediente = new Expediente(codExpediente);
        expediente = expedienteDAO.find(expediente);

        Ambito amb = new Ambito(expediente.getAmbito().getId());
        amb = ambitoDAO.find(amb);

        int totalElectoresRemitidos = expedientePadronDAO.getCountByExpediente(expediente);
        int totalElectoresIncorporados = expedientePadronDAO.getCountByExpedienteAndEstado(expediente,
                Parametros.ESTADO_ELECTOR_ACTIVO);

        JsonParser jsonParser = new JsonParser();
        JsonObject jsonObject = (JsonObject) jsonParser.parse(amb.getInformacion());
        String nombre = jsonObject.get("nombres").toString() + " "
                + jsonObject.get("apellidoPaterno").toString() + " "
                + jsonObject.get("apellidoMaterno").toString();

        InputStream is = registroController.class.getResourceAsStream("/ejemplo.docx");
        XWPFDocument document = new XWPFDocument(is);

        XWPFHeaderFooterPolicy policy = document.getHeaderFooterPolicy();
        if (policy == null) {
            CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
            policy = new XWPFHeaderFooterPolicy(document, sectPr);
        }

        if (policy.getDefaultHeader() == null && policy.getFirstPageHeader() == null
                && policy.getDefaultFooter() == null) {
            XWPFFooter footerD = policy.getFooter(1);// createFooter(policy.DEFAULT);
            XWPFRun run = footerD.getParagraphs().get(0).createRun();
            run.setText("usuario");
            XWPFParagraph paragraph = footerD.createParagraph();
            paragraph.setAlignment(ParagraphAlignment.DISTRIBUTE);
            run = paragraph.createRun();
            run.setFontFamily("Arial");
            run.setFontSize(8);
            run.setText(
                    "Jr.Washington N 1894, Cercado de Lima. Central Telefonica: 417-0630 www.onpe.gob.pe informes@onpe.gob.pe");

        }

        XWPFParagraph paragraph = document.createParagraph();

        XWPFRun run = paragraph.createRun();
        run.setFontSize(11);
        run.setFontFamily("Arial");
        run.setText("Lima,");
        run.addBreak();

        paragraph = document.createParagraph();
        run = paragraph.createRun();
        run.setFontSize(11);
        run.setFontFamily("Arial");
        run.setBold(true);
        run.setText("OFICIO N       -2016-GPP/ONPE");
        run.setUnderline(UnderlinePatterns.SINGLE);
        run.addBreak();

        paragraph = document.createParagraph();
        run = paragraph.createRun();
        run.setFontSize(11);
        run.setFontFamily("Arial");
        run.setText("Seor");

        XWPFRun run1 = paragraph.createRun();
        run1.setFontSize(11);
        run1.setFontFamily("Arial");
        run1.setText(nombre.replace("\"", ""));
        run1.setBold(true);
        run1.addBreak();

        XWPFRun run2 = paragraph.createRun();
        run2.setFontSize(11);
        run2.setFontFamily("Arial");
        run2.setText(jsonObject.get("cargo").toString().replace("\"", ""));
        run2.addBreak();
        run2.setText("Centro Poblado " + amb.getNombreAmbito());
        run2.addBreak();
        run2.setText("Av. 28 de Julio S/N Centro Cvico Huacrachuco - Municipalidad Provincial de "
                + amb.getProvincia());
        run2.addBreak();
        run2.setText(amb.getDepartamento() + " - " + amb.getProvincia() + " - " + amb.getDistrito());
        run2.addBreak();

        run2 = paragraph.createRun();
        run2.setFontSize(11);
        run2.setFontFamily("Arial");
        run2.setUnderline(UnderlinePatterns.WORDS);
        run2.setText("Presente");

        run2 = paragraph.createRun();
        run2.setFontSize(11);
        run2.setFontFamily("Arial");
        run2.setText(".-");

        paragraph = document.createParagraph();
        run.addBreak();
        run = paragraph.createRun();
        run.setFontSize(11);
        run.setFontFamily("Arial");
        run.addBreak();
        run.setText("Asunto");
        run.addTab();
        run.addTab();
        run.setText(": SOLICITUD DE CREACIN DE MESA DE SUFRAGIO.");
        run.addBreak();

        paragraph = document.createParagraph();
        run = paragraph.createRun();
        run.setFontSize(11);
        run.setFontFamily("Arial");
        run.setText("Referencia");
        run.addTab();
        run.setText(": OFICIO N 087-2016/M-CP.CHOCOBAMBA (16AGO2016) - Exp. " + expediente.getExpediente());
        run.addBreak();

        paragraph = document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.THAI_DISTRIBUTE);
        run = paragraph.createRun();
        run.setFontSize(11);
        run.setFontFamily("Arial");
        run.setText(
                "Me dirijo a usted con relacin al documento de la referencia con la finalidad de hacer de su "
                        + "conocimiento que se ha cumplido con todos los requisitos que dan inicio al trmite de "
                        + "instalacin de mesas de sufragio en el Centro Poblado " + amb.getNombreAmbito()
                        + ", distrito " + amb.getDistrito() + ", " + "provincia " + amb.getProvincia()
                        + ", departamento " + amb.getDepartamento() + ".");
        paragraph = document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.THAI_DISTRIBUTE);
        run = paragraph.createRun();
        run.setFontSize(11);
        run.setFontFamily("Arial");
        run.addBreak();
        run.setText("Al respecto, el mencionado expediente contiene un listado de electores que solicitan ser "
                + "parte de la mesa de sufragio de la localidad " + amb.getNombreAmbito()
                + ", el cual, luego de la validacin " + "realizada, se informa que podrn ser incorporados "
                + totalElectoresIncorporados + " electores del total de " + totalElectoresRemitidos
                + " registros "
                + "de electores remitidos. Se adjunta un cuadro resumen con las observaciones mencionadas.");
        paragraph = document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.THAI_DISTRIBUTE);
        run = paragraph.createRun();
        run.setFontSize(11);
        run.setFontFamily("Arial");
        run.addBreak();
        run.setText(
                "Asimismo, se programar un viaje para la verificacin de rutas, tiempos y servicios de la "
                        + "localidad, la cual se coordinar previamente con las autoridades del centro poblado a fin de "
                        + "programarla adecuadamente; luego de lo cual se emitir un informe de respuesta al "
                        + "resultado de la solicitud, que de ser positivo, conllevara a la instalacin de mesas de sufragio "
                        + "en el centro poblado en mencin, con miras a las ");
        run = paragraph.createRun();
        run.setFontSize(11);
        run.setFontFamily("Arial");
        run.setBold(true);
        run.setText("Elecciones Regionales y Municipales de 2018.");
        paragraph = document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.THAI_DISTRIBUTE);
        run = paragraph.createRun();
        run.setFontSize(11);
        run.setFontFamily("Arial");
        run.addBreak();
        run.setText("Finalmente, de requerir mayor informacin, agradeceremos se comunique con nosotros al "
                + "telefono 417-0630 anexo 8484 o al 8481.");
        paragraph = document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.THAI_DISTRIBUTE);
        run = paragraph.createRun();
        run.setFontSize(11);
        run.setFontFamily("Arial");
        run.addBreak();
        run.setText("Sin otro particular.");

        paragraph = document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.THAI_DISTRIBUTE);
        run = paragraph.createRun();
        run.setFontSize(11);
        run.setFontFamily("Arial");
        run.addBreak();
        run.addBreak();
        run.setText("Atentamente,");
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        document.write(response.getOutputStream());
    } catch (Exception ex) {
        Logger.getLogger(registroController.class.getName()).log(Level.SEVERE, null, 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();
    run.setBold(true);// ww w. j  av  a  2s  .c o  m
    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) {

    }

}

From source file:sofmeth.mco3.gui.DRLFrame.java

private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed
    // TODO add your handling code here:

    //TODO: need error handling to check if inputs in Fix Time etc are numbers and not letters
    XWPFDocument document = new XWPFDocument();
    try {//from  w  w  w . j  ava2 s .c om
        FileOutputStream out = new FileOutputStream(new File("defect recording log.docx"));
        //making of title
        XWPFParagraph para = document.createParagraph();
        para.setAlignment(ParagraphAlignment.CENTER);
        para.setSpacingAfter(500);
        XWPFRun run = para.createRun();
        run.setText("Defect Recording Log");
        run.setBold(true);
        run.setFontSize(16);
        //end making of title
        //adding name etc to document
        XWPFTable details = document.createTable(3, 2);
        //setting cell width
        CTTblWidth width = details.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9500));
        //end setting cell width
        //details.getCTTbl().getTblPr().unsetTblBorders();
        XWPFTableRow dtlRow = details.getRow(0);
        dtlRow.getCell(0).setText("Name: " + nameField);
        dtlRow.getCell(1).setText("Date: " + dateField);
        dtlRow = details.getRow(1);
        dtlRow.getCell(0).setText("Program: " + progField);
        dtlRow.getCell(1).setText("Program#: " + progNumField);
        dtlRow = details.getRow(2);
        dtlRow.getCell(0).setText("Professor: " + profField);
        dtlRow.getCell(1).setText("Language: " + langField);
        //end adding name to document
        para = document.createParagraph();
        run = para.createRun();
        run.addBreak();
        run.addBreak();
        XWPFTable table = document.createTable(6, 8);
        width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9500));
        for (int i = -1; i < 6; i++) {

            if (i == 5)
                break;
            if (i == -1) { //top row of the table
                XWPFTableRow row1 = table.getRow(i + 1);
                for (int j = 0; j < 8; j++) {

                    switch (j) { //no formatting yet like bold etc
                    case 0:
                        row1.getCell(j).setText("Date");
                        break;
                    case 1:
                        row1.getCell(j).setText("Number");
                        break;
                    case 2:
                        row1.getCell(j).setText("Type");
                        break;
                    case 3:
                        row1.getCell(j).setText("Inject");
                        break;
                    case 4:
                        row1.getCell(j).setText("Remove");
                        break;
                    case 5:
                        row1.getCell(j).setText("Fix Time");
                        break;
                    case 6:
                        row1.getCell(j).setText("Fix Defect");
                        break;
                    case 7:
                        row1.getCell(j).setText("Description");
                        break;
                    }

                }
            }
            //although this code assumes that there are values inside
            //UPDATE1: ok i fixed it i think
            else {
                XWPFTableRow row = table.getRow(i + 1);
                for (int j = 0; j < 8; j++) {
                    if (drlTable.getModel().getValueAt(i, 1) != null
                            && !drlTable.getModel().getValueAt(i, j).toString().isEmpty()) {

                        if (drlTable.getModel().getValueAt(i, j) != null
                                && !drlTable.getModel().getValueAt(i, j).toString().isEmpty())

                            row.getCell(j).setText(drlTable.getModel().getValueAt(i, j).toString());
                    } else
                        break;
                }
            }

        }
        document.write(out);

        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:sofmeth.mco3.gui.PIPFrame.java

private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed
    // TODO add your handling code here:

    XWPFDocument document = new XWPFDocument();
    try {/*from   w w w.j ava2  s . c om*/
        FileOutputStream out = new FileOutputStream(new File("pip form.docx"));
        //start making title
        XWPFParagraph para = document.createParagraph();
        para.setAlignment(ParagraphAlignment.CENTER);
        para.setSpacingAfter(500);
        XWPFRun run = para.createRun();
        run.setText("Process Improvement Proposal(PIP)");
        run.setBold(true);
        run.setFontSize(16);
        //end making title
        //start making name table
        XWPFTable details = document.createTable(2, 2);
        //setting cell width
        CTTblWidth width = details.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9000));
        //end setting cell width
        //details.getCTTbl().getTblPr().unsetTblBorders();
        XWPFTableRow row = details.getRow(0);
        row.getCell(0).setText("Name: " + nameField);
        row.getCell(1).setText("Date: " + nameField);
        row = details.getRow(1);
        row.getCell(0).setText("Professor: " + profField);
        row.getCell(1).setText("Program#: " + progNumField);

        para = document.createParagraph();
        run = para.createRun();
        run.addBreak();
        run.addBreak();
        //end making name table
        XWPFTable table = document.createTable(6, 2);
        width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9000));
        for (int i = -1; i < 6; i++) {

            if (i == 5)
                break;
            if (i == -1) { //top row of the table
                XWPFTableRow row1 = table.getRow(i + 1);
                for (int j = 0; j < 2; j++) {

                    switch (j) { //no formatting yet like bold etc
                    case 0:
                        row1.getCell(j).setText("Number");
                        break;
                    case 1:
                        row1.getCell(j).setText("Problem Description");
                        break;

                    }

                }
            }
            //although this code assumes that there are values inside
            //UPDATE1: ok i fixed it i think
            else {
                row = table.getRow(i + 1);
                for (int j = 0; j < 2; j++) {
                    if (probTable.getModel().getValueAt(i, 1) != null
                            && !probTable.getModel().getValueAt(i, j).toString().isEmpty()) {

                        if (probTable.getModel().getValueAt(i, j) != null
                                && !probTable.getModel().getValueAt(i, j).toString().isEmpty())

                            row.getCell(j).setText(probTable.getModel().getValueAt(i, j).toString());
                    } else
                        break;
                }
            }

        }
        para = document.createParagraph();
        run = para.createRun();
        run.addBreak();
        run.addBreak();
        XWPFTable table2 = document.createTable(6, 2);
        width = table2.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9000));
        for (int i = -1; i < 6; i++) {

            if (i == 5)
                break;
            if (i == -1) { //top row of the table
                XWPFTableRow row1 = table2.getRow(i + 1);
                for (int j = 0; j < 2; j++) {

                    switch (j) { //no formatting yet like bold etc
                    case 0:
                        row1.getCell(j).setText("Number");
                        break;
                    case 1:
                        row1.getCell(j).setText("Proposal Description");
                        break;

                    }

                }
            }
            //although this code assumes that there are values inside
            //UPDATE1: ok i fixed it i think
            else {
                row = table2.getRow(i + 1);
                for (int j = 0; j < 2; j++) {
                    if (propTable.getModel().getValueAt(i, 1) != null
                            && !propTable.getModel().getValueAt(i, j).toString().isEmpty()) {

                        if (propTable.getModel().getValueAt(i, j) != null
                                && !propTable.getModel().getValueAt(i, j).toString().isEmpty())

                            row.getCell(j).setText(propTable.getModel().getValueAt(i, j).toString());
                    } else
                        break;
                }
            }

        }
        para = document.createParagraph();
        run = para.createRun();
        run.addBreak();
        run.addBreak();
        table = document.createTable(2, 1);
        width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9000));
        row = table.getRow(0);
        row.getCell(0).setText("Notes and Comments");
        row = table.getRow(1);
        row.getCell(0).setText(notesArea.getText());

        document.write(out);

        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:sofmeth.mco3.gui.PPSFrame.java

private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed
    // TODO add your handling code here:
    XWPFDocument document = new XWPFDocument();

    try {//from ww  w. jav  a  2 s  .co  m
        FileOutputStream out = new FileOutputStream(new File("PPS " + comboValue + ".docx"));

        //making of title
        XWPFParagraph para = document.createParagraph();
        para.setAlignment(ParagraphAlignment.CENTER);
        para.setSpacingAfter(500);
        XWPFRun run = para.createRun();
        run.setText("Project Plan Summary(" + comboValue + ")");
        run.setBold(true);
        run.setFontSize(16);
        //end making of title
        para = document.createParagraph();
        run = para.createRun();

        //adding name etc to document
        XWPFTable details = document.createTable(3, 2);
        //setting cell width
        CTTblWidth width = details.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9000));
        //end setting cell width
        //details.getCTTbl().getTblPr().unsetTblBorders();
        XWPFTableRow row = details.getRow(0);
        row.getCell(0).setText("Name: " + nameField);
        row.getCell(1).setText("Date: " + dateField);
        row = details.getRow(1);
        row.getCell(0).setText("Program: " + progField);
        row.getCell(1).setText("Program#: " + progNumField);
        row = details.getRow(2);
        row.getCell(0).setText("Professor: " + profField);
        row.getCell(1).setText("Language: " + langField);

        para = document.createParagraph();
        run = para.createRun();
        run.addBreak();
        run.addBreak();

        if (comboValue == "4A" || comboValue == "5A") {
            XWPFTable table = document.createTable(7, 4);
            width = table.getCTTbl().addNewTblPr().addNewTblW();
            width.setType(STTblWidth.DXA);
            width.setW(BigInteger.valueOf(9000));

            //poor code ahead!

            row = table.getRow(0);
            for (int i = 0; i < 4; i++) {
                switch (i) {
                case 0:
                    row.getCell(i).setText("Summary");
                    break;
                case 1:
                    row.getCell(i).setText("Plan");
                    break;
                case 2:
                    row.getCell(i).setText("Actual");
                    break;
                case 3:
                    row.getCell(i).setText("To Date");
                    break;
                }
            }
            row = table.getRow(1);
            for (int i = 0; i < 4; i++) {
                switch (i) {
                case 0:
                    row.getCell(i).setText("LOC/Hour");
                    break;
                case 1:
                    row.getCell(i).setText(locField.getText());
                    break;
                case 2:
                    row.getCell(i).setText(locField2.getText());
                    break;
                case 3:
                    row.getCell(i).setText(locField3.getText());
                    break;
                }
            }
            row = table.getRow(2);
            row.getCell(0).setText("Planned Time");
            row.getCell(1).setText(plannedField.getText());
            row.getCell(3).setText(plannedField2.getText());

            row = table.getRow(3);
            row.getCell(0).setText("Actual Time");
            row.getCell(2).setText(actualField.getText());
            row.getCell(3).setText(actualField2.getText());

            row = table.getRow(4);
            row.getCell(0).setText("CPI");
            row.getCell(3).setText(cpiField.getText());

            row = table.getRow(5);
            for (int i = 0; i < 4; i++) {
                switch (i) {
                case 0:
                    row.getCell(i).setText("% Reused");
                    break;
                case 1:
                    row.getCell(i).setText(perreusedField.getText());
                    break;
                case 2:
                    row.getCell(i).setText(perreusedField2.getText());
                    break;
                case 3:
                    row.getCell(i).setText(perreusedField3.getText());
                    break;
                }
            }
            row = table.getRow(6);
            for (int i = 0; i < 4; i++) {
                switch (i) {
                case 0:
                    row.getCell(i).setText("% Reused");
                    break;
                case 1:
                    row.getCell(i).setText(newreusedField.getText());
                    break;
                case 2:
                    row.getCell(i).setText(newreusedField2.getText());
                    break;
                case 3:
                    row.getCell(i).setText(newreusedField3.getText());
                    break;
                }
            }
            para = document.createParagraph();
        }

        if (comboValue == "2A" || comboValue == "3A" || comboValue == "4A" || comboValue == "5A") {

            System.out.print("test");
            XWPFTable table = document.createTable(5, 4);
            width = table.getCTTbl().addNewTblPr().addNewTblW();
            width.setType(STTblWidth.DXA);
            width.setW(BigInteger.valueOf(9000));

            for (int i = -1; i < 5; i++) {

                if (i == 4)
                    break;
                if (i == -1) { //top row of the table
                    XWPFTableRow row1 = table.getRow(i + 1);
                    for (int j = 0; j < 4; j++) {

                        switch (j) { //no formatting yet like bold etc
                        case 0:
                            row1.getCell(j).setText("Program Size");
                            break;
                        case 1:
                            row1.getCell(j).setText("Plan");
                            break;
                        case 2:
                            row1.getCell(j).setText("Actual");
                            break;
                        case 3:
                            row1.getCell(j).setText("To Date");
                            break;

                        }

                    }
                }
                //although this code assumes that there are values inside
                //UPDATE1: ok i fixed it i think
                else {
                    row = table.getRow(i + 1);
                    for (int j = 0; j < 4; j++) {

                        if (loc11Table.getModel().getValueAt(i, j) != null
                                && !loc11Table.getModel().getValueAt(i, j).toString().isEmpty())
                            row.getCell(j).setText(loc11Table.getModel().getValueAt(i, j).toString());

                    }
                }

            }
            table = document.createTable(4, 4);
            for (int i = 0; i < 4; i++) {

                row = table.getRow(i);
                for (int j = 0; j < 4; j++) {

                    if (loc11Table2.getModel().getValueAt(i, j) != null
                            && !loc11Table2.getModel().getValueAt(i, j).toString().isEmpty())
                        row.getCell(j).setText(loc11Table2.getModel().getValueAt(i, j).toString());

                }

            }

            para = document.createParagraph();

        }

        XWPFTable table = document.createTable(8, 5);
        width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9000));
        //time
        for (int i = -1; i < 8; i++) {

            if (i == 7)
                break;
            if (i == -1) { //top row of the table
                XWPFTableRow row1 = table.getRow(i + 1);
                for (int j = 0; j < 5; j++) {

                    switch (j) { //no formatting yet like bold etc
                    case 0:
                        row1.getCell(j).setText("Time in Phase");
                        break;
                    case 1:
                        row1.getCell(j).setText("Plan");
                        break;
                    case 2:
                        row1.getCell(j).setText("Actual");
                        break;
                    case 3:
                        row1.getCell(j).setText("To Date");
                        break;
                    case 4:
                        row1.getCell(j).setText("To Date %");
                        break;
                    }

                }
            }
            //although this code assumes that there are values inside
            //UPDATE1: ok i fixed it i think
            else {
                row = table.getRow(i + 1);
                for (int j = 0; j < 5; j++) {

                    if (timeTable.getModel().getValueAt(i, j) != null
                            && !timeTable.getModel().getValueAt(i, j).toString().isEmpty())
                        row.getCell(j).setText(timeTable.getModel().getValueAt(i, j).toString());

                }
            }
        }
        System.out.print(timeTable.getModel().getValueAt(0, 0).toString());
        //defect inject
        para = document.createParagraph();
        run = para.createRun();
        run.addBreak();
        table = document.createTable(7, 5);
        width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9000));
        for (int i = -1; i < 7; i++) {

            if (i == 6)
                break;
            if (i == -1) { //top row of the table
                XWPFTableRow row1 = table.getRow(i + 1);
                for (int j = 0; j < 5; j++) {

                    switch (j) { //no formatting yet like bold etc
                    case 0:
                        row1.getCell(j).setText("Defects Injected");
                        break;
                    case 1:
                        row1.getCell(j).setText("Plan");
                        break;
                    case 2:
                        row1.getCell(j).setText("Actual");
                        break;
                    case 3:
                        row1.getCell(j).setText("To Date");
                        break;
                    case 4:
                        row1.getCell(j).setText("To Date %");
                        break;
                    }

                }
            }
            //although this code assumes that there are values inside
            //UPDATE1: ok i fixed it i think
            else {
                row = table.getRow(i + 1);
                for (int j = 0; j < 5; j++) {

                    if (defectiTable.getModel().getValueAt(i, j) != null
                            && !defectiTable.getModel().getValueAt(i, j).toString().isEmpty())

                        row.getCell(j).setText(defectiTable.getModel().getValueAt(i, j).toString());

                }
            }

        }
        //defect removed
        para = document.createParagraph();
        run = para.createRun();
        run.addBreak();

        table = document.createTable(8, 5);
        width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9000));
        for (int i = -1; i < 8; i++) {

            if (i == 7)
                break;
            if (i == -1) { //top row of the table
                XWPFTableRow row1 = table.getRow(i + 1);
                for (int j = 0; j < 5; j++) {

                    switch (j) { //no formatting yet like bold etc
                    case 0:
                        row1.getCell(j).setText("Time in Phase");
                        break;
                    case 1:
                        row1.getCell(j).setText("Plan");
                        break;
                    case 2:
                        row1.getCell(j).setText("Actual");
                        break;
                    case 3:
                        row1.getCell(j).setText("To Date");
                        break;
                    case 4:
                        row1.getCell(j).setText("To Date %");
                        break;
                    }

                }
            }
            //although this code assumes that there are values inside
            //UPDATE1: ok i fixed it i think
            else {
                row = table.getRow(i + 1);
                for (int j = 0; j < 5; j++) {

                    if (defectrTable.getModel().getValueAt(i, j) != null
                            && !defectrTable.getModel().getValueAt(i, j).toString().isEmpty())

                        row.getCell(j).setText(defectrTable.getModel().getValueAt(i, j).toString());

                }
            }
        }

        document.write(out);
        out.close();

    } catch (Exception e) {

    }
}

From source file:sofmeth.mco3.gui.Result2AFrame.java

private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed
    // TODO add your handling code here:
    XWPFDocument document = new XWPFDocument();
    try {//  w  ww  .j a  v a2s .  c o m
        FileOutputStream out = new FileOutputStream(new File("program 2a results.docx"));
        //making of title
        XWPFParagraph para = document.createParagraph();
        para.setAlignment(ParagraphAlignment.CENTER);
        para.setSpacingAfter(500);
        XWPFRun run = para.createRun();
        run.setText("Program 2A Results");
        run.setBold(true);
        run.setFontSize(16);
        //end making of title
        //adding name etc to document
        XWPFTable details = document.createTable(2, 2);
        //setting cell width
        CTTblWidth width = details.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9500));
        //end setting cell width
        //details.getCTTbl().getTblPr().unsetTblBorders();
        XWPFTableRow dtlRow = details.getRow(0);
        dtlRow.getCell(0).setText("Name: " + nameField);
        dtlRow.getCell(1).setText("Date: " + dateField);
        dtlRow = details.getRow(1);
        dtlRow.getCell(0).setText("Professor: " + profField);
        dtlRow.getCell(1).setText("Program#: " + progNumField);
        //end adding name to document
        para = document.createParagraph();
        run = para.createRun();
        run.addBreak();
        run.addBreak();

        //creating of TPT table
        XWPFTable table = document.createTable(11, 2);

        width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(5000));
        /*
                
        TODO: make all loops like this while loops?
        */
        for (int i = -1; i < 11; i++) {

            if (i == 10)
                break;
            if (i == -1) { //top row of the table
                XWPFTableRow row1 = table.getRow(i + 1);
                for (int j = 0; j < 2; j++) {

                    switch (j) { //no formatting yet like bold etc
                    case 0:
                        row1.getCell(j).setText("Program Number");
                        break;
                    case 1:
                        row1.getCell(j).setText("LOC");
                        break;

                    }

                }
            }
            //although this code assumes that there are values inside
            //UPDATE1: ok i fixed it i think
            else {
                XWPFTableRow row = table.getRow(i + 1);
                for (int j = 0; j < 2; j++) {
                    if (resultTable.getModel().getValueAt(i, 1) != null
                            && !resultTable.getModel().getValueAt(i, j).toString().isEmpty()) {

                        if (resultTable.getModel().getValueAt(i, j) != null
                                && !resultTable.getModel().getValueAt(i, j).toString().isEmpty())

                            row.getCell(j).setText(resultTable.getModel().getValueAt(i, j).toString());
                    } else
                        break;
                }
            }
        }
        document.write(out);
        out.close();
    } catch (Exception e) {

    }
}

From source file:sofmeth.mco3.gui.Result3AFrame.java

private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed
    // TODO add your handling code here:
    XWPFDocument document = new XWPFDocument();
    try {//from w w w.  j ava  2 s .c om
        FileOutputStream out = new FileOutputStream(new File("program 3a results.docx"));
        //making of title
        XWPFParagraph para = document.createParagraph();
        para.setAlignment(ParagraphAlignment.CENTER);
        para.setSpacingAfter(500);
        XWPFRun run = para.createRun();
        run.setText("Program 3A Results");
        run.setBold(true);
        run.setFontSize(16);
        //end making of title
        //adding name etc to document
        XWPFTable details = document.createTable(2, 2);
        //setting cell width
        CTTblWidth width = details.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9500));
        //end setting cell width
        //details.getCTTbl().getTblPr().unsetTblBorders();
        XWPFTableRow dtlRow = details.getRow(0);
        dtlRow.getCell(0).setText("Name: " + nameField);
        dtlRow.getCell(1).setText("Date: " + dateField);
        dtlRow = details.getRow(1);
        dtlRow.getCell(0).setText("Professor: " + profField);
        dtlRow.getCell(1).setText("Program#: " + progNumField);
        //end adding name to document
        para = document.createParagraph();
        run = para.createRun();
        run.addBreak();
        run.addBreak();

        //creating of TPT table
        XWPFTable table = document.createTable(11, 4);

        width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9500));
        /*
                
        TODO: make all loops like this while loops?
        */
        for (int i = -1; i < 11; i++) {

            if (i == 10)
                break;
            if (i == -1) { //top row of the table
                XWPFTableRow row1 = table.getRow(i + 1);
                for (int j = 0; j < 4; j++) {

                    switch (j) { //no formatting yet like bold etc
                    case 0:
                        row1.getCell(j).setText("Program Number");
                        break;
                    case 1:
                        row1.getCell(j).setText("Function Name");
                        break;
                    case 2:
                        row1.getCell(j).setText("Object LOC");
                        break;
                    case 3:
                        row1.getCell(j).setText("Total LOC");

                    }

                }
            }
            //although this code assumes that there are values inside
            //UPDATE1: ok i fixed it i think
            else {
                XWPFTableRow row = table.getRow(i + 1);
                for (int j = 0; j < 4; j++) {
                    if (resultTable.getModel().getValueAt(i, 1) != null
                            && !resultTable.getModel().getValueAt(i, j).toString().isEmpty()) {

                        if (resultTable.getModel().getValueAt(i, j) != null
                                && !resultTable.getModel().getValueAt(i, j).toString().isEmpty())

                            row.getCell(j).setText(resultTable.getModel().getValueAt(i, j).toString());
                    } else
                        break;
                }
            }
        }
        document.write(out);
        out.close();
    } catch (Exception e) {

    }
}

From source file:sofmeth.mco3.gui.SETFrame.java

private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed
    // TODO add your handling code here:
    XWPFDocument document = new XWPFDocument();
    try {//  ww w. ja v  a2  s .com
        FileOutputStream out = new FileOutputStream(new File("size estimating template.docx"));
        //making of title
        XWPFParagraph para = document.createParagraph();
        para.setAlignment(ParagraphAlignment.CENTER);
        para.setSpacingAfter(500);
        XWPFRun run = para.createRun();
        run.setText("Size Estimating Template");
        run.setBold(true);
        run.setFontSize(16);
        //end making of title
        //adding name etc to document
        XWPFTable details = document.createTable(2, 2);
        //setting cell width
        CTTblWidth width = details.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9500));
        //end setting cell width
        //details.getCTTbl().getTblPr().unsetTblBorders();
        XWPFTableRow dtlRow = details.getRow(0);
        dtlRow.getCell(0).setText("Name: " + nameField);
        dtlRow.getCell(1).setText("Date: " + dateField);
        dtlRow = details.getRow(1);
        dtlRow.getCell(0).setText("Professor: " + profField);
        dtlRow.getCell(1).setText("Program#: " + progNumField);
        //end adding name to document
        para = document.createParagraph();
        run = para.createRun();
        run.addBreak();
        run.addBreak();

        //computation
        for (int i = 0; i < 6; i++) {

            if (locTable.getModel().getValueAt(i, 4) != null
                    && !locTable.getModel().getValueAt(i, 4).toString().isEmpty()) {

                bSize = bSize + Integer.parseInt(locTable.getModel().getValueAt(i, 4).toString());
            }

        }
        for (int i = 0; i < 6; i++) {

            if (noTable.getModel().getValueAt(i, 4) != null
                    && !noTable.getModel().getValueAt(i, 4).toString().isEmpty()) {

                totalNew = totalNew + Integer.parseInt(noTable.getModel().getValueAt(i, 4).toString());
            }

        }
        for (int i = 0; i < 6; i++) {

            if (roTable.getModel().getValueAt(i, 1) != null
                    && !roTable.getModel().getValueAt(i, 1).toString().isEmpty()) {

                reTotal = reTotal + Integer.parseInt(roTable.getModel().getValueAt(i, 1).toString());
            }

        }

        pLoc = bSize + totalNew;
        int b0 = Integer.parseInt(b0Field.getText());
        int b1 = Integer.parseInt(b1Field.getText());
        esNew = b0 + b1 * (pLoc + locMod);
        esTotal = esNew + bSize - locDeleted - locMod + reTotal;
        //

        //creating of TPT table
        XWPFTable table = document.createTable(1, 1);
        width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9500));

        XWPFTableRow row = table.getRow(0);
        row.getCell(0).setText("Base Program");

        table = document.createTable(3, 2);

        for (int i = 0; i < 3; i++) {

            row = table.getRow(i);
            switch (i) {
            case 0:
                row.getCell(0).setText("Base Size(B)");
                row.getCell(1).setText(baseField.getText());
                break;
            case 1:
                row.getCell(0).setText("LOC Deleted(D)");
                row.getCell(1).setText(delField.getText());
                break;
            case 2:
                row.getCell(0).setText("LOC Modified(M)");
                row.getCell(1).setText(modField.getText());
                break;
            }
        }

        para = document.createParagraph();
        run = para.createRun();
        table = document.createTable(1, 1);
        width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9500));
        row = table.getRow(0);
        row.getCell(0).setText("Projected LOC (P)");

        table = document.createTable(7, 5);

        for (int i = -1; i < 6; i++) {

            if (i == 5)
                break;
            if (i == -1) { //top row of the table
                XWPFTableRow row1 = table.getRow(i + 1);
                for (int j = 0; j < 5; j++) {

                    switch (j) { //no formatting yet like bold etc
                    case 0:
                        row1.getCell(j).setText("Base Additions");
                        break;
                    case 1:
                        row1.getCell(j).setText("Type");
                        break;
                    case 2:
                        row1.getCell(j).setText("Methods");
                        break;
                    case 3:
                        row1.getCell(j).setText("Relative Size");
                        break;
                    case 4:
                        row1.getCell(j).setText("LOC");
                        break;

                    }

                }
            }
            //although this code assumes that there are values inside
            //UPDATE1: ok i fixed it i think
            else {
                row = table.getRow(i + 1);
                for (int j = 0; j < 5; j++) {
                    if (locTable.getModel().getValueAt(i, 1) != null
                            && !locTable.getModel().getValueAt(i, j).toString().isEmpty()) {

                        if (locTable.getModel().getValueAt(i, j) != null
                                && !locTable.getModel().getValueAt(i, j).toString().isEmpty())

                            row.getCell(j).setText(locTable.getModel().getValueAt(i, j).toString());
                    } else
                        break;
                }
            }

        }

        System.out.print(bSize);

        table = document.createTable(1, 2);
        row = table.getRow(0);
        row.getCell(0).setText("Total Base Additions(BA)");
        row.getCell(1).setText(Integer.toString(bSize));

        para = document.createParagraph();
        run = para.createRun();

        table = document.createTable(7, 5);
        width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9500));
        for (int i = -1; i < 6; i++) {

            if (i == 5)
                break;
            if (i == -1) { //top row of the table
                XWPFTableRow row1 = table.getRow(i + 1);
                for (int j = 0; j < 5; j++) {

                    switch (j) { //no formatting yet like bold etc
                    case 0:
                        row1.getCell(j).setText("New Objects(NO)");
                        break;
                    case 1:
                        row1.getCell(j).setText("Type");
                        break;
                    case 2:
                        row1.getCell(j).setText("Methods");
                        break;
                    case 3:
                        row1.getCell(j).setText("Relative Size");
                        break;
                    case 4:
                        row1.getCell(j).setText("LOC");
                        break;

                    }

                }
            }
            //although this code assumes that there are values inside
            //UPDATE1: ok i fixed it i think
            else {
                row = table.getRow(i + 1);
                for (int j = 0; j < 5; j++) {
                    if (noTable.getModel().getValueAt(i, 1) != null
                            && !noTable.getModel().getValueAt(i, j).toString().isEmpty()) {

                        if (noTable.getModel().getValueAt(i, j) != null
                                && !noTable.getModel().getValueAt(i, j).toString().isEmpty())

                            row.getCell(j).setText(noTable.getModel().getValueAt(i, j).toString());
                    } else
                        break;
                }
            }

        }
        table = document.createTable(1, 2);
        row = table.getRow(0);
        row.getCell(0).setText("Total New Objects(NO)");
        row.getCell(1).setText(Integer.toString(totalNew));

        para = document.createParagraph();
        //para.setPageBreak(true);

        table = document.createTable(7, 2);
        width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9500));
        for (int i = -1; i < 6; i++) {

            if (i == 6)
                break;
            if (i == -1) { //top row of the table
                XWPFTableRow row1 = table.getRow(i + 1);
                for (int j = 0; j < 2; j++) {

                    switch (j) { //no formatting yet like bold etc
                    case 0:
                        row1.getCell(j).setText("Reused Objects");
                        break;
                    case 1:
                        row1.getCell(j).setText("LOC");
                        break;

                    }

                }
            }
            //although this code assumes that there are values inside
            //UPDATE1: ok i fixed it i think
            else {
                row = table.getRow(i + 1);
                for (int j = 0; j < 2; j++) {
                    if (roTable.getModel().getValueAt(i, 1) != null
                            && !roTable.getModel().getValueAt(i, j).toString().isEmpty()) {

                        if (roTable.getModel().getValueAt(i, j) != null
                                && !roTable.getModel().getValueAt(i, j).toString().isEmpty())

                            row.getCell(j).setText(roTable.getModel().getValueAt(i, j).toString());
                    } else
                        break;
                }
            }

        }
        table = document.createTable(1, 2);
        row = table.getRow(0);
        row.getCell(0).setText("Reused Total(R)");
        row.getCell(1).setText(Integer.toString(reTotal));

        para = document.createParagraph();
        table = document.createTable(5, 2);
        width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9500));

        for (int i = 0; i < 5; i++) {

            row = table.getRow(i);
            switch (i) {
            case 0:
                row.getCell(0).setText("Projected LOC(P)");
                row.getCell(1).setText(Integer.toString(pLoc));
                break;
            case 1:
                row.getCell(0).setText("Regression Parameter(B0)");
                row.getCell(1).setText(Integer.toString(b0));
                break;
            case 2:
                row.getCell(0).setText("Regression Parameter(B1)");
                row.getCell(1).setText(Integer.toString(b1));
                break;
            case 3:
                row.getCell(0).setText("Estimated New and Changed LOC(N)");
                row.getCell(1).setText(Integer.toString(esNew));
                break;
            case 4:
                row.getCell(0).setText("Estimated Total LOC(T)");
                row.getCell(1).setText(Integer.toString(esTotal));
                break;
            }
        }

        document.write(out);
        out.close();

    } catch (Exception e) {

    }

}

From source file:sofmeth.mco3.gui.SourceCodeFrame.java

private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed
    // TODO add your handling code here:
    XWPFDocument document = new XWPFDocument();
    //write document
    try {/*from   w w w .  j ava  2 s. c  om*/
        FileOutputStream out = new FileOutputStream(new File("sourcecode.docx"));
        //making of title
        XWPFParagraph para = document.createParagraph();
        para.setAlignment(ParagraphAlignment.CENTER);
        para.setSpacingAfter(500);
        XWPFRun run = para.createRun();
        run.setText("Source Code Listing");
        run.setBold(true);
        run.setFontSize(16);
        //end making of title
        para = document.createParagraph();
        run = para.createRun();

        //adding name etc to document
        XWPFTable details = document.createTable(3, 2);
        //setting cell width
        CTTblWidth width = details.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9000));
        //end setting cell width
        //details.getCTTbl().getTblPr().unsetTblBorders();
        XWPFTableRow row = details.getRow(0);
        row.getCell(0).setText("Name: " + nameField);
        row.getCell(1).setText("Date: " + dateField);
        row = details.getRow(1);
        row.getCell(0).setText("Program: " + progField);
        row.getCell(1).setText("Program#: " + progNumField);
        row = details.getRow(2);
        row.getCell(0).setText("Professor: " + profField);
        row.getCell(1).setText("Language: " + langField);
        //end adding name to document

        para = document.createParagraph();
        run = para.createRun();
        run.addBreak();
        run.addBreak();
        String sourceCode = codeTextArea.getText();
        if (sourceCode.contains("\n")) {
            String[] lines = sourceCode.split("\n");
            run.setText(lines[0], 0);
            for (int i = 1; i < lines.length; i++) {

                run.addBreak();
                if (lines[i].contains("\t")) {
                    int tabSpace = lines[i].length() - lines[i].replaceAll("\t", "").length();
                    for (int j = 0; j < tabSpace; j++) {
                        run.addTab();
                    }
                }
                run.setText(lines[i]);
            }
        } else
            run.setText(sourceCode);

        //sourceCode.replaceAll("\\n", "&#xA;");
        document.write(out);
        out.close();

    } catch (Exception e) {

    }

}

From source file:sofmeth.mco3.gui.SPTFrame.java

private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed
    // TODO add your handling code here:
    XWPFDocument document = new XWPFDocument();
    try {//from   w w w. j av  a  2  s .c o  m
        FileOutputStream out = new FileOutputStream(new File("schedule planning template.docx"));
        //making of title
        XWPFParagraph para = document.createParagraph();
        para.setAlignment(ParagraphAlignment.CENTER);
        para.setSpacingAfter(500);
        XWPFRun run = para.createRun();
        run.setText("Schedule Planning Template");
        run.setBold(true);
        run.setFontSize(16);
        //end making of title
        //adding name etc to document
        XWPFTable details = document.createTable(2, 2);
        //setting cell width
        CTTblWidth width = details.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9500));
        //end setting cell width
        //details.getCTTbl().getTblPr().unsetTblBorders();
        XWPFTableRow dtlRow = details.getRow(0);
        dtlRow.getCell(0).setText("Name: " + nameField);
        dtlRow.getCell(1).setText("Date: " + dateField);
        dtlRow = details.getRow(1);
        dtlRow.getCell(0).setText("Professor: " + profField);
        dtlRow.getCell(1).setText("Program#: " + progNumField);
        //end adding name to document
        para = document.createParagraph();
        run = para.createRun();
        run.addBreak();
        run.addBreak();

        //creating of TPT table
        XWPFTable table = document.createTable(12, 9);
        width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9500));
        /*
                
        TODO: make all loops like this while loops?
        */
        for (int i = -1; i < 10; i++) {

            if (i == 9)
                break;
            if (i == -1) { //top row of the table
                XWPFTableRow row1 = table.getRow(i + 1);
                for (int j = 0; j < 9; j++) {

                    switch (j) { //no formatting yet like bold etc
                    case 0:
                        row1.getCell(j).setText("Week/Day Number");
                        break;
                    case 1:
                        row1.getCell(j).setText("Date");
                        break;
                    case 2:
                        row1.getCell(j).setText("Direct Hours");
                        break;
                    case 3:
                        row1.getCell(j).setText("Cumulative");
                        break;
                    case 4:
                        row1.getCell(j).setText("Cumulative Value");
                        break;
                    case 5:
                        row1.getCell(j).setText("Direct Hours");
                        break;
                    case 6:
                        row1.getCell(j).setText("Cumulative Hours");
                        break;
                    case 7:
                        row1.getCell(j).setText("Cumulative Value");
                        break;
                    case 8:
                        row1.getCell(j).setText("Adjusted Value");
                        break;

                    }

                }
            }
            //although this code assumes that there are values inside
            //UPDATE1: ok i fixed it i think
            else {
                XWPFTableRow row = table.getRow(i + 1);
                for (int j = 0; j < 9; j++) {
                    if (sptTable.getModel().getValueAt(i, 1) != null
                            && !sptTable.getModel().getValueAt(i, j).toString().isEmpty()) {

                        if (sptTable.getModel().getValueAt(i, j) != null
                                && !sptTable.getModel().getValueAt(i, j).toString().isEmpty())

                            row.getCell(j).setText(sptTable.getModel().getValueAt(i, j).toString());
                    } else
                        break;
                }
            }
        }
        document.write(out);
        out.close();
    } catch (Exception e) {

    }
}