Java Code Examples for org.apache.poi.xwpf.usermodel.XWPFDocument#write()

The following examples show how to use org.apache.poi.xwpf.usermodel.XWPFDocument#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JeecgTemplateWordView.java    From autopoi with Apache License 2.0 6 votes vote down vote up
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
	String codedFileName = "临时文件.docx";
	if (model.containsKey(TemplateWordConstants.FILE_NAME)) {
		codedFileName = (String) model.get(TemplateWordConstants.FILE_NAME) + ".docx";
	}
	if (isIE(request)) {
		codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8");
	} else {
		codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
	}
	response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
	XWPFDocument document = WordExportUtil.exportWord07((String) model.get(TemplateWordConstants.URL), (Map<String, Object>) model.get(TemplateWordConstants.MAP_DATA));
	ServletOutputStream out = response.getOutputStream();
	document.write(out);
	out.flush();
}
 
Example 2
Source File: JeecgTemplateWordView.java    From jeasypoi with Apache License 2.0 6 votes vote down vote up
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
	String codedFileName = "临时文件.docx";
	if (model.containsKey(TemplateWordConstants.FILE_NAME)) {
		codedFileName = (String) model.get(TemplateWordConstants.FILE_NAME) + ".docx";
	}
	if (isIE(request)) {
		codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8");
	} else {
		codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
	}
	response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
	XWPFDocument document = WordExportUtil.exportWord07((String) model.get(TemplateWordConstants.URL), (Map<String, Object>) model.get(TemplateWordConstants.MAP_DATA));
	ServletOutputStream out = response.getOutputStream();
	document.write(out);
	out.flush();
}
 
Example 3
Source File: WatermarkWordTests.java    From kbase-doc with Apache License 2.0 6 votes vote down vote up
@Test
public void testDocx2() throws IOException {
	String filepath = "E:\\ConvertTester\\docx\\NVR5X-I人脸比对配置-ekozhan.docx";
	XWPFDocument doc = new XWPFDocument(new FileInputStream(filepath));
	XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(doc);
	
	policy.createWatermark("ekozhan123");
	doc.write(new FileOutputStream("E:\\ConvertTester\\docx\\NVR5X-I人脸比对配置-ekozhan-11.docx"));
	doc.close();
}
 
Example 4
Source File: JeecgTemplateWordView.java    From easypoi with Apache License 2.0 6 votes vote down vote up
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
                                       HttpServletResponse response) throws Exception {
    String codedFileName = "临时文件.docx";
    if (model.containsKey(TemplateWordConstants.FILE_NAME)) {
        codedFileName = (String) model.get(TemplateWordConstants.FILE_NAME) + ".docx";
    }
    if (isIE(request)) {
        codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8");
    } else {
        codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
    }
    response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
    XWPFDocument document = WordExportUtil.exportWord07(
        (String) model.get(TemplateWordConstants.URL),
        (Map<String, Object>) model.get(TemplateWordConstants.MAP_DATA));
    ServletOutputStream out = response.getOutputStream();
    document.write(out);
    out.flush();
}
 
Example 5
Source File: PDF2WordExample.java    From tutorials with MIT License 6 votes vote down vote up
private static void generateDocFromPDF(String filename) throws IOException {
	XWPFDocument doc = new XWPFDocument();

	String pdf = filename;
	PdfReader reader = new PdfReader(pdf);
	PdfReaderContentParser parser = new PdfReaderContentParser(reader);

	for (int i = 1; i <= reader.getNumberOfPages(); i++) {
		TextExtractionStrategy strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
		String text = strategy.getResultantText();
		XWPFParagraph p = doc.createParagraph();
		XWPFRun run = p.createRun();
		run.setText(text);
		run.addBreak(BreakType.PAGE);
	}
	FileOutputStream out = new FileOutputStream("src/output/pdf.docx");
	doc.write(out);
	out.close();
	reader.close();
	doc.close();
}
 
Example 6
Source File: TocTest.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws FileNotFoundException, IOException {
    XWPFDocument doc = new XWPFDocument(new FileInputStream("toc.docx"));

    XWPFStyles styles = doc.getStyles();
    System.out.println(styles);

    XWPFParagraph p = doc.createParagraph();
    XWPFRun run = p.createRun();
    run.setText("Heading 1");
    p.getCTP().addNewPPr();
    p.getCTP().getPPr().addNewPStyle();
    p.setStyle(HEADING1);

    XWPFParagraph tocPara = doc.createParagraph();
    CTP ctP = tocPara.getCTP();
    CTSimpleField toc = ctP.addNewFldSimple();
    toc.setInstr("TOC \\h");
    toc.setDirty(STOnOff.TRUE);

    System.out.println(doc.isEnforcedUpdateFields());

    // doc.enforceUpdateFields();

    // doc.createTOC();

    doc.write(new FileOutputStream("CreateWordBookmark.docx"));
    doc.close();
}
 
Example 7
Source File: SimpleTable.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public static void createSimpleTable() throws Exception {
    @SuppressWarnings("resource")
XWPFDocument doc = new XWPFDocument();

    XWPFTable table = doc.createTable(3, 3);

    table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");

    // table cells have a list of paragraphs; there is an initial
    // paragraph created when the cell is created. If you create a
    // paragraph in the document to put in the cell, it will also
    // appear in the document following the table, which is probably
    // not the desired result.
    XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);

    XWPFRun r1 = p1.createRun();
    r1.setBold(true);
    r1.setText("The quick brown fox");
    r1.setItalic(true);
    r1.setFontFamily("Courier");
    r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
    r1.setTextPosition(100);

    table.getRow(2).getCell(2).setText("only text");

    FileOutputStream out = new FileOutputStream("simpleTable.docx");
    doc.write(out);
    out.close();
}
 
Example 8
Source File: WordUtils.java    From java-tutorial with MIT License 4 votes vote down vote up
/**
 * 向word模板中填充数据,生成新word
 * templePath 是word模板的路径,outPutPath是输出路径
 * 注意模板是doc导出的就是doc,docx导出的是docx,否则会错误,
 *
 * @param mapData    填充的数据
 * @param templePath 模板路径
 * @throws Exception
 */
public static void generateWord(HashMap mapData, String templePath, String outPutPath) throws Exception {
    //因为空格无法输出,过滤一下空格 用-代替
    Iterator<Map.Entry<String, String>> iter = mapData.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry entry = iter.next();
        Object key = entry.getKey();
        Object val = entry.getValue();
        if ("".equals(val)) {
            //空格无法输出
            mapData.put(key, "-");
        }
    }

    //先创建文件
    File file = new File(outPutPath);
    if (file.exists()) {
        file.delete();
    }
    file.getParentFile().mkdirs();
    file.createNewFile();

    try {
        //获取文件后缀
        String suffix = getSuffix(templePath);
        if (suffix.equalsIgnoreCase(DOCX_SUFFIX)) {
            XWPFDocument doc = WordExportUtil.exportWord07(templePath, mapData);
            FileOutputStream fos = new FileOutputStream(file);
            doc.write(fos);
            fos.close();
        } else if (suffix.equalsIgnoreCase(DOC_SUFFIX)) {
            HWPFDocument hwpfDocument = new HWPFDocument(new FileInputStream(templePath));
            Range range = hwpfDocument.getRange();
            getRange(range, mapData);
            FileOutputStream stream = new FileOutputStream(file);
            hwpfDocument.write(stream);
            stream.flush();
            stream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 9
Source File: POIServices.java    From M2Doc with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Save the document into the file pointing at the given path.
 * 
 * @param uriConverter
 *            the {@link URIConverter uri converter} to use.
 * @param document
 *            the validated document to save.
 * @param theDestinationURI
 *            the {@link URI} were to save the content of the validated document.
 * @throws IOException
 *             throws if the writing of the {@link URI} fails.
 */
public void saveFile(URIConverter uriConverter, XWPFDocument document, URI theDestinationURI) throws IOException {
    try (OutputStream os = uriConverter.createOutputStream(theDestinationURI)) {
        document.write(os);
    }
}