文件管理 · 2022年7月25日

java2word下载|java解析word文档有哪些方法

1. java操作word 的有哪几种方式

java读取word文档时,虽然网上介绍了很多插件poi、java2Word、jacob、itext等等,poi无法读取格式(新的API估计行好像还在处于研发阶段,不太稳定,做项目不太敢用);java2Word、jacob容易报错找不到注册,比较诡异,我曾经在不同的机器上试过,操作方法完全一致,有的机器不报错,有的报错,去他们论坛找高人解决也说不出原因,项目部署用它有点玄;itxt好像写很方便但是我查了好久资料没有见到过关于读的好办法。经过一番选择还是折中点采用rtf最好,毕竟rtf是开源格式,不需要借助任何插件,只需基本IO操作外加编码转换即可。rtf格式文件表面看来和doc没啥区别,都可以用word打开,各种格式都可以设定。—– 实现的功能:读取rtf模板内容(格式和文本内容),替换变化部分,形成新的rtf文档。—– 实现思路:模板中固定部分手动输入,变化的部分用$info$表示,只需替换$info$即可。1、采用字节的形式读取rtf模板内容2、将可变的内容字符串转为rtf编码3、替换原文中的可变部分,形成新的rtf文档主要程序如下:public String bin2hex(String bin) {char[] digital = "0123456789ABCDEF".toCharArray();StringBuffer sb = new StringBuffer("");byte[] bs = bin.getBytes();int bit;for (int i = 0; i < bs.length;i++) {bit = (bs[i] & 0x0f0)>> 4;sb.append("\\'");sb.append(digital[bit]);bit = bs[i] & 0x0f;sb.append(digital[bit]);}return sb.toString(); }public String readByteRtf(InputStream ins, String path){ String sourcecontent =""; try{ ins = newFileInputStream(path); byte[] b= new byte[1024];if (ins == null) {System.out.println("源模板文件不存在");}int bytesRead = 0;while (true) {bytesRead = ins.read(b, 0, 1024); // return final read bytescountsif(bytesRead == -1) {// end of InputStreamSystem.out.println("读取模板文件结束");break;}sourcecontent += new String(b, 0, bytesRead); // convert to stringusing bytes} }catch(Exception e){ e.printStackTrace(); }

2. java解析word文档有哪些方法

java读取word文档时,虽然网上介绍了很多插件poi、java2Word、jacob、itext等等,poi无法读取格式(新的估计行好像还在处于研发阶段,不太稳定,做项目不太敢用);java2Word、jacob容易报错找不到注册,比较诡异,我曾经在不同的机器上试过,操作方法完全一致,有的机器不报错,有的报错,去他们论坛找高人解决也说不出原因,项目部署用它有点玄;itxt好像写很方便但是我查了好久资料没有见到过关于读的好办法。经过一番选择还是折中点采用rtf最好,毕竟rtf是开源格式,不需要借助任何插件,只需基本IO操作外加编码转换即可。rtf格式文件表面看来和doc没啥区别,都可以用word打开,各种格式都可以设定。—– 实现的功能:读取rtf模板内容(格式和文本内容),替换变化部分,形成新的rtf文档。—– 实现思路:模板中固定部分手动输入,变化的部分用$info$表示,只需替换$info$即可。1、采用字节的形式读取rtf模板内容2、将可变的内容字符串转为rtf编码3、替换原文中的可变部分,形成新的rtf文档主要程序如下:public String bin2hex(String bin) {char[] digital = "0123456789ABCDEF".toCharArray();StringBuffer sb = new StringBuffer("");byte[] bs = bin.getBytes();int bit;for (int i = 0; i < bs.length;i++) {bit = (bs[i] & 0x0f0)>> 4;sb.append("\\'");sb.append(digital[bit]);bit = bs[i] & 0x0f;sb.append(digital[bit]);}return sb.toString(); }public String readByteRtf(InputStream ins, String path){ String sourcecontent =""; try{ ins = newFileInputStream(path); byte[] b= new byte[1024];if (ins == null) {System.out.println("源模板文件不存在");}int bytesRead = 0;while (true) {bytesRead = ins.read(b, 0, 1024); // return final read bytescountsif(bytesRead == -1) {// end of InputStreamSystem.out.println("读取模板文件结束");break;}sourcecontent += new String(b, 0, bytesRead); // convert to stringusing bytes} }catch(Exception e){ e.printStackTrace(); } return sourcecontent ;}以上为核心代码,剩余部分就是替换,从新组装java中的String.replace(oldstr,newstr);方法可以实现,在这就不贴了。源代码部分详见附件。运行源代码前提:c盘创建YQ目录,将附件中"模板.rtf"复制到YQ目录之下,运行OpreatorRTF.java文件即可,就会在YQ目录下生成文件名如:21时15分19秒_cheney_记录.rtf的文件。 package com;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.text.SimpleDateFormat;import java.util.Date;public class OperatorRTF {public String strToRtf(String content){char[] digital = "0123456789ABCDEF".toCharArray();StringBuffer sb = new StringBuffer("");byte[] bs = content.getBytes();int bit;for (int i = 0; i < bs.length; i++) {bit = (bs[i] & 0x0f0)>> 4;sb.append("\\'");sb.append(digital[bit]);bit = bs[i] & 0x0f;sb.append(digital[bit]);}return sb.toString();}public String replaceRTF(String content,String replacecontent,intflag){String rc = strToRtf(replacecontent);String target = "";if(flag==0){target = content.replace("$timetop$",rc);}if(flag==1){target = content.replace("$info$",rc);}if(flag==2){target = content.replace("$idea$",rc);}if(flag==3){target = content.replace("$advice$",rc);}if(flag==4){target = content.replace("$infosend$",rc);}return target;}public String getSavePath() {String path = "C:\\YQ";File fDirecotry = new File(path);if (!fDirecotry.exists()) {fDirecotry.mkdirs();}return path;}public String ToSBC(String input){char[] c =input.toCharArray();for (int i =0; i < c.length; i++){if (c[i] == 32){c[i] = (char) 12288;continue;}if (c[i] < 127){c[i] = (char) (c[i] + 65248);}}return newString(c);}public void rgModel(String username, String content) {// TODO Auto-generated method stubDate current=new Date();SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-ddHH:mm:ss");String targetname = sdf.format(current).substring(11,13) + "时";targetname += sdf.format(current).substring(14,16) + "分";targetname += sdf.format(current).substring(17,19) + "秒";targetname += "_" + username +"_记录.rtf";String strpath = getSavePath();String sourname = strpath+"\\"+"模板.rtf";String sourcecontent = "";InputStream ins = null;try{ins = new FileInputStream(sourname);byte[] b = new byte[1024];if (ins == null) {System.out.println("源模板文件不存在");}int bytesRead = 0;while (true) {bytesRead = ins.read(b, 0, 1024); // return final read bytescountsif(bytesRead == -1) {// end of InputStreamSystem.out.println("读取模板文件结束");break;}sourcecontent += new String(b, 0, bytesRead); // convert to stringusing bytes}}catch(Exception e){e.printStackTrace();}String targetcontent = "";String array[] = content.split("~");for(int i=0;i<array.length;i++){if(i==0){targetcontent = replaceRTF(sourcecontent, array[i], i);}else{targetcontent = replaceRTF(targetcontent, array[i], i);}} try {FileWriter fw = new FileWriter(getSavePath()+"\\" +targetname,true);PrintWriter out = new PrintWriter(fw);if(targetcontent.equals("")||targetcontent==""){out.println(sourcecontent);}else{out.println(targetcontent);}out.close();fw.close();System.out.println(getSavePath()+" 该目录下生成文件" +targetname + " 成功");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args) {// TODO Auto-generated method stubOperatorRTF oRTF = new OperatorRTF();String content ="2008年10月12日9时-2008年10月12日6时~我们参照检验药品的方法~我们参照检验药品的方法~我们参照检验药品的方法~我们参照检验药品的方法";oRTF.rgModel("cheney",content);}}

3. java导出word文档后,怎么在客户端提供下载。。。

做成一套吧。1、上传功能。将上传的文件保存到服务器上,同时将服务器上文件的路径,文件名等等数据存到数据库中。2、显示数据库中保存的文件列表。3、选择对应的文件点击下载,后台到数据库中读取路径,然后处理请求。

4. java怎么判断word文档版本

java读取word文档时,虽然网上介绍了很多插件poi、java2Word、jacob、itext等等,poi无法读取格式(新的API估 计行好像还在处于研发阶段,不太稳定,做项目不太敢用);java2Word、jacob容易报错找不到注册,比较诡异,我曾经在不同的机器上试过

5. java 从数据库读取2进制文件(word。excel)等 打成压缩包输出代码思路

JDBC 读 blob,得到InputStream、再使用JAVA的zip的流,输出

6. 手机JAVA2.0能看TXT或者WORD的文件吗

txt文件任意一个阅读软件都可以看,比如GGbook,QQ阅读,百阅……我记得百阅可以直接看word

7. java如何根据word模板生成word文档

先下载jacob_1.10.1.zip。解压后将jacob.dll放到windows/system32下面或\j2sdk\bin下面。将jacob.jar加入项目。/** Java2word.java** Created on 2007年8月13日, 上午10:32** To change this template, choose Tools | Template Manager* and open the template in the editor.*//** 传入数据为HashMap对象,对象中的Key代表word模板中要替换的字段,Value代表用来替换的值。* word模板中所有要替换的字段(即HashMap中的Key)以特殊字符开头和结尾,如:$code$、$date$……,以免执行错误的替换。* 所有要替换为图片的字段,Key中需包含image或者Value为图片的全路径(目前只判断文件后缀名为:.bmp、.jpg、.gif)。* 要替换表格中的数据时,HashMap中的Key格式为“[email protected]”,其中:R代表从表格的第R行开始替换,N代表word模板中的第N张表格;Value为ArrayList对象,ArrayList中包含的对象统一为String[],一条String[]代表一行数据,ArrayList中第一条记录为特殊记录,记录的是表格中要替换的列号,如:要替换第一列、第三列、第五列的数据,则第一条记录为String[3] {“1”,”3”,”5”}。*/package com.word.util;/**** @author kdl*/import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.Variant;public class Java2word { private boolean saveOnExit; /** * word文档 */ Dispatch doc = null;/** * word运行程序对象s */ private ActiveXComponent word; /** * 所有word文档 */ private Dispatch documents;/** * 构造函数 */ public Java2word() { if(word==null){ word = new ActiveXComponent("Word.Application"); word.setProperty("Visible",new Variant(false)); } if(documents==null) documents = word.getProperty("Documents").toDispatch(); saveOnExit = false; } /** * 设置参数:退出时是否保存 * @param saveOnExit boolean true-退出时保存文件,false-退出时不保存文件 */ public void setSaveOnExit(boolean saveOnExit) { this.saveOnExit = saveOnExit; } /** * 得到参数:退出时是否保存 * @return boolean true-退出时保存文件,false-退出时不保存文件 */ public boolean getSaveOnExit() { return saveOnExit; }/** * 打开文件 * @param inputDoc String 要打开的文件,全路径 * @return Dispatch 打开的文件 */ public Dispatch open(String inputDoc) { return Dispatch.call(documents,"Open",inputDoc).toDispatch(); //return Dispatch.invoke(documents,"Open",Dispatch.Method,new Object[]{inputDoc},new int[1]).toDispatch(); } /** * 选定内容 * @return Dispatch 选定的范围或插入点 */ public Dispatch select() { return word.getProperty("Selection").toDispatch(); } /** * 把选定内容或插入点向上移动 * @param selection Dispatch 要移动的内容 * @param count int 移动的距离 */ public void moveUp(Dispatch selection,int count) { for(int i = 0;i < count;i ++) Dispatch.call(selection,"MoveUp"); } /** * 把选定内容或插入点向下移动 * @param selection Dispatch 要移动的内容 * @param count int 移动的距离 */ public void moveDown(Dispatch selection,int count) { for(int i = 0;i < count;i ++) Dispatch.call(selection,"MoveDown"); } /** * 把选定内容或插入点向左移动 * @param selection Dispatch 要移动的内容 * @param count int 移动的距离 */ public void moveLeft(Dispatch selection,int count) { for(int i = 0;i < count;i ++) { Dispatch.call(selection,"MoveLeft"); } } /** * 把选定内容或插入点向右移动 * @param selection Dispatch 要移动的内容 * @param count int 移动的距离 */ public void moveRight(Dispatch selection,int count) { for(int i = 0;i < count;i ++) Dispatch.call(selection,"MoveRight"); } /** * 把插入点移动到文件首位置 * @param selection Dispatch 插入点 */ public void moveStart(Dispatch selection) { Dispatch.call(selection,"HomeKey",new Variant(6)); }/** * 从选定内容或插入点开始查找文本 * @param selection Dispatch 选定内容 * @param toFindText String 要查找的文本 * @return boolean true-查找到并选中该文本,false-未查找到文本 */ public boolean find(Dispatch selection,String toFindText) { //从selection所在位置开始查询 Dispatch find = word.call(selection,"Find").toDispatch(); //设置要查找的内容 Dispatch.put(find,"Text",toFindText); //向前查找 Dispatch.put(find,"Forward","True"); //设置格式 Dispatch.put(find,"Format","True"); //大小写匹配 Dispatch.put(find,"MatchCase","True"); //全字匹配 Dispatch.put(find,"MatchWholeWord","True"); //查找并选中 return Dispatch.call(find,"Execute").getBoolean(); } /** * 把选定内容替换为设定文本 * @param selection Dispatch 选定内容 * @param newText String 替换为文本 */ public void replace(Dispatch selection,String newText) { //设置替换文本 Dispatch.put(selection,"Text",newText); } /** * 全局替换 * @param selection Dispatch 选定内容或起始插入点 * @param oldText String 要替换的文本 * @param newText String 替换为文本 */ public void replaceAll(Dispatch selection,String oldText,Object replaceObj) { //移动到文件开头 moveStart(selection); if(oldText.startsWith("table") || replaceObj instanceof ArrayList) replaceTable(selection,oldText,(ArrayList) replaceObj); else { String newText = (String) replaceObj; if(newText==null) newText=""; if(oldText.indexOf("image") != -1&!newText.trim().equals("") || newText.lastIndexOf(".bmp") != -1 || newText.lastIndexOf(".jpg") != -1 || newText.lastIndexOf(".gif") != -1){ while(find(selection,oldText)) { replaceImage(selection,newText); Dispatch.call(selection,"MoveRight"); } }else{ while(find(selection,oldText)) { replace(selection,newText); Dispatch.call(selection,"MoveRight"); } } } } /** * 替换图片 * @param selection Dispatch 图片的插入点 * @param imagePath String 图片文件(全路径) */ public void replaceImage(Dispatch selection,String imagePath) { Dispatch.call(Dispatch.get(selection,"InLineShapes").toDispatch(),"AddPicture",imagePath); } /** * 替换表格 * @param selection Dispatch 插入点 * @param tableName String 表格名称,形如[email protected]、[email protected][email protected],R代表从表格中的第N行开始填充,N代表word文件中的第N张表 * @param fields HashMap 表格中要替换的字段与数据的对应表 */ public void replaceTable(Dispatch selection,String tableName,ArrayList dataList) { if(dataList.size() <= 1) { System.out.println("Empty table!"); return; } //要填充的列 String[] cols = (String[]) dataList.get(0); //表格序号 String tbIndex = tableName.substring(tableName.lastIndexOf("@") + 1); //从第几行开始填充 int fromRow = Integer.parseInt(tableName.substring(tableName.lastIndexOf("$") + 1,tableName.lastIndexOf("@"))); //所有表格 Dispatch tables = Dispatch.get(doc,"Tables").toDispatch(); //要填充的表格 Dispatch table = Dispatch.call(tables,"Item",new Variant(tbIndex)).toDispatch(); //表格的所有行 Dispatch rows = Dispatch.get(table,"Rows").toDispatch(); //填充表格 for(int i = 1;i < dataList.size();i ++) { //某一行数据 String[] datas = (String[]) dataList.get(i);//在表格中添加一行 if(Dispatch.get(rows,"Count").getInt() < fromRow + i – 1) Dispatch.call(rows,"Add"); //填充该行的相关列 for(int j = 0;j < datas.length;j ++) { //得到单元格 Dispatch cell = Dispatch.call(table,"Cell",Integer.toString(fromRow + i – 1),cols[j]).toDispatch(); //选中单元格 Dispatch.call(cell,"Select"); //设置格式 Dispatch font = Dispatch.get(selection,"Font").toDispatch(); Dispatch.put(font,"Bold","0"); Dispatch.put(font,"Italic","0"); //输入数据 Dispatch.put(selection,"Text",datas[j]); } } } /** * 保存文件 * @param outputPath String 输出文件(包含路径) */ public void save(String outputPath) { Dispatch.call(Dispatch.call(word,"WordBasic").getDispatch(),"FileSaveAs",outputPath); } /** * 关闭文件 * @param document Dispatch 要关闭的文件 */ public void close(Dispatch doc) { Dispatch.call(doc,"Close",new Variant(saveOnExit)); word.invoke("Quit",new Variant[]{}); word = null; } /** * 根据模板、数据生成word文件 * @param inputPath String 模板文件(包含路径) * @param outPath String 输出文件(包含路径) * @param data HashMap 数据包(包含要填充的字段、对应的数据) */ public void toWord(String inputPath,String outPath,HashMap data) { String oldText; Object newValue; try { if(doc==null) doc = open(inputPath); Dispatch selection = select(); Iterator keys = data.keySet().iterator(); while(keys.hasNext()) { oldText = (String) keys.next(); newValue = data.get(oldText);replaceAll(selection,oldText,newValue); } save(outPath); } catch(Exception e) { System.out.println("操作word文件失败!"); e.printStackTrace(); } finally { if(doc != null) close(doc); } }

8. Java2word可以在linux上用吗

可以不用IDE,只要配置好java环境变量,就跟windows 下一样用文本文件方式编写,然后javac,java去编译和执行,不过这样开发效率很低;再说,idea/Eclipse也有linux版本吖,为啥不用?