81,095
社区成员
发帖
与我相关
我的任务
分享
public class WordAction extends DispatchAction
{
// 请求入口
public ActionForward saveword(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception
{
String filemodule="D:/bb.doc";
PrivilegeUser pr=new PrivilegeUser();
pr.setName(request.getParameter("name"));
pr.setSex(request.getParameter("sex"));
this.savewords(filemodule,pr.getName(),pr);
request.setAttribute("pr", pr);
return mapping.findForward("showxx");
}
public void savewords(String filemodule,String username,PrivilegeUser pu)
{
try
{
System.out.println("-------------");
WordBean wb=new WordBean();
wb.newDoce(filemodule, "d:/", username);//根据用户名创建一个word文件
String infile="d:/"+username+"个人资料.doc";
wb.replaceWord(infile,pu);//根据
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public class WordBean // extends Panel
{
Document docc =null;
public void newDoce(String modulefile, String newfilepath, String username) {
try {
docc = new Document();
docc.newDoc(modulefile, newfilepath + username + "个人资料.doc");
docc.close(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
public void replaceWord(String inFile, PrivilegeUser pu) {
try {
boolean flag = true;
String[] module = { "name", "sex" };
String[] pulist = { pu.getName(), pu.getSex(), pu.getPhoto() };
for (int i = 0; i < module.length; i++) {
if (flag) {
docc = new Document();
docc.open(inFile);
if (module[i].equals("pic")) {
File file = new File(pulist[i]);
flag = docc.insertimageAtBookmark(module[i], file);
} else
flag = docc.insertAtBookmark(module[i], pulist[i]);
docc.close(true);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
主要的处理方法
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
// Referenced classes of package com.heavenlake.wordapi:
// StyleWarning, TableStyleWarning
public class Document
{
private Dispatch doc;
private Dispatch documents;
// 选定的范围或插入点
private Dispatch selection;
private static String newFileName;
private ActiveXComponent word;
public Document()
{
word = new ActiveXComponent("Word.Application");
// word.setProperty("Visible", new Variant(false)); //不可见打开word
documents = word.getProperty("Documents").toDispatch();
// doc=Dispatch.call(documents, "Add").toDispatch();// create
}
private void close(Dispatch doc, boolean saveOnExit)
{
if (doc != null)
{
Dispatch.call(doc, "Close", new Variant(saveOnExit));
word.invoke("Quit", new Variant[0]);
ComThread.Release();
}
}
public void close(boolean saveOnExit) throws Exception
{
if (newFileName!=null&&newFileName.length() > 0)
{
if (saveOnExit)
{
saveAs(newFileName);
close(doc, false);
}
}
else
{
close(doc, saveOnExit);
}
}
private boolean find(Dispatch selection, String toFindText)
{
ActiveXComponent _tmp = word;
if (toFindText == null || toFindText.equals("")){
return false;
}
Dispatch find = ActiveXComponent.call(selection, "Find").toDispatch();
Dispatch.put(find, "Text", toFindText);
Dispatch.put(find, "Forward", "True");
Dispatch.put(find, "Format", "True");
Dispatch.put(find, "MatchCase", "True");
Dispatch.put(find, "MatchWholeWord", "False");
return Dispatch.call(find, "Execute").getBoolean();
}
public boolean find(String toFindText)
{
boolean flag = find(selection, toFindText);
return flag;
}
public void insert(File imageFile)
{
if (imageFile.exists())
{
replaceImage(selection, imageFile.getAbsolutePath());
}
}
public void insertAtBookmark(String bookmarkName, File imageFile)
{
if (imageFile.exists())
{
Dispatch bookmarkRange = getBookmark(bookmarkName);
replaceImage(bookmarkRange, imageFile.getAbsolutePath());
insertBookmark(bookmarkRange, bookmarkName);
}
else
{
System.out.println("图片文件不存在");
}
}
public void insertln()
{
Dispatch range = Dispatch.get(selection, "Range").toDispatch();
Dispatch.call(range, "InsertParagraphAfter");
moveDown(1);
}
public void newDoc(String template, String fileName)
{
File file = new File(template);
if (file.exists())
{
doc = Dispatch.call(documents, "Open", template).toDispatch();
newFileName = fileName;//命名文件,但没把文件保存.
}
else
{
System.out.println("模板文件不存在");
}
}
public void newDoc(String fileName) throws Exception
{
doc = Dispatch.call(documents, "Open").toDispatch();
selection = Dispatch.get(word, "Selection").toDispatch();
newFileName = fileName;
}
public void open(String fileName)
{
File file = new File(fileName);
if (file.exists())
{
doc = Dispatch.call(documents, "Open", fileName).toDispatch();
selection = Dispatch.get(word, "Selection").toDispatch();
}
else
{
System.out.println("文件不存在,不能打开");
}
}
public List readTable(int tbIndex, int fromRow) throws Exception
{
ArrayList tableData = new ArrayList();
if (fromRow < 1)
{
fromRow = 1;
}
if (tbIndex < 1)
{
tbIndex = 1;
}
Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
int tablesCount = Dispatch.get(tables, "Count").getInt();
if (tablesCount < tbIndex)
{
throw new Exception(
"\u6307\u5B9A\u7684\u8868\u683C\u4E0D\u5B58\u5728\u3002\u6253\u5F00\u7684\u6587\u6863"
+ "\u4E2D\u6CA1\u6709\u8868\u683C\u3002");
}
if (tablesCount < tbIndex)
{
throw new Exception(
"\u6307\u5B9A\u7684\u8868\u683C\u4E0D\u5B58\u5728\u3002\u6253\u5F00\u7684\u6587\u6863"
+ "\u4E2D\u53EA\u6709" + tablesCount + "\u5F20\u8868\u3002");
}
Dispatch table = Dispatch.call(tables, "Item", new Variant(tbIndex)).toDispatch();
Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
int rowsCount = Dispatch.get(rows, "Count").getInt();
Dispatch Columns = Dispatch.get(table, "Columns").toDispatch();
int Columnscount = Dispatch.get(Columns, "Count").toInt();
for (int i = fromRow; i < rowsCount + 1; i++)
{
ArrayList rowData = new ArrayList();
for (int j = 1; j < Columnscount + 1; j++)
{
Dispatch cell = Dispatch.call(table, "Cell", i + "", j + "").toDispatch();
Dispatch Range = Dispatch.get(cell, "Range").toDispatch();
String text = Dispatch.get(Range, "Text").toString();
rowData.add(text.substring(0, text.length() - 1));
}
tableData.add(rowData);
}
return tableData;
}
public boolean insertAtBookmark(String bookmarkName, String newText)
{
boolean flag = insertAtBookmark(bookmarkName, newText, null);
return flag;
}
public boolean insertAtBookmark(String bookmarkName, String newText, String styleName)
{
try
{
Dispatch bookmarkRange;
bookmarkRange = getBookmark(bookmarkName);
replace(newText);
insertBookmark(bookmarkRange, bookmarkName);
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
private Dispatch getBookmark(String bookmarkName)
{
//获取bookmarkName的标签
Dispatch bookmark = Dispatch.call(doc, "Bookmarks", bookmarkName).toDispatch();
bookmark = Dispatch.get(bookmark, "Range").toDispatch();//在全局的范围内获取
return bookmark;
}
private void replace( String newText)
{
Dispatch.put(selection, "Text", newText);
}
private void insertBookmark(Dispatch selection, String bookmarkName)
{//往dispatch对象添加一个书签对象.
Dispatch Bookmarks = Dispatch.get(doc, "Bookmarks").toDispatch();
Dispatch.call(Bookmarks, "Add", bookmarkName, selection);
}
public boolean replace(String toFindText, String newText)
{
if (!find(toFindText))
return false;
boolean leaf = false;
boolean flag = find(toFindText);
if (flag)
{
replace( newText);
leaf = true;
}
return leaf;
}
/**
* 全局替换文本
*
* @param toFindText
* 查找字符串
* @param newText
* 要替换的内容
*/
private void replaceAll(Dispatch selection, String oldText, String newText)
{
moveStart(selection);
for (; find(selection, oldText); Dispatch.call(selection, "MoveRight"))
{
replace( newText);
}
}
public void replaceAll(String oldText, String newText)
{
replaceAll(selection, oldText, newText);
}
public void saveAs(String outputFile)
{
Dispatch.call(doc, "SaveAs", outputFile);
}
public boolean insertimageAtBookmark(String bookmarkName, File imageFile) throws Exception
{
//插入图片
if (imageFile.exists())
{
Dispatch bookmarkRange = getBookmark(bookmarkName);//获取指定的标签对象
replaceImage(bookmarkRange, imageFile.getAbsolutePath());
insertBookmark(bookmarkRange, bookmarkName);
return true;
}else
{
return false;
}
}
private void replaceImage(Dispatch selection, String imagePath)
{//入word里边添加图片s
Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(), "AddPicture", imagePath);
}
}
com.jacob.com.ComFailException: Invoke of: Add
Source: Microsoft Word
Description: Word
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:804)
at com.jacob.com.Dispatch.callN(Dispatch.java:440)
at com.jacob.com.Dispatch.call(Dispatch.java:520)
at com.word.util.Document.newDoc(Document.java:176)
at com.word.util.WordBean.newDoce(WordBean.java:11)
at com.word.action.WordAction.savewords(WordAction.java:56)
at com.word.action.WordAction.saveword(WordAction.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)
java.lang.NullPointerException