怎么用JAVA操作WORD文档 我现在用的是JACOB,不知道怎么读取WORD文档的一段内容。找不到帮助文档,请高手点拨 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个网上有不少,基本上就是先selection,把光标定位倒你要替换的地方,然后就是replace就可以了 有个台湾人写的《Java深度历险》里面讲到过这个 youngyj1982(youngyj1982) : 我是要读取那段的内容,怎么会用到replace呢? 能交流交流吗?我的QQ 41025576 一个例子,你看看package changeWordType;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.Variant;public class Test { /** * * @param path * @param paths * @param savepaths */ public static void change(String path, String paths, String savepaths) { File d = new File(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 = new ActiveXComponent( "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( docs, "Open", Dispatch.Method, new Object[]{inFile, new Variant(false), new Variant(true)}, new int[1]) .toDispatch(); // 作为txt格式保存到临时文件 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{tpFile, new Variant(10)}, new int[1]); Variant f = new Variant(false); Dispatch.call(doc, "Close", f); flag = true; } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[]{}); } System.out.println("转化完毕!\n--------"); } } else { pathss = paths; // 进入下一级目录 pathss = pathss + lists[i].getName() + "\\"; // 递归遍历所有目录 change(path, pathss, savepaths); } } } public static String formatname(String name) { name = name.replace('、', '_'); name = name.replace(')', ')'); name = name.replace('(', '('); return name; } public static String pinyin(String path, String hanzi) { File f = new File(path); String pinyin = new String(""); pinyin = hanzi; try { FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(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); } return pinyin; } public static void main(String[] args) { String paths = new String("e:\\Work\\"); String savepaths = new String("e:\\Work\\result\\"); String path = new String("e:\\work\\pinyin.txt"); change(path, paths, savepaths); }} java操作word详解,并且有开发包下载 http://www.chinajavalab.com/cgi-bin/topic.cgi?forum=1&topic=46 GZIP压缩所有子目录以及子目录底下的文件问题 如何遍历MAP类型的里的内容? 大哥帮忙呀!救人一命胜造七级浮屠! 金山笔试题目 兄弟们,进来看一下这个程序啊,大家一起学习啊!! 请用java写二叉树算法,实现添加数据形成二叉树功能,并以先序的方式打印出来 为什么我用file对象删除不了文件? 哪里可以下载到jbuilder8的升级包? 求救高手:一个困扰我很久的问题!!!急! sql语句的连接问题? 使用JMF框架建立视频会议时产生的问题!!!很急,有精通的高手帮忙帮忙 如何用Java做报表?
我是要读取那段的内容,怎么会用到replace呢? 能交流交流吗?我的QQ 41025576
package changeWordType;import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;public class Test { /**
*
* @param path
* @param paths
* @param savepaths
*/
public static void change(String path, String paths, String savepaths) {
File d = new File(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 = new ActiveXComponent(
"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(
docs,
"Open",
Dispatch.Method,
new Object[]{inFile, new Variant(false),
new Variant(true)}, new int[1])
.toDispatch();
// 作为txt格式保存到临时文件
Dispatch.invoke(doc, "SaveAs", Dispatch.Method,
new Object[]{tpFile, new Variant(10)},
new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[]{});
}
System.out.println("转化完毕!\n--------");
}
} else {
pathss = paths;
// 进入下一级目录
pathss = pathss + lists[i].getName() + "\\";
// 递归遍历所有目录
change(path, pathss, savepaths);
}
} } public static String formatname(String name) {
name = name.replace('、', '_');
name = name.replace(')', ')');
name = name.replace('(', '(');
return name;
} public static String pinyin(String path, String hanzi) {
File f = new File(path);
String pinyin = new String("");
pinyin = hanzi;
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(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);
}
return pinyin;
} public static void main(String[] args) { String paths = new String("e:\\Work\\");
String savepaths = new String("e:\\Work\\result\\");
String path = new String("e:\\work\\pinyin.txt"); change(path, paths, savepaths); }}