




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
项目开发过程中,需求涉及到了各种文档转换为HTML或者网页易显示格式,现在将实现方式整理如下: 一、了解Jacob先了解一下概念,JACOB 就是 JAVA-COM Bridge的缩写,提供自动化的访问com的功能,也是通过JNI功能访问windows平台下的com组件或者win32系统库的。这是一个开始于1999年的开源项目的成果,有很多使用者对该项目进行了修改,做出了自己的贡献。下载地址:/project/showfiles.php?group_id=109543&package_id=118368二、Jacob安装1、我们解开下载的jacob_1.9.zip,在文件夹中找到jacob.dll和jacob.jar两个文件2、将压缩包解压后,Jacob.jar添加到Libraries中;3、将Jacob.dll放至“WINDOWSSYSTEM32”下面。需要注意的是: 【使用IDE启动Web服务器时,系统读取不到Jacob.dll,例如用MyEclipse启动Tomcat,就需要将dll文件copy到MyEclipse(经本人实践,发现此处应为JDK,不然会有问题哦)安装目录的“jrebin”下面。最后需要关闭MyEclipse,再重启。一般系统没有加载到Jacob.dll文件时,报错信息为:“java.lang.UnsatisfiedLinkError: no jacob in java.library.path”】三、使用Jacob转换Word,Excel为HTML JAVA代码:import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import com.jacob.activeX.ActiveXComponent;import .Dispatch;import .Variant;public class TransformFiletoHtmlint WORD_HTML = 8;int WORD_TXT = 7;int EXCEL_HTML = 44;/* * WORD转HTML * param docfile WORD文件全路径 * param htmlfile 转换后HTML存放路径 */public void wordToHtml(String docfile, String htmlfile)ActiveXComponent app = new ActiveXComponent(Word.Application); / 启动wordtryapp.setProperty(Visible, new Variant(false);Dispatch docs = app.getProperty(Documents).toDispatch();Dispatch doc = Dispatch.invoke(docs,Open,Dispatch.Method,new Object docfile, new Variant(false),new Variant(true) , new int1).toDispatch();Dispatch.invoke(doc, SaveAs, Dispatch.Method, new Object htmlfile, new Variant(WORD_HTML) , new int1);Variant f = new Variant(false);Dispatch.call(doc, Close, f);catch (Exception e)e.printStackTrace();finallyapp.invoke(Quit, new Variant );/* * EXCEL转HTML * param xlsfile EXCEL文件全路径 * param htmlfile 转换后HTML存放路径 */public void excelToHtml(String xlsfile, String htmlfile)ActiveXComponent app = new ActiveXComponent(Excel.Application); / 启动exceltryapp.setProperty(Visible, new Variant(false);Dispatch excels = app.getProperty(Workbooks).toDispatch();Dispatch excel = Dispatch.invoke(excels,Open,Dispatch.Method,new Object xlsfile, new Variant(false),new Variant(true) , new int1).toDispatch();Dispatch.invoke(excel, SaveAs, Dispatch.Method, new Object htmlfile, new Variant(EXCEL_HTML) , new int1);Variant f = new Variant(false);Dispatch.call(excel, Close, f);catch (Exception e)e.printStackTrace();finallyapp.invoke(Quit, new Variant );/* * /删除指定文件夹 * param folderPath 文件夹全路径 * param htmlfile 转换后HTML存放路径 */ public void delFolder(String folderPath) try delAllFile(folderPath); /删除完里面所有内容 String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); myFilePath.delete(); /删除空文件夹 catch (Exception e) e.printStackTrace();/* * /删除指定文件夹下所有文件 * param path 文件全路径 */public boolean delAllFile(String path) boolean flag = false; File file = new File(path); if (!file.exists() return flag; if (!file.isDirectory() return flag; String tempList = file.list(); File temp = null; for (int i = 0; i tempList.length; i+) if (path.endsWith(File.separator) temp = new File(path + tempListi); else temp = new File(path + File.separator + tempListi); if (temp.isFile() temp.delete(); if (temp.isDirectory() delAllFile(path + / + tempListi);/先删除文件夹里面的文件 delFolder(path + / + tempListi);/再删除空文件夹 flag = true; return flag; 调用JAVA代码:public class Test1 public static void main(String args) / TODO Auto-generated method stubTransformFiletoHtml trans = new TransformFiletoHtml();trans.wordToHtml(D:sinye.doc, D:sinye.html);package com.bperp.word.util;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;public class WordWriter private WordOperator word;public WordWriter(String filePath)word=new WordOperator();word.openDocument(filePath);public WordWriter(InputStream input,String filePath,String fileName) throws ExceptionString path=saveAsDocFile(input,filePath,fileName);word=new WordOperator();word.openDocument(path);/* * 将word文档输入流保存为本地得到word文件 * param input * param filePath * param fileName * throws Exception */SuppressWarnings(unused)private String saveAsDocFile(InputStream input,String filePath,String fileName)throws Exceptionif(!StringUtils.isValidateString(filePath)|!StringUtils.isValidateString(fileName)throw new Exception(The filePath or fileName is error);if(input=null)throw new Exception(InputStream is null);File file=new File(filePath);if(!file.exists()throw new Exception( The FilePath is null);filePath = validateFilePath(filePath);fileName = getRandomFileName(fileName); InputStreamin=null; OutputStream out=null; try in=new BufferedInputStream(input);out=new BufferedOutputStream(new FileOutputStream(filePath+fileName);byte b=new byte1024;for(int p=0; (p=in.read(b)!=-1;)out.write(b);out.flush(); finally if(out!=null) out.close(); if(in!=null) in.close(); return filePath+fileName;/* * 验证Word文件路径 * param filePath * return */private String validateFilePath(String filePath) if(filePath.lastIndexOf()=-1)&(filePath.lastIndexOf(/)=-1)filePath=filePath+/;return filePath;/* * 生成一个新的文件名(保证文件名不相同) * param fileName * return */private String getRandomFileName(String fileName) fileName= fileName + _+ new SimpleDateFormat(yyyyMMddHHmmssZ).format(new Date()+.doc;return fileName;/* * replaceText * param map */public void replaceAllText(Map map)if(map=null)return;Set keys=map.keySet();Iterator it=keys.iterator();while(it.hasNext()String key=it.next();word.replaceAllText(key, map.get(key);/* * add details * param values */public void insertContextInRow(ListMap values,int tableIndex)if(tableIndex=1)tableIndex=1;if(values=null|values.size()=0)return;int p=null;Map m=values.get(0);Set keys=m.keySet();Iterator it=keys.iterator();while(it.hasNext()String str=it.next();int a=word.getTableCellPostion(str, tableIndex);if(a!=null&a0!=0)p=a;if(p!=null&p0!=0)for(int i=1;ivalues.size();i+) word.addTableRow(tableIndex,p0);/在表格插入行数Iterator it2=keys.iterator();while(it2.hasNext()int row=p0;int col=0;String str=it2.next();inta=word.getTableCellPostion(str, tableIndex);if(a!=null)col=a1;for(Map map:values)word.putTxtToCell(tableIndex, row, col, map.get(str);row+; /* * close document */public void close()word.closeDocument();word.close();/* * 依据Word文件完整路径删除文件 * param path * throws Exception */public void deleteWordFile(String path) throws ExceptionFile f=new File(path);if(!f.exists()throw new Exception(The file is not exists);f.delete();/* * * param args * throws Exception */public static void main(String args) throws ExceptionInputStream in=new FileInputStream(d:aaa.doc);String path=d:qq;String fileName=aaa;WordWriter writer=new WordWriter(in,path,fileName);Map map1=new HashMap();map1.put(p21, 上海商哲);map1.put(p12, 1550);writer.replaceAllText(map1);ListMap values =new ArrayListMap();for(int i=0;i10;i+)Map map=new HashMap();map.put($1, 111111111111);map
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 22文言文二则 伯牙鼓琴教学设计-语文六年级上册统编版
- 2025年气候变化对气候变率的影响
- 2025年气候变化对农业生产模式的调整策略
- 五 富有的是精神说课稿-2023-2024学年中职语文职业模块 服务类语文版
- 医疗文书规范化书写试题及答案
- 2024年中级注册安全工程师煤矿安全考试真题及答案
- 2025公路工程考试试题及答案
- 2025年注册土木工程师考试土木工程施工组织试卷及答案
- 2025年防汛知识培训试题及答案
- (完整版)钢结构期末考试题(含答案)
- 快件处理员(中级)职业技能鉴定考试题库(含答案)
- JT-T-848-2013公路用复合隔离栅立柱
- 早期工业时期英国工艺美术运动设计课件
- 《江苏住宅物业管理服务标准》(DB32T538-2002)
- 装饰装修质量通病防治质量通病防治措施
- 物理课件电源和电流
- 《无人机载荷与行业应用》教学课件合集
- 《西安交通大学》课件
- 搜索引擎营销案例分析
- 华信惠悦GGS全球职等系统
- 肝血管瘤患者的护理查房
评论
0/150
提交评论