public class JavaToWords {
/**
* word运行程序对象
*/
private ActiveXComponent word;
/**
* 选定内容
* @return Dispatch 选定的范围或插入点
*/
public Dispatch select() {
return word.getProperty("Selection").toDispatch();
}
public void toWord(String seekView) {
Dispatch selection = select();
//设置页眉
if(seekView != null && !"".equals(seekView)){
//取得活动窗体对象
Dispatch ActiveWindow = word.getProperty("ActiveWindow").toDispatch();
//取得活动窗格对象
Dispatch ActivePane = Dispatch.get(ActiveWindow,"ActivePane").toDispatch();
//取得视窗对象
Dispatch View = Dispatch.get(ActivePane, "View").toDispatch();
try{
Dispatch.put(View,"SeekView", "9"); //设置页眉
Dispatch.put(selection,"Text",seekView);
Dispatch.put(View, "SeekView", "10"); // 10是设置页脚
Dispatch.put(selection, "Text", seekView); //
}finally{
if(ActiveWindow != null ) ActiveWindow.safeRelease();
if(ActivePane != null ) ActivePane.safeRelease();
if(View != null ) View.safeRelease();
}
}
}
}
希望对你有些帮助,不过好像要下一个外部资源包,叫jacob 的,我也记的不太清楚了,应该是这个,你找找看
free Spire.doc for java都能实现,参考解决方案:Java 添加Word页眉页脚,Java 添加Word 目录
利用itext 2.1.7 实现的
Paragraph parafooter = new Paragraph();
footerFont = new RtfFont("宋体_GB2312", fontsize, fontStyle, Color.BLACK);
parafooter.setFont(new Font(footerFont));
parafooter.add(new Phrase("第"));
parafooter.add(new RtfPageNumber());
parafooter.add(new Phrase("页 共"));
parafooter.add(new RtfTotalPageNumber());
parafooter.add(new Phrase("页"));
RtfHeaderFooter footer = new RtfHeaderFooter(parafooter);
footer.setAlignment(Element.ALIGN_CENTER);
footer.setBorder(Rectangle.NO_BORDER);
document.setFooter(footer);
集成PageOffice,然后通过PageOffice调用word的VBA接口就可以实现了。
前提是VBA可以做到,不过看你说的这个需求,VBA是可以做到的。
用PageOffice很简单就能实现。