You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by weezy8 <we...@i.ua> on 2012/01/24 13:55:50 UTC

Re: Help in XWPF POI code

Here is my Java code for reading docx document, maybe it helps somebody:

/**
	 * Read content of *.doc file
	 * @param fileName - full name of target file
	 * @return - {@code String} representation of file content
	 */
	public String readDocxFileToString(String fileName) {
		//String content = "";
		StringBuilder content = new StringBuilder();
		File file = new File(fileName);

		String filePath = FileUtil.getAbsolutePath(fileName);
		log.debug(this.toString() + "; Opening file: " + filePath);

		XWPFDocument doc;
		try {

			doc = new XWPFDocument(new FileInputStream(file));

			List<XWPFParagraph> paragraphs = doc.getParagraphs();

			for (XWPFParagraph par : paragraphs) {

				List<XWPFRun> runs = par.getRuns();

				for (XWPFRun run : runs) {

					//content += run.getText(0);
					content = content.append(run.getText(0));

				}
			}

		} catch (FileNotFoundException ex) {
			System.out.println("File was not found :: " + filePath);
		} catch (IOException e) {
			System.out.println("Failed to close '" + filePath + "' file output
stream");
		}

		return content.toString();
		
	}

 

--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Help-in-XWPF-POI-code-tp3287261p5281585.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org