jcom生成一个word文件

jcom生成,你可以定义字体的样式
public synchronized void createWord(String filePath, String text,
			String fontName, String fontSize, Boolean fontBold, int align) {
		ReleaseManager rm = new ReleaseManager();
		IDispatch docApp = null;
		try {
			docApp = new IDispatch(rm, "Word.Application");

			IDispatch documents = (IDispatch) docApp.get("Documents");

			documents.method("add", null);

			IDispatch selection = ((IDispatch) docApp.get("Selection"));

			IDispatch paragraphFormat = ((IDispatch) selection
					.get("ParagraphFormat"));
			paragraphFormat.put("Alignment", new Integer(align));// 对齐方式0:left,1:center,2:right
			IDispatch font = ((IDispatch) selection.get("Font"));
			font.put("name", fontName);
			font.put("Size", fontSize);
			font.put("Bold", fontBold);
			selection.method("TypeText", new Object[] { text });

			((IDispatch) docApp.get("ActiveDocument")).method("saveAs",
					new Object[] { filePath, new Integer(0) });

		} catch (JComException e) {
			e.printStackTrace();
		} finally {
			try {
				if (docApp != null) {
					((IDispatch) docApp.get("ActiveDocument")).put("Saved",
							new Boolean(true));

					docApp.method("quit", null);
					docApp = null;
				}
				rm.release();
				rm = null;
			} catch (JComException e) {

				e.printStackTrace();
			}
		}

	}

你可能感兴趣的:(jcom)