admin 管理员组

文章数量: 1184232


2024年1月10日发(作者:cms系统的基本功能是什么)

项目开发过程中,需求涉及到了各种文档转换为HTML或者网页易显示格式,现在将实现方式整理如下:

一、了解Jacob

先了解一下概念,JACOB 就是 JAVA-COM Bridge的缩写,提供自动化的访问com的功能,也是通过JNI功能访问windows平台下的com组件或者win32系统库的。这是一个开始于1999年的开源项目的成果,有很多使用者对该项目进行了修改,做出了自己的贡献。

下载地址:/project/?group_id=109543&package_id=118368

二、Jacob安装

1、我们解开下载的jacob_,在文件夹中找到和两个文件

2、将压缩包解压后,添加到Libraries中;

3、将放至“WINDOWSSYSTEM32”下面。

需要注意的是:

【使用IDE启动Web服务器时,系统读取不到,例如用MyEclipse启动Tomcat,就需要将dll文件copy到(经本人实践,发现此处应为JDK,不然会有问题哦)安装目录的“jrebin”下面。最后需要关闭MyEclipse,再重启。

一般系统没有加载到文件时,报错信息为:“sfiedLinkError: no

jacob in ”】

三、使用Jacob转换Word,Excel为HTML

JAVA代码:

import edReader;

import edWriter;

import ;

import putStream;

import tFoundException;

import iter;

import ption;

import treamReader;

import XComponent;

import ch;

import t;

public class TransformFiletoHtml

{

int 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("ation"); // 启动word

try

{

perty("Visible", new Variant(false));

Dispatch docs = perty("Documents").toDispatch();

Dispatch doc = (docs,"Open",,new Object[]

{ docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();

(doc, "SaveAs", , new Object[] {htmlfile, new

Variant(WORD_HTML) }, new int[1]);

Variant f = new Variant(false);

(doc, "Close", f);

}

catch (Exception e)

{

tackTrace();

}

finally

{

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

}

}

/**

* EXCEL转HTML

* @param xlsfile EXCEL文件全路径

* @param htmlfile 转换后HTML存放路径

*/

public void excelToHtml(String xlsfile, String htmlfile)

{

ActiveXComponent app = new ActiveXComponent("ation"); // 启动excel

try

{

perty("Visible", new Variant(false));

Dispatch excels = perty("Workbooks").toDispatch();

Dispatch excel = (excels,"Open",,new Object[]

{ xlsfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();

(excel, "SaveAs", , new Object[] {htmlfile, new

Variant(EXCEL_HTML) }, new int[1]);

Variant f = new Variant(false);

(excel, "Close", f);

}

catch (Exception e)

{

tackTrace();

}

finally

{

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

}

}

/**

* /删除指定文件夹

* @param folderPath 文件夹全路径

* @param htmlfile 转换后HTML存放路径

*/

public void delFolder(String folderPath)

{

try

{

delAllFile(folderPath); //删除完里面所有内容

String filePath = folderPath;

filePath = ng();

myFilePath = new (filePath);

(); //删除空文件夹

} catch (Exception e) {tackTrace();}

}

/**

* /删除指定文件夹下所有文件

* @param path 文件全路径

*/

public boolean delAllFile(String path)

{

boolean flag = false;

File file = new File(path);

if (!())

{

return flag;

}

if (!ctory())

{

return flag;

}

String[] tempList = ();

File temp = null;

for (int i = 0; i < ; i++)

{

if (th(tor))

{

temp = new File(path + tempList[i]);

}

else

{

temp = new File(path + tor + tempList[i]);

}

if (())

{

();

}

if (ctory())

{

delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件

delFolder(path + "/" + tempList[i]);//再删除空文件夹

flag = true;

}

}

return flag;

}

}

调用JAVA代码:

public class Test1 {

public static void main(String[] args) {

// TODO Auto-generated method stub

TransformFiletoHtml trans = new TransformFiletoHtml();

Html("D:", "D:");

}

}

package ;

import edInputStream;

import edOutputStream;

import ;

import putStream;

import tputStream;

import tream;

import Stream;

import DateFormat;

import ist;

import ;

import p;

import or;

import ;

import ;

import ;

public class WordWriter {

private WordOperator word;

public WordWriter(String filePath){

word=new WordOperator();

cument(filePath);

}

public WordWriter(InputStream input,String filePath,String fileName) throws Exception{

String path=saveAsDocFile(input,filePath,fileName);

word=new WordOperator();

cument(path);

}

/**

* 将word文档输入流保存为本地得到word文件

* @param input

* @param filePath

* @param fileName

* @throws Exception

*/

@SuppressWarnings("unused")

private String saveAsDocFile(InputStream input,String filePath,String fileName)throws

Exception{

if(!dateString(filePath)||!dateString(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(!()){

throw new Exception(" The FilePath is null");

}

filePath = validateFilePath(filePath);

fileName = getRandomFileName(fileName);

InputStream in=null;

OutputStream out=null;

try{

in=new BufferedInputStream(input);

out=new BufferedOutputStream(new FileOutputStream(filePath+fileName));

byte[] b=new byte[1024];

for(int p=0; (p=(b))!=-1;){

(b);

();

}

}finally{

if(out!=null){

();

}

if(in!=null){

();

}

}

return filePath+fileName;

}

/**

* 验证Word文件路径

* @param filePath

* @return

*/

private String validateFilePath(String filePath) {

if((dexOf("")==-1)&&(dexOf("/")==-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=();

Iterator it=or();

while(t()){

String key=();

eAllText(key, (key));

}

}

/**

* add details

* @param values

*/

public void insertContextInRow(List> values,int tableIndex){

if(tableIndex<=1){

tableIndex=1;

}

if(values==null||()<=0){

return;

}

int[] p=null;

Map m=(0);

Set keys=();

Iterator it=or();

while(t()){

String str=();

int[] a=leCellPostion(str, tableIndex);

if(a!=null&&a[0]!=0){

p=a;

}

}

if(p!=null&&p[0]!=0){

for(int i=1;i<();i++){

leRow(tableIndex,p[0]);//在表格插入行数

}

}

Iterator it2=or();

while(t()){

int row=p[0];

int col=0;

String str=();

int[]a=leCellPostion(str, tableIndex);

if(a!=null){

col=a[1];

}

for(Map map:values){

ToCell(tableIndex, row, col, (str));

row++;

}

}

}

/**

* close document

*/

public void close(){

ocument();

();

}

/**

* 依据Word文件完整路径删除文件

* @param path

* @throws Exception

*/

public void deleteWordFile(String path) throws Exception{

File f=new File(path);

if(!()){

throw new Exception("The file is not exists");

}

();

}

/**

*

}

* @param args

* @throws Exception

*/

public static void main(String args[]) throws Exception{

InputStream in=new FileInputStream("d:");

String path="d:qq";

String fileName="aaa";

WordWriter writer=new WordWriter(in,path,fileName);

Map map1=new HashMap();

}

("p21", "上海商哲");

("p12", "1550");

eAllText(map1);

List> values =new ArrayList>();

for(int i=0;i<10;i++){

Map map=new HashMap();

("$1", "1");

("$2", "222222222222");

("$3", "333333333333");

("$4", "444444444444");

("$5", "555555555555");

("$6", "666666666666");

(map);

}

ContextInRow(values, 1);

();

只写了一个测试word转html的,excel转html的同理,在TransformFiletoHtml类中,写了两个方法,一个是删除文件夹的方法(delFolder()),一个是删除文件夹下所有文件的方法(delAllFile())。写这个的目的是出于:在word或者excel转html的过程中,除了生成制定的html页面外,jacob组件会生成一些转换html页面时相关的其它页面,但是这些相关的其它页面不是我们所需要的,因此想把它删除,其实不删除也可以,只是看着不爽,在测试中,发现,word有时不会生成这样的文件,而且即使生成了,也能删除掉,但是excel生成的文件却不能删除,还望高手们给予解答。

另外,在你将excel转换html时,如果你的代码没问题,转换时,老提示什么存在用户区域的安全设置这什么的,会让你选择继续转换,还是取消。这是因为你转换的那个excel中写了保护,解决办法是在excel的工具->保护->允许用户编辑区域,删除里面的所有保护。

1) 把在 C:Program FilesJavajdk1.5.0_08bin、C:Program

FilesJavajdk1.5.0_08jrebin、C:WINDOWSsystem32 目录下各.放一份

2) 把放入项目的lib包下,并且在“java构建路径”中也要加载此jar包。.

3) 运行项目即可编译通过.


本文标签: 文件 删除 转换 项目