word转换html

这是我在工作中碰到的一个需求,一开始我做的比较死,直接返回排版好的页面,后来客户需要修改这个页面的一些东西,问题来了,我不能改变一次就对页面进行一次修改吧。

后来琢磨着把这个功能做成活的,可以让用户自己上传一个word,在页面查看的时候,自动生成html,这样会更人性化一点,于是乎有了下面的代码。


前提是需要引入jar包,poi 3.8及以上的版本。


public class Word2HtmlUtil {

	private static String deamonStr = "";

	private static String realPathStr = "";

	public static void main(String argv[]) {
		try {
			convert2Html("C:\\Users\\admin\\Documents\\Tencent Files\\917997617\\FileRecv\\学院职业技能鉴定工作流程(修改).doc",
					"C:\\Users\\admin\\Documents\\Tencent Files\\917997617\\FileRecv\\学院职业技能鉴定工作流程(修改).html", "", null, false);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 把内容写到html中
	 * @param content
	 * @param path
	 * @param replace
	 */
	public static void writeFile(String content, String path, boolean replace) {
		FileOutputStream fos = null;
		BufferedWriter bw = null;
		try {
			File file = new File(path);
			fos = new FileOutputStream(file);
			bw = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));
			if (replace) {// 如果需要取消标题设为true
				try {
					String beginStr = "<span";
					String endStr = "</span>";
					int startIndex = content.indexOf(beginStr, 0) + beginStr.length();
					int startIndex_1 = content.indexOf(">", startIndex);
					if (startIndex_1 > startIndex) {
						beginStr = "<span class=\"s1\"";
					}
					int endIndex = content.indexOf(endStr, startIndex_1);
					String title = content.substring(startIndex_1, endIndex);
					content = content.replace(beginStr + title + endStr, "");
				}
				catch (Exception e) {
				}
			}
			bw.write(content);
		}
		catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace();
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		}
		finally {
			try {
				if (bw != null)
					bw.close();
				if (fos != null)
					fos.close();
			}
			catch (IOException ie) {
			}
		}
	}

	/**
	 * word转换为html,目前只支持2003
	 * @param fileName:读取的word路径
	 * @param outPutFile:输入路径
	 * @param deamon:域名
	 * @param realPath:项目路径
	 * @param replace:是否去除标题
	 */
	public static void convert2Html(String fileName, String outPutFile, String deamon, String realPath, boolean replace) {
		deamonStr = deamon;
		realPathStr = realPath;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));

			WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
			wordToHtmlConverter.setPicturesManager(new PicturesManager() {
				public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {
					String imgPath = realPathStr + "picTmp" + File.separator + suggestedName;// 图片保存路径
					File file = new File(imgPath);
					try {
						if (!file.exists()) {
							if (!file.getParentFile().exists()) {
								if (file.getParentFile().mkdirs()) {
									file.createNewFile();
								}
							}
							else if (!file.exists()) {
								file.createNewFile();
							}
						}
						OutputStream os = new FileOutputStream(file);
						os.write(content);
						os.close();
					}
					catch (Exception e) {
						e.printStackTrace();
					}
					return "http://" + deamonStr + "/picTmp/" + suggestedName;// 图片在页面显示的路径
				}
			});
			wordToHtmlConverter.processDocument(wordDocument);
			// save pictures
			List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();
			if (pics != null) {

				for (int i = 0; i < pics.size(); i++) {
					Picture pic = pics.get(i);
					try {
						pic.writeImageContent(new FileOutputStream(realPathStr + "picTmp" + File.separator + pic.suggestFullFileName()));
					}
					catch (FileNotFoundException e) {
						e.printStackTrace();
					}
				}
			}
			Document htmlDocument = wordToHtmlConverter.getDocument();
			DOMSource domSource = new DOMSource(htmlDocument);
			StreamResult streamResult = new StreamResult(out);

			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer serializer = tf.newTransformer();
			serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			serializer.setOutputProperty(OutputKeys.INDENT, "yes");
			serializer.setOutputProperty(OutputKeys.METHOD, "html");
			serializer.transform(domSource, streamResult);
			out.close();
			writeFile(new String(out.toByteArray()), outPutFile, replace);
		}
		catch (Exception e) {
			e.printStackTrace();
		}

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

火柴有猿

您的鼓励,将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值