首页 > 代码库 > Java Jacob 写Word

Java Jacob 写Word

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class JacobWord {

	public static void main(String[] args) {

		// String sDir = "F:\\";
		// String sInputDoc = sDir + "file_in.doc";
		// String sOldText = "[label:import:1]";
		// String sNewText =
		// "I am some horribly long sentence, so long that [insert bullshit here]";
		boolean tVisible = false;
		boolean tSaveOnExit = false;

		// 获取Word操作对象
		ActiveXComponent oWord = new ActiveXComponent("Word.Application");

		// 打开Word程序
		oWord.setProperty("Visible", new Variant(tVisible));

		// 获得文档区
		Dispatch oDocuments = oWord.getProperty("Documents").toDispatch();

		// 打开指定的文档
		// Dispatch oDocument = Dispatch.call(oDocuments, "Open",
		// sInputDoc).toDispatch();

		// 新建文档
		Dispatch oDocument = Dispatch.call(oDocuments, "Add").toDispatch();

		// 获取当前文档
		Dispatch oSelection = oWord.getProperty("Selection").toDispatch();

		// 获取查找对象
		// Dispatch oFind = oWord.call(oSelection, "Find").toDispatch();

		// 查找替换
		// Dispatch.put(oFind, "Text", sOldText);
		// Dispatch.call(oFind, "Execute");
		// Dispatch.put(oSelection, "Text", sNewText);

		// 插入大标题
		Dispatch.put(oSelection, "Text", "大标题");
		Dispatch oFont = Dispatch.get(oSelection, "Font").toDispatch();
		Dispatch oAlign = Dispatch.get(oSelection, "ParagraphFormat").toDispatch();
		Dispatch.put(oFont, "Bold", "1");
		Dispatch.put(oFont, "Italic", "0");
		Dispatch.put(oFont, "Underline", "0");
		Dispatch.put(oFont, "Size", new Variant(18));
		Dispatch.put(oFont, "Name", new Variant("宋体"));
		Dispatch.put(oAlign, "Alignment", "1");

		// 插入小标题
		Dispatch.call(oSelection, "MoveDown");
		Dispatch.put(oSelection, "Text", "\n小标题\n");
		oFont = Dispatch.get(oSelection, "Font").toDispatch();
		oAlign = Dispatch.get(oSelection, "ParagraphFormat").toDispatch();
		Dispatch.put(oFont, "Bold", "1");
		Dispatch.put(oFont, "Italic", "0");
		Dispatch.put(oFont, "Underline", "0");
		Dispatch.put(oFont, "Size", new Variant(14));
		Dispatch.put(oAlign, "Alignment", "1");

		// 插入正文
		Dispatch.call(oSelection, "MoveDown");
		Dispatch.put(oSelection, "Text", "(一)正文。");
		oFont = Dispatch.get(oSelection, "Font").toDispatch();
		oAlign = Dispatch.get(oSelection, "ParagraphFormat").toDispatch();
		Dispatch.put(oFont, "Bold", "1");
		Dispatch.put(oFont, "Italic", "0");
		Dispatch.put(oFont, "Underline", "0");
		Dispatch.put(oFont, "Size", new Variant(10));
		Dispatch.put(oAlign, "Alignment", "0");
		Dispatch.call(oSelection, "MoveDown");
		Dispatch.put(oSelection, "Text", "\n(二)正文。");
		Dispatch.call(oSelection, "MoveDown");
		Dispatch.put(oSelection, "Text", "\n(三)正文。\n");

		// 插入结尾
		Dispatch.call(oSelection, "MoveDown");
		Dispatch.put(oSelection, "Text", "\t\t\t\t\t\t\t\t\t\t1.结尾");
		oFont = Dispatch.get(oSelection, "Font").toDispatch();
		oAlign = Dispatch.get(oSelection, "ParagraphFormat").toDispatch();
		Dispatch.put(oFont, "Bold", "0");
		Dispatch.put(oFont, "Italic", "0");
		Dispatch.put(oFont, "Underline", "0");
		Dispatch.put(oFont, "Size", new Variant(8));
		Dispatch.put(oAlign, "Alignment", "0");
		Dispatch.call(oSelection, "MoveDown");
		Dispatch.put(oSelection, "Text", "\n\t\t\t\t\t\t\t\t\t\t2.结尾");
		Dispatch.call(oSelection, "MoveDown");
		Dispatch.put(oSelection, "Text", "\n\t\t\t\t\t\t\t\t\t\t3.结尾\n");

		// 插入时间
		Dispatch.call(oSelection, "MoveDown");
		Dispatch.put(oSelection, "Text", "2014-09-26");
		oFont = Dispatch.get(oSelection, "Font").toDispatch();
		oAlign = Dispatch.get(oSelection, "ParagraphFormat").toDispatch();
		Dispatch.put(oFont, "Bold", "0");
		Dispatch.put(oFont, "Italic", "0");
		Dispatch.put(oFont, "Underline", "0");
		Dispatch.put(oFont, "Size", new Variant(8));
		Dispatch.put(oAlign, "Alignment", "2");

		// 保存并关闭
		Dispatch.call(oDocument, "SaveAs", new Variant("F://" + System.nanoTime() + ".doc"));
		Dispatch.call(oDocument, "Close", new Variant(tSaveOnExit));

		// 退出Word程序
		oWord.invoke("Quit", new Variant[0]);

		// // 插入图片
		// String sImgFile = sDir + "image.png";
		// Dispatch.call(oSelection, "MoveDown");
		// Dispatch oImage = Dispatch.get(oSelection,
		// "InLineShapes").toDispatch();
		// Dispatch.call(oImage, "AddPicture", sImgFile);
		//
		// // 插入超链接
		// String sHyperlink = "http://www.google.com";
		// Dispatch.put(oSelection, "Text", "Text for the link to Google");
		// Object oRange = Dispatch.call(oSelection, "Range");
		// Dispatch oLink = Dispatch.get(oDocument, "Hyperlinks").toDispatch();
		// Dispatch.call(oLink, "Add", oRange, sHyperlink);
	}

}

Java Jacob 写Word