1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java页脚两行_java jacob 多个word合并 页眉页脚怎么生成???

java页脚两行_java jacob 多个word合并 页眉页脚怎么生成???

时间:2020-04-22 19:27:30

相关推荐

java页脚两行_java jacob 多个word合并  页眉页脚怎么生成???

java jacob 多个word合并 页眉页脚怎么生成???、

生成出来的 页脚都不对...

数量不对 还有重复的 萌新求解答....

String dest=filePath.substring(0,filePath.indexOf("."))+"-副本.doc";

FileChannel inputChannel = null;

FileChannel outputChannel = null;

try {

inputChannel = new FileInputStream(filePath).getChannel();

outputChannel = new FileOutputStream(dest).getChannel();

outputChannel.transferFrom(inputChannel, 0, inputChannel.size());

}catch (IOException e) {

e.printStackTrace();

}

finally {

try {

outputChannel.close();

inputChannel.close();

} catch (IOException e) {

e.printStackTrace();

}

}

boolean flag=true;

//打开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) dest,

new Variant(false), new Variant(true) },

new int[3]).toDispatch();

//获得当前文档选定部分或光标所在位置

Dispatch selection = app.getProperty("Selection").toDispatch();

//光标定位到文档首位置

Dispatch.call(selection, "HomeKey", new Variant(6));

//在光标所在位置插入文本

//光标定位到文档末尾位置

Dispatch.call(selection, "EndKey", new Variant(6));

Dispatch.call(selection, "TypeParagraph");

//插入一个分页符

Dispatch.call(selection, "InsertBreak");

int i = 0;

//追加文件

for (TestingInstanceDetailAttach attach:attachList){

i++;

String path=attach.getUpload().getPath();

if(StringUtils.isNotBlank(path)){

File file = new File(path);

if(file.exists()&&path.endsWith("doc")){

//每一篇文档前添加报送类型、单位和标题

//文档内容

Dispatch.invoke(selection,

"insertFile", Dispatch.Method, new Object[] {

(String)path, "",

new Variant(false), new Variant(false),

new Variant(false) }, new int[3]);

//每一篇文档后添加分页符

Dispatch.call(selection, "EndKey", new Variant(6));

Dispatch.call(selection, "TypeParagraph");

}

}

}

//保存新的word文件

File folder = new File(filePath.substring(0, filePath.lastIndexOf("/")+1));

if(!folder.exists() && !folder.isDirectory()){

folder.mkdirs();

}

Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method,

new Object[] { filePath, new Variant(1) }, new int[3]);

Variant f = new Variant(false);

Dispatch.call((Dispatch) doc, "Close", f);

} catch (Exception e) {

flag=false;

throw new RuntimeException("合并word文件出错.原因:" + e);

} finally {

app.invoke("Quit", new Variant[] {});

if (!dest.endsWith(File.separator))

dest = dest + File.separator;

File dirFile = new File(dest);

if (!dirFile.exists()) {

System.out.println("删除目录失败:" + dest + "不存在!");

return false;

}else{

dirFile.delete();

}

}

return flag;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。