Java如何操作Word,Excel,PDF文档?
Java Excel API 文档 http://www.andykhan.com/jexcelapi/ 1、一个jacob操作Word的例子,其他操作excel,pdf的sample里都有 import java.io.File; import com.jacob.com.*; import com.jacob.activeX.*; public class WordTest { public static void main(String[] args) { WordBean word=new WordBean(); word.openWord(true); word.createNewDocument(); word.insertText("Hello word."); } } import com.jacob.activeX.*; import com.jacob.com.*; public class WordBean extends java.awt.Panel { private ActiveXComponent MsWordApp = null; private Dispatch document = null; public WordBean() { super(); } public void openWord(boolean makeVisible) { //Open Word if we've not done it already if (MsWordApp == null) { MsWordApp = new ActiveXComponent("Word.Application"); } //Set the visible property as required. Dispatch.put(MsWordApp, "Visible", new Variant(makeVisible)); } public void createNewDocument() { //Find the Documents collection object maintained by Word Dispatch documents = Dispatch.get(MsWordApp,"Documents").toDispatch(); //Call the Add method of the Documents collection to create //a new document to edit document = Dispatch.call(documents,"Add").toDispatch(); } public void insertText(String textToInsert) { // Get the current selection within Word at the moment. If // a new document has just been created then this will be at // the top of the new doc Dispatch selection = Dispatch.get(MsWordApp,"Selection").toDispatch(); //Put the specified text at the insertion point Dispatch.put(selection,"Text",textToInsert); } public void saveFileAs(String filename) { Dispatch.call(document,"SaveAs",filename); } public void printFile() { //Just print the current document to the default printer Dispatch.call(document,"PrintOut"); } public void closeDocument() { // Close the document without saving changes // 0 = wdDoNotSaveChanges // -1 = wdSaveChanges // -2 = wdPromptToSaveChanges Dispatch.call(document, "Close", new Variant(0)); document = null; } public void closeWord() { Dispatch.call(MsWordApp,"Quit"); MsWordApp = null; document = null; } }
Java程序把Word文档直接换成Html文件方法是什么呢?
acob是Java和Windows下的Com桥,通过它我们可以在Java程序中调用COM组件。
如果你的JDK是1.4,那你需要下载Jacob 1.9的jni库才能正常运行,早期版本在JDK 1.4下有些问题。
以下是引用片段:package com;/*** 〈p〉Title:Word文档转html类〈/p〉* 〈p〉Description: 〈/p〉* 〈p〉Copyright:() 2002〈/p〉* @author 舵手* @version 1.0*/import com.jacob.com.*;import com.jacob.activeX.*;public class WordtoHtml {/***文档转换函数*@param docfile word文档的绝对路径加文件名(包含扩展名)*@param htmlfile 转换后的html文件绝对路径和文件名(不含扩展名)*/public static void change(String docfile, String htmlfile) {ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动wordtry {app.setProperty("Visible", new Variant(false));//设置word不可见Object docs = app.getProperty("Documents").toDispatch();Object doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[]{ docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();// 打开word文件Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile,new Variant (8) }, new int[1]);// 作为html格式保存到临时文件Variant f = new Variant(false);Dispatch.call(doc, "Close", f);} catch (Exception e) {e.printStackTrace();} finally {app.invoke("Quit", new Variant[]{});}}public static void main(String[] strs){WordtoHtml.change("c:\\a\\运输管理调度系统总体方案.doc", "c:\\a\\t");}}
求教用java 生成word!!!
1-apache的POI,此方法对Excel的导出做的很好,目前对Word的导出方面的功能尚未完全。
2-纯JavaScript脚本实现。
主要通过客户端调用本机Office组件来实现。
3-在JSP页面引入头文件实现。
纯JavaScript脚本实现细节方面大体是创建一个word组件ActiveXObject('Word.Application'),用js通过表ID取得表内容然后保存到word,要注意的是js实现有很多不好的地方,例如Internet选项需要把ActiveX空间全部启用,安全级别设置为中。
这样的话岂不是每台机器都要配置一下。
其次每次生成word文档以后弹出对话框(无法保存此文件,因为它已在别处打开(C:\...\STARTUP\Powerword.dot)),出现此问题就需要把C:\Documents and Settings\当前用户名\Application Data\Microsoft\Word\STARTUP下的Powerword.dot文件删除,每次遇到此问题就需要删除文件来解决,十分不方便。
JSP页面引入来实现Word保存就方便多了,但是也有不足的地方,首先如果需要引入如果需要下载的话就引入<%@ page contentType="application/msword; charset=utf-8" %>其实如果大家用框架做就方便多了,比如Struts2。
在Action里直接写如下代码:if(out!=null){String fileName="";fileName+="评价报告.doc";try {HttpServletResponse response = ServletActionContext.getResponse();response.setHeader("Content-disposition","attachment; filename="+new String(fileName.getBytes("GB2312"), "8859_1"));} catch (UnsupportedEncodingException e) {e.printStackTrace();}out是jsp页面表单元素,一个button,用于提交表单到相应Action进行Word下载。
Action设置jsp页面头文件。
这样每次点击button就可以把相应jsp页面的内容保存到Word中并且支持下载,Word中内容并且是可编辑状态。
不足的地方在于由于表内容是动态生成,有的需要先查看在下载Word,就需要另外建立一个新JSP页面进行Word下载,当然首先要在struts.xml里配置好页面转向。
新建立的页面传值同查看页面要保持一样。
java怎么判断word文档版本
请贴出代码,谢谢。
请关闭输入流,释放资源,谢谢。
调用close()方法。
其他貌似没有发现什么问题。
public static String run(String filename){WordExtractor extractor=null;String text=null;try{FileInputStream in = new FileInputStream (filename);extractor = new WordExtractor();text=extractor.extractText(in);}catch(Exception ex){//logreturn null;}return text;}public static void main(String[] args){try{FileOutputStream out=new FileOutputStream("result.txt");out.write(WordProcess.run(args[0]).getBytes());out.flush();out.close();}catch(Exception ex){System.out.println(ex.toString());}}看看这个。
模范这样写,试试看。
这个代码我试过,没问题,如果这样写还是有问题,那就不是代码的问题了。
如何用JAVA打开一个word文件啊?要求有完整程序,跪求啊
首先我用的技术是 poi 这是代码,一个工具类得调用public class WordUtil { /** * 基于模板文件导出 word 文档,此方法主要是用来处理文档中需要替换的文本内容,对图片和表格无效 * * @param templatePath * 模板文件的路径,要求路径中要包含全名,并且模板文件只能是 07 及以上格式,即 docx 的文件 * @param destFilePath * 导出文件的存放路径,包含文件名,例如,E:/test/小区公告.docx * @param data * 用来替换文档中预定义的字符串,要求预定义的字符串与 data 中的 key 值要相同 */ public static void exportWordByTemplate(String templatePath, String destFilePath, Map data) { FileOutputStream out = null; XWPFDocument doc = null; try { doc = new XWPFDocument(POIXMLDocument.openPackage(templatePath)); List listRun; List listParagraphs = doc.getParagraphs(); for (int i = 0; i < listParagraphs.size(); i++) { listRun = listParagraphs.get(i).getRuns(); for (int j = 0; j < listRun.size(); j++) { if (data.get(listRun.get(j).getText(0)) != null) { String val = data.get(listRun.get(j).getText(0)); listRun.get(j).setText(val, 0); } } } File destFile = new File(destFilePath); if (!destFile.getParentFile().exists()) { destFile.getParentFile().mkdirs(); } out = new FileOutputStream(destFilePath); doc.write(out); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 基于模板文件导出 word 文档,该方法支持03格式,但是此方法只能保留文档内容,不能保留文档中的样式和图片,建议将模板使用 07 的格式保存 * * @param templatePath * 模板文件的路径 * @param destFilePath * 导出文件的存放路径,包含文件名,例如,E:/test/小区公告.doc * @param data * 用来替换文档中预定义的字符串,要求预定义的字符串与 data 中的 key 值要相同 */ public static void export03WordByTemplate(String templatePath, String destFilePath, Map data) { try { WordExtractor doc = new WordExtractor(new FileInputStream( templatePath)); String content = doc.getText(); for (String key : data.keySet()) { content = content.replaceAll(key, data.get(key)); } byte b[] = content.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(b); POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry directory = fs.getRoot(); directory.createDocument("WordDocument", bais); FileOutputStream ostream = new FileOutputStream(destFilePath); fs.writeFilesystem(ostream); bais.close(); ostream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { Map maps = new HashMap(); maps.put("appellation", "万达公寓业主:"); maps.put( "main_body", "输出的内容"); maps.put("date", "2013年1月23日"); exportWordByTemplate("E:/sss 2.docx", "E:/test/test.doc", maps);}}"E:/sss 2.docx 模板存放的地址。
E:/test/test.doc 新生成的地址。
用java将word文件上传到服务器,把word里面的内容保存到数据库
使用java中的io进行读取BufferedReader bufferedReader = null;File file = new File("文档地址+文档名.docx");if(!file.exists()){System.out.println("文件不存在");} else {bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "读取的字符格式(UTF-8或GBK)"));String lineText = null;while((lineText = bufferedReader.readLine()) != null){if (linText != null && !lineText.eq("")){System.out.println("一次读取一行,一行内容为:" + lineText);}}}
java poi XWPFTable操作word表格的问题?
1.下载下载3.8beta4版本,请记得一定要下载该版本,其他版本读取word模板并改写内容生成新的文件后,打开新文件时会提示“word无法读取文档,文档可能损坏。
”2.集成到项目这一步很简单,只要把下载后解压得到的poi-3.8-beta4-20110826.jar和poi-scratchpad-3.8-beta4-20110826.jar两个文件复制到java web项目的lib目录下就行了3.制作word模板把需要变动的值全部用代码来代替,例如你需要改变名称的值,则可以在模板中用name来表示。
详细见附件中的doc文件。
4.调用接口方法实现对word的读写操作整个过程就是先读取模板,然后修改内容,再重新生成新的文档保存到本地或者输出文件流提供下载,下面分别是生成新文档和输出文件流两种方式的代码片断,详细的代码请见下列代码中的readwriteWord()两个重载方法。
JAVA中如何把WORD文档直接转换成html?
jacob是java和windows下的com桥,通过它我们可以在java程序中调用COM组件。
如果你的JDK是1.4,那你需要下载jacob1.9的jni库才能正常运行,早期版本在JDK1.4下有些问题。
package com;/** * Title:Word文档转html类 * Description: * Copyright:() 2002 * @author 舵手 * @version 1.0 */import com.jacob.com.*;import com.jacob.activeX.*; public class WordtoHtml { /** *文档转换函数 *@param docfile word文档的绝对路径加文件名(包含扩展名) *@param htmlfile 转换后的html文件绝对路径和文件名(不含扩展名) */ public static void change(String docfile, String htmlfile) { ActiveXComponent app = new ActiveXComponent("Word.Application");// 启动word try { app.setProperty("Visible", new Variant(false)); //设置word不可见 Object docs = app.getProperty("Documents").toDispatch(); Object doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch(); // 打开word文件 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(8) }, new int[1]); // 作为html格式保存到临时文件 Variant f = new Variant(false); Dispatch.call(doc, "Close", f); } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[]{}); } } public static void main(String[] strs){ WordtoHtml.change("c:\\a\\运输管理调度系统总体方案.doc", "c:\\a\\t"); }}
转载请注明出处51数据库 » java wordml