package com.szjy.word;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.ume.framework.base.BaseDispatchAction;
import com.ume.framework.coreapp.orgmanage.User;
import com.ume.framework.util.Pub;
public class WordMerge extends BaseDispatchAction {
public ActionForward Word(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try{
String basePath = request.getRealPath("/")+"word\\";
User user = Pub.getUser(request);
String rybh = user.getUserSN();
String xxdm = user.getDepartment();
List list = new ArrayList();
String file1 =basePath+"jbqk"+rybh+".doc";
String file2 =basePath+"sxpd"+rybh+".doc";
String file3= basePath+"xysp"+rybh+".doc";
String file4= basePath+"sxjk"+rybh+".doc";
String file5= basePath+"yssy"+rybh+".doc";
String file6= basePath+"shsj"+rybh+".doc";
String file7= basePath+"zwcs"+rybh+".doc";
String file8= basePath+"jspy"+rybh+".doc";
String file9= basePath+"zzcl"+rybh+".doc";
//
list.add(file9);
list.add(file1);
list.add(file2);
list.add(file3);
list.add(file4);
list.add(file5);
list.add(file6);
list.add(file7);
list.add(file8);
String gkWord = basePath+"gkhz\\"+"gk_"+xxdm+"_"+rybh+".doc";
String worName = "gk_"+xxdm+"_"+rybh+".doc";
if(uniteDoc(list,gkWord)){
return mapping.findForward("gkhz");
}else{
//Pub.writeXmlErrorMessage(response, "意外错误!!");
return null;
}
}
catch (Exception e) {
//log.error(e, e);
e.printStackTrace();
//Pub.writeXmlErrorMessage(response, "意外错误!!" + e.toString());
PrintWriter out = response.getWriter();
out.println("<br/> <font style='color:red'>汇总失败,请检查是否已经生成全部组成项或文件已被占用!</font>");
out.flush();
} finally {
}
return null;
}
public boolean uniteDoc(List fileList, String savepaths)
throws Exception {
if (fileList.size() == 0 || fileList == null) {
return false;
}
//打开word
ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
try {
// 设置word不可见
app.setProperty("Visible", new Variant(false));
//获得documents对象
Object docs = app.getProperty("Documents").toDispatch();
//打开第一个文件
Object doc = Dispatch
.invoke(
(Dispatch) docs,
"Open",
Dispatch.Method,
new Object[] { (String) fileList.get(0),
new Variant(false), new Variant(true) },
new int[3]).toDispatch();
//追加文件
for (int i = 1; i < fileList.size(); i++) {
Dispatch.invoke(app.getProperty("Selection").toDispatch(),
"insertFile", Dispatch.Method, new Object[] {
(String) fileList.get(i), "",
new Variant(false), new Variant(false),
new Variant(false) }, new int[3]);
}
//保存新的word文件
Dispatch selection=Dispatch.get(app,"Selection").toDispatch();
Dispatch align=Dispatch.get(selection,"ParagraphFormat").toDispatch();;
Dispatch ActiveWindow=app.getProperty("ActiveWindow").toDispatch();
Dispatch ActivePane=Dispatch.get(ActiveWindow,"ActivePane").toDispatch();
Dispatch.put(align,"Alignment","1");
// Dispatch.put(selection,"Text",seekView);
Dispatch documents = Dispatch.get(app,"Documents").toDispatch();
Dispatch document = Dispatch.call(documents,"Add").toDispatch();
final Dispatch range = Dispatch.get(selection, "Range").toDispatch();
final Dispatch fields = Dispatch.get(range, "Fields").toDispatch();
Dispatch paragraphFormat=Dispatch.get(selection,"ParagraphFormat").getDispatch();
Dispatch.put(paragraphFormat, "Alignment", 1);
Dispatch.call(fields, "Add", range, new Variant(-1), "NumPages",true).toDispatch();
Dispatch.call(selection, "TypeText", "/");
Dispatch.call(fields, "Add", range, new Variant(-1), "Page", true).toDispatch();
Dispatch font = Dispatch.get(range, "Font").toDispatch();
Dispatch.put(font,"Name",new Variant("宋体_GB2312"));
Dispatch.put(font, "Bold", new Variant(true));
Dispatch.put(font, "Size", 9);
Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method,new Object[] { savepaths, new Variant(1) }, new int[3]);
Variant f = new Variant(false);
Dispatch.call((Dispatch) doc, "Close", f);
} catch (Exception e) {
throw new RuntimeException("合并word文件出错.原因:" + e);
} finally {
app.invoke("Quit", new Variant[] {});
}
System.out.print("success!");
return true;
}
}
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.ume.framework.base.BaseDispatchAction;
import com.ume.framework.coreapp.orgmanage.User;
import com.ume.framework.util.Pub;
public class WordMerge extends BaseDispatchAction {
public ActionForward Word(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try{
String basePath = request.getRealPath("/")+"word\\";
User user = Pub.getUser(request);
String rybh = user.getUserSN();
String xxdm = user.getDepartment();
List list = new ArrayList();
String file1 =basePath+"jbqk"+rybh+".doc";
String file2 =basePath+"sxpd"+rybh+".doc";
String file3= basePath+"xysp"+rybh+".doc";
String file4= basePath+"sxjk"+rybh+".doc";
String file5= basePath+"yssy"+rybh+".doc";
String file6= basePath+"shsj"+rybh+".doc";
String file7= basePath+"zwcs"+rybh+".doc";
String file8= basePath+"jspy"+rybh+".doc";
String file9= basePath+"zzcl"+rybh+".doc";
//
list.add(file9);
list.add(file1);
list.add(file2);
list.add(file3);
list.add(file4);
list.add(file5);
list.add(file6);
list.add(file7);
list.add(file8);
String gkWord = basePath+"gkhz\\"+"gk_"+xxdm+"_"+rybh+".doc";
String worName = "gk_"+xxdm+"_"+rybh+".doc";
if(uniteDoc(list,gkWord)){
return mapping.findForward("gkhz");
}else{
//Pub.writeXmlErrorMessage(response, "意外错误!!");
return null;
}
}
catch (Exception e) {
//log.error(e, e);
e.printStackTrace();
//Pub.writeXmlErrorMessage(response, "意外错误!!" + e.toString());
PrintWriter out = response.getWriter();
out.println("<br/> <font style='color:red'>汇总失败,请检查是否已经生成全部组成项或文件已被占用!</font>");
out.flush();
} finally {
}
return null;
}
public boolean uniteDoc(List fileList, String savepaths)
throws Exception {
if (fileList.size() == 0 || fileList == null) {
return false;
}
//打开word
ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
try {
// 设置word不可见
app.setProperty("Visible", new Variant(false));
//获得documents对象
Object docs = app.getProperty("Documents").toDispatch();
//打开第一个文件
Object doc = Dispatch
.invoke(
(Dispatch) docs,
"Open",
Dispatch.Method,
new Object[] { (String) fileList.get(0),
new Variant(false), new Variant(true) },
new int[3]).toDispatch();
//追加文件
for (int i = 1; i < fileList.size(); i++) {
Dispatch.invoke(app.getProperty("Selection").toDispatch(),
"insertFile", Dispatch.Method, new Object[] {
(String) fileList.get(i), "",
new Variant(false), new Variant(false),
new Variant(false) }, new int[3]);
}
//保存新的word文件
Dispatch selection=Dispatch.get(app,"Selection").toDispatch();
Dispatch align=Dispatch.get(selection,"ParagraphFormat").toDispatch();;
Dispatch ActiveWindow=app.getProperty("ActiveWindow").toDispatch();
Dispatch ActivePane=Dispatch.get(ActiveWindow,"ActivePane").toDispatch();
Dispatch View=Dispatch.get(ActivePane,"View").toDispatch();
/// 页脚自动生成页码
Dispatch.put(View,"SeekView","10");Dispatch.put(align,"Alignment","1");
// Dispatch.put(selection,"Text",seekView);
Dispatch documents = Dispatch.get(app,"Documents").toDispatch();
Dispatch document = Dispatch.call(documents,"Add").toDispatch();
final Dispatch range = Dispatch.get(selection, "Range").toDispatch();
final Dispatch fields = Dispatch.get(range, "Fields").toDispatch();
Dispatch paragraphFormat=Dispatch.get(selection,"ParagraphFormat").getDispatch();
Dispatch.put(paragraphFormat, "Alignment", 1);
Dispatch.call(fields, "Add", range, new Variant(-1), "NumPages",true).toDispatch();
Dispatch.call(selection, "TypeText", "/");
Dispatch.call(fields, "Add", range, new Variant(-1), "Page", true).toDispatch();
Dispatch font = Dispatch.get(range, "Font").toDispatch();
Dispatch.put(font,"Name",new Variant("宋体_GB2312"));
Dispatch.put(font, "Bold", new Variant(true));
Dispatch.put(font, "Size", 9);
Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method,new Object[] { savepaths, new Variant(1) }, new int[3]);
Variant f = new Variant(false);
Dispatch.call((Dispatch) doc, "Close", f);
} catch (Exception e) {
throw new RuntimeException("合并word文件出错.原因:" + e);
} finally {
app.invoke("Quit", new Variant[] {});
}
System.out.print("success!");
return true;
}
}