java 取商_Java读取word文档,转换为网页

public classTest3 {/***

*@parampath

*@parampaths

*@paramsavepaths*/

public static voidchange(String path, String paths, String savepaths) {

File d= newFile(paths);//取得当前文件夹下所有文件和目录的列表

File lists[] =d.listFiles();

String pathss= new String("");//对当前目录下面所有文件进行检索

for (int i = 0; i < lists.length; i++) {if(lists[i].isFile()) {//得到doc文件名

String filename =lists[i].getName();//定义最简文件名

String realfilename = new String(filename.substring(0,

(filename.length()- 4)));//定义文件全名为路径加文件名

String fullsavefilename = paths +realfilename;

fullsavefilename= fullsavefilename.replace('/', '_');// /

fullsavefilename = fullsavefilename.replace(':', '@');//fullsavefilename = fullsavefilename.substring(47,//fullsavefilename.length());//定义文件类型

String filetype = new String("");

filetype= filename.substring((filename.length() - 3),

filename.length());if (filetype.equals("doc")) {

System.out.println("--------/n当前正在转换......");//打印当前目录路径

System.out.println(paths);//打印doc文件名

System.out.println(realfilename);//启动word

ActiveXComponent app = newActiveXComponent("Word.Application");

String docpath= paths +filename;

String txtpath= savepaths + fullsavefilename + ".htm";//格式化文件名

txtpath =formatname(txtpath);//将中文文件名改为拼音

txtpath =pinyin(path, txtpath);//要转换的word文件

String inFile =docpath;//txt文件

String tpFile =txtpath;boolean flag = false;try{//设置word不可见

app.setProperty("Visible", new Variant(false));

Object docs= app.getProperty("Documents").toDispatch();//打开word文件

Object doc =Dispatch.invoke(

(Dispatch) docs,"Open",

Dispatch.Method,new Object[] { inFile, new Variant(false),new Variant(true) }, new int[1])

.toDispatch();//作为txt格式保存到临时文件

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

Dispatch.Method,newObject[] { tpFile,new Variant(10) }, new int[1]);

Variant f= new Variant(false);

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

flag= true;

}catch(Exception e) {

e.printStackTrace();

}finally{

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

}

System.out.println("转化完毕!/n--------");

}

}else{

pathss=paths;//进入下一级目录

pathss = pathss + lists[i].getName() + "//";//递归遍历所有目录

change(path, pathss, savepaths);

}

}

}public staticString formatname(String name) {

name= name.replace('、', '_');

name= name.replace(')', ')');

name= name.replace('(', '(');returnname;

}public staticString pinyin(String path, String hanzi) {

File f= newFile(path);

String pinyin= new String("");

pinyin=hanzi;try{

FileReader fr= newFileReader(f);

BufferedReader br= newBufferedReader(fr);

String line= new String("");

line=br.readLine();int count = 0;int location = 0;while (line != null) {while (pinyin.indexOf(line.charAt(0)) != -1) {

location= pinyin.indexOf(line.charAt(0));

pinyin= pinyin.substring(0, location)+ line.substring(1, 2)+ pinyin.substring(location + 1, pinyin.length());

}

line=br.readLine();

System.out.print("/b/b/b/b/b/b" + count++);

}

fr.close();

br.close();

}catch(Exception e) {//System.err.println(e);

}returnpinyin;

}public static voidmain(String[] args) {

String paths= new String("e://360Downloads//");

String savepaths= new String("e://360Downloads//Video//");

String path= new String("e://360Downloads//pinyin.doc");

change(path, paths, savepaths);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值