Example usage for org.apache.poi.xwpf.usermodel XWPFDocument write

List of usage examples for org.apache.poi.xwpf.usermodel XWPFDocument write

Introduction

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

Prototype

@SuppressWarnings("resource")
public final void write(OutputStream stream) throws IOException 

Source Link

Document

Write out this document to an Outputstream.

Usage

From source file:org.ArticleEditor.DataContent.ArticleDocDataObject.java

@Override
public void save(File file) throws IOException {
    XWPFDocument document = (XWPFDocument) getDocument();
    try (FileOutputStream output = new FileOutputStream(file)) {
        document.write(output);
    }/*from   ww  w .jav a  2s  .com*/
}

From source file:org.articleEditor.insertContent.DocumentUpdater1.java

License:Apache License

public static void main(String[] args) {
    try {/*  w w  w  . ja  v a2 s  . c om*/
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JTextPane pane = new JTextPane();
        DocxEditorKit kit = new DocxEditorKit();
        pane.setEditorKit(kit);
        String templateFilePath = "C:/WORK/joeffice/DocxTemplate.docx";
        String templateChangedFilePath = "C:/WORK/joeffice/DocxTemplateChanged.docx";
        kit.read(new FileInputStream(templateFilePath), pane.getDocument(), 0);
        XWPFDocument poiDocument = (XWPFDocument) pane.getDocument().getProperty("XWPFDocument");

        pane.getDocument().addDocumentListener(new DocumentUpdater1(poiDocument));
        JScrollPane scrollPane = new JScrollPane(pane);
        f.add(scrollPane);

        StyledDocument doc = (StyledDocument) pane.getDocument();
        doc.insertString(0, "aaa\nbbb\nccc", null);

        SimpleAttributeSet attrs = new SimpleAttributeSet();
        StyleConstants.setBold(attrs, true);
        doc.setCharacterAttributes(4, 3, attrs, false);

        poiDocument.write(new FileOutputStream(templateChangedFilePath));

        f.setSize(300, 300);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}

From source file:org.eclipse.sw360.licenseinfo.outputGenerators.DocxGenerator.java

License:Open Source License

@Override
public byte[] generateOutputFile(Collection<LicenseInfoParsingResult> projectLicenseInfoResults,
        String projectName, String projectVersion, String licenseInfoHeaderText) throws SW360Exception {
    ByteArrayOutputStream docxOutputStream = new ByteArrayOutputStream();
    Optional<byte[]> docxTemplateFile;
    XWPFDocument xwpfDocument;
    try {/*from   w  w w  .  j  av  a  2 s  . c o m*/
        switch (getOutputVariant()) {
        case DISCLOSURE:
            docxTemplateFile = CommonUtils.loadResource(DocxGenerator.class, DOCX_TEMPLATE_FILE);
            xwpfDocument = new XWPFDocument(new ByteArrayInputStream(docxTemplateFile.get()));
            if (docxTemplateFile.isPresent()) {
                fillDocument(xwpfDocument, projectLicenseInfoResults, projectName, projectVersion,
                        licenseInfoHeaderText, false);
            } else {
                throw new SW360Exception(
                        "Could not load the template for xwpf document: " + DOCX_TEMPLATE_FILE);
            }
            break;
        case REPORT:
            docxTemplateFile = CommonUtils.loadResource(DocxGenerator.class, DOCX_TEMPLATE_REPORT_FILE);
            xwpfDocument = new XWPFDocument(new ByteArrayInputStream(docxTemplateFile.get()));
            if (docxTemplateFile.isPresent()) {
                fillDocument(xwpfDocument, projectLicenseInfoResults, projectName, projectVersion,
                        licenseInfoHeaderText, true);
            } else {
                throw new SW360Exception(
                        "Could not load the template for xwpf document: " + DOCX_TEMPLATE_REPORT_FILE);
            }
            break;
        default:
            throw new IllegalArgumentException("Unknown generator variant type: " + getOutputVariant());
        }
        xwpfDocument.write(docxOutputStream);
        docxOutputStream.close();
    } catch (XmlException e) {
        throw new SW360Exception("Got XmlException while generating docx document: " + e.getMessage());
    } catch (IOException e) {
        throw new SW360Exception("Got IOException when generating docx document: " + e.getMessage());
    } catch (TException e) {
        throw new SW360Exception("Error reading sw360 licenses: " + e.getMessage());
    }
    return docxOutputStream.toByteArray();
}

From source file:org.goobi.managedbeans.ProcessBean.java

License:Open Source License

public void generateResultDoc() {
    FacesContext facesContext = FacesContextHelper.getCurrentFacesContext();
    if (!facesContext.getResponseComplete()) {

        /*//from   w  w  w  .j a v  a  2 s.c om
         * -------------------------------- Vorbereiten der Header-Informationen --------------------------------
         */
        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
        try {
            ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
            String contentType = servletContext.getMimeType("search.doc");
            response.setContentType(contentType);
            response.setHeader("Content-Disposition", "attachment;filename=\"search.doc\"");
            ServletOutputStream out = response.getOutputStream();
            SearchResultHelper sch = new SearchResultHelper();
            XWPFDocument wb = sch.getResultAsWord(prepareSearchColumnData(), this.filter, sortList(),
                    this.showClosedProcesses, this.showArchivedProjects);
            wb.write(out);
            out.flush();
            facesContext.responseComplete();

        } catch (IOException e) {

        }
    }
}

From source file:org.hackthelegacy.hack400tool.ibmiscannercore.IBMiConnector.java

License:Open Source License

public void exportToDOCX(String fileName, String inputText) {

    System.out.println("Save as file: " + fileName);
    try {// ww  w .  j ava2  s.com
        prepareFile(fileName);
        FileOutputStream fileOutStream = new FileOutputStream(fileName);
        XWPFDocument wordDocument = new XWPFDocument();
        String[] textArray = inputText.split("\n");
        for (String textLine : textArray) {
            if (textLine.length() == 0 || textLine == null)
                continue;
            XWPFParagraph tmpParagraph = wordDocument.createParagraph();
            XWPFRun tmpRun = tmpParagraph.createRun();
            tmpRun.setText(textLine);
            tmpRun.setFontSize(8);
            tmpRun.setFontFamily("Courier New");
        }
        wordDocument.write(fileOutStream);
        fileOutStream.close();
    } catch (Exception ex) {
        Logger.getLogger(IBMiConnector.class.getName()).log(Level.SEVERE, null, ex);
        return;
    }
}

From source file:org.jeecgframework.poi.test.word.ContractDemoTest.java

License:Apache License

@Test
public void testHasTotal() {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String curTime = format.format(new Date());

    Map<String, Object> map = new HashMap<String, Object>();
    List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
    Map<String, Object> map1 = new HashMap<String, Object>();
    map1.put("type", "");
    map1.put("presum", "1580");
    map1.put("thissum", "1750");
    map1.put("curmonth", "1-11");
    map1.put("now", curTime);
    mapList.add(map1);/*from ww w  .  j  a v a  2s  .c o  m*/
    Map<String, Object> map2 = new HashMap<String, Object>();
    map2.put("type", "");
    map2.put("presum", "1080");
    map2.put("thissum", "1650");
    map2.put("curmonth", "1-11");
    map2.put("now", curTime);
    mapList.add(map2);
    map.put("taxlist", mapList);
    map.put("totalpreyear", "2660");
    map.put("totalthisyear", "3400");
    Map<String, String> total = new HashMap<String, String>();
    total.put("orderId", "2660");
    total.put("orderCreateTime", "3400");
    total.put("supplyName", "3410");
    map.put("order", total);
    try {
        XWPFDocument doc = WordExportUtil.exportWord07("org/jeecgframework/poi/test/word/doc/contractDemo.docx",
                map);
        FileOutputStream fos = new FileOutputStream("D:/excel/ContractDemo.docx");
        doc.write(fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:org.jeecgframework.poi.test.word.TaxManagementTest.java

License:Apache License

@Test
public void testHasTotal() {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String curTime = format.format(new Date());

    Map<String, Object> map = new HashMap<String, Object>();
    List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
    Map<String, Object> map1 = new HashMap<String, Object>();
    map1.put("type", "");
    map1.put("presum", "1580");
    map1.put("thissum", "1750");
    map1.put("curmonth", "1-11");
    map1.put("now", curTime);
    mapList.add(map1);//from  w ww . jav  a  2  s.  c om
    Map<String, Object> map2 = new HashMap<String, Object>();
    map2.put("type", "");
    map2.put("presum", "1080");
    map2.put("thissum", "1650");
    map2.put("curmonth", "1-11");
    map2.put("now", curTime);
    mapList.add(map2);
    map.put("taxlist", mapList);
    map.put("totalpreyear", "2660");
    map.put("totalthisyear", "3400");
    try {
        XWPFDocument doc = WordExportUtil.exportWord07("org/jeecgframework/poi/test/word/doc/?.docx",
                map);
        FileOutputStream fos = new FileOutputStream("D:/excel/?_test.docx");
        doc.write(fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:org.jeecgframework.poi.test.word.TaxManagementTest.java

License:Apache License

@Test
public void testEntity() {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String curTime = format.format(new Date());

    Map<String, Object> map = new HashMap<String, Object>();
    List<TaxManagement> mapList = new ArrayList<TaxManagement>();
    TaxManagement tax = new TaxManagement();
    tax.setType("");
    tax.setCurmonth("1-11");
    tax.setNow(curTime);// w w  w  .  jav  a  2 s. c o m
    tax.setPresum("1580");
    tax.setThissum("1750");
    mapList.add(tax);
    tax = new TaxManagement();
    tax.setType("");
    tax.setCurmonth("1-10");
    tax.setNow(curTime);
    tax.setPresum("1650");
    tax.setThissum("1750");
    mapList.add(tax);
    map.put("taxlist", new ExcelListEntity(mapList, TaxManagement.class));
    Map<String, String> total = new HashMap<String, String>();
    total.put("totalpreyear", "2660");
    total.put("totalthisyear", "3400");
    map.put("total", total);
    map.put("totalpreyear", "2660");
    map.put("totalthisyear", "3400");
    try {
        XWPFDocument doc = WordExportUtil.exportWord07("org/jeecgframework/poi/test/word/doc/?.docx",
                map);
        FileOutputStream fos = new FileOutputStream("D:/excel/?_entity.docx");
        doc.write(fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.joeffice.wordprocessor.DocumentUpdater.java

License:Apache License

public static void main(String[] args) {
    try {//from  w w w  .j  av a  2  s.  co  m
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JTextPane pane = new JTextPane();
        DocxEditorKit kit = new DocxEditorKit();
        pane.setEditorKit(kit);
        String templateFilePath = "C:/WORK/joeffice/DocxTemplate.docx";
        String templateChangedFilePath = "C:/WORK/joeffice/DocxTemplateChanged.docx";
        kit.read(new FileInputStream(templateFilePath), pane.getDocument(), 0);
        XWPFDocument poiDocument = (XWPFDocument) pane.getDocument().getProperty("XWPFDocument");

        pane.getDocument().addDocumentListener(new DocumentUpdater(poiDocument));
        JScrollPane scrollPane = new JScrollPane(pane);
        f.add(scrollPane);

        StyledDocument doc = (StyledDocument) pane.getDocument();
        doc.insertString(0, "aaa\nbbb\nccc", null);

        SimpleAttributeSet attrs = new SimpleAttributeSet();
        StyleConstants.setBold(attrs, true);
        doc.setCharacterAttributes(4, 3, attrs, false);

        poiDocument.write(new FileOutputStream(templateChangedFilePath));

        f.setSize(300, 300);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}

From source file:org.kino.server.api.contractgenerator.java

static void writeDocxTemplate(InputStream src, OutputStream dststrem, Map<String, String> replacementMap)
        throws InvalidFormatException, IOException {
    XWPFDocument doc = new XWPFDocument(src);
    replaceInParagraphs(replacementMap, doc.getParagraphs());
    for (XWPFTable tbl : doc.getTables()) {
        for (XWPFTableRow row : tbl.getRows()) {
            for (XWPFTableCell cell : row.getTableCells()) {
                replaceInParagraphs(replacementMap, cell.getParagraphs());

            }/*from  www .ja  v a  2s. co  m*/
        }
    }

    doc.write(dststrem);

}