怎用用java导入、导入word形式的考试题目
/** * 创建word文档 步骤: 1,建立文档 2,创建一个书写器 3,打开文档 4,向文档中写入数据 5,关闭文档 */ public class WordDemo { public WordDemo() { } COS_MANIFEST_DTLS /** * @param args */ public static void main(String[] args) { // 创建word文档,并设置纸张的大小 Document document = new Document(PageSize.A4); try { RtfWriter2.getInstance(document, new FileOutputStream("E:/word.doc")); document.open(); //设置合同头 Paragraph ph = new Paragraph(); Font f = new Font(); Paragraph p = new Paragraph("出口合同", new Font(Font.NORMAL, 18, Font.BOLDITALIC, new Color(0, 0, 0)) ); p.setAlignment(1); document.add(p); ph.setFont(f); // 设置中文字体 // BaseFont bfFont = // BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); // Font chinaFont = new Font(); /* * 创建有三列的表格 */ Table table = new Table(4); document.add(new Paragraph("生成表格")); table.setBorderWidth(1); table.setBorderColor(Color.BLACK); table.setPadding(0); table.setSpacing(0); /* * 添加表头的元素 */ Cell cell = new Cell("表头");//单元格 cell.setHeader(true); cell.setColspan(3);//设置表格为三列 cell.setRowspan(3);//设置表格为三行 table.addCell(cell); table.endHeaders();// 表头结束 // 表格的主体 cell = new Cell("Example cell 2"); cell.setRowspan(2);//当前单元格占两行,纵向跨度 table.addCell(cell); table.addCell("1,1"); table.addCell("1,2"); table.addCell("1,3"); table.addCell("1,4"); table.addCell("1,5"); table.addCell(new Paragraph("用java生成的表格1")); table.addCell(new Paragraph("用java生成的表格2")); table.addCell(new Paragraph("用java生成的表格3")); table.addCell(new Paragraph("用java生成的表格4")); document.add(new Paragraph("用java生成word文件")); document.add(table); document.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }。
紧急求助如何用java实现word文档的导入,请大家踊跃发言,谢谢大家
环境支持 1.1 添加poi支持:包下载地址http://www.apache.org/dyn/closer.cgi/poi/release/ 1.2 POI对Excel文件的读取操作比较方便,POI还提供对Word的DOC格式文件的读取。
但在它的发行版本中没有发布对Word支持的模块,需要另外下载一个POI的扩展的Jar包。下载地址为http://www.ibiblio.org/maven2/org/textmining/tm-extractors/0.4/ 下载extractors-0.4_zip这个文件package com.ray.poi.util;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.textmining.text.extraction.WordExtractor;/** * 读写doc * @author wangzonghao * */public class POIWordUtil { /** * 读入doc * @param doc * @return * @throws Exception */ public static String readDoc(String doc) throws Exception { // 创建输入流读取DOC文件 FileInputStream in = new FileInputStream(new File(doc)); WordExtractor extractor = null; String text = null; // 创建WordExtractor extractor = new WordExtractor(); // 对DOC文件进行提取 text = extractor.extractText(in); return text; } /** * 写出doc * @param path * @param content * @return */ public static boolean writeDoc(String path, String content) { boolean w = false; try { // byte b[] = content.getBytes("ISO-8859-1"); byte b[] = content.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(b); POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry directory = fs.getRoot(); DocumentEntry de = directory.createDocument("WordDocument", bais); FileOutputStream ostream = new FileOutputStream(path); fs.writeFilesystem(ostream); bais.close(); ostream.close(); } catch (IOException e) { e.printStackTrace(); } return w; } }测试package com.ray.poi.util;import junit.framework.TestCase;public class POIUtilTest extends TestCase { public void testReadDoc() { try{ String text = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc"); System.out.println(text); }catch(Exception e){ e.printStackTrace(); } } public void testWriteDoc() { String wr; try { wr = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc"); boolean b = POIWordUtil.writeDoc("c:\\demo.doc",wr); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}。
JAVA读取WORD,EXCEL,PDF文件的方法是什么呢
JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法 OFFICE文档使用POI控件,PDF可以使用PDFBOX0.7.3控件,完全支持中文,用XPDF也行,不过感觉PDFBOX比较好,而且作者也在更新。
水平有限,万望各位指正 WORD: import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.poi.hwpf.extractor.WordExtractor; import java.io.File; import java.io.InputStream; import java.io.FileInputStream; import com.search.code.Index; public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException { String bodyText = null; try { WordExtractor ex = new WordExtractor(is);//is是WORD文件的InputStream bodyText = ex.getText(); if(!bodyText.equals("")){ index.AddIndex(url, title, bodyText); } }catch (DocCenterException e) { throw new DocCenterException("无法从该Mocriosoft Word文档中提取内容", e); }catch(Exception e){ e.printStackTrace(); } } return null; } Excel: import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFCell; import java.io.File; import java.io.InputStream; import java.io.FileInputStream; import com.search.code.Index; public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException { StringBuffer content = new StringBuffer(); try{。
Java如何操作Word?Java如何操作Word,Excel,
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怎么读取DOC文件
word有微软的专用格式,如果要读取其内容,可以使用jar包,如下: 1。
用jacob. 其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 jacob下载: http://www.matrix.org.cn/down_view.asp?id=13 下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个例子: import java.io.File; import com.jacob.com.*; import com.jacob.activeX.*; public class FileExtracter{ public static void main(String[] args) { ActiveXComponent app = new ActiveXComponent("Word.Application"); String inFile = "c:\\test.doc"; String tpFile = "c:\\temp.htm"; String otFile = "c:\\temp.xml"; boolean flag = false; try { app.setProperty("Visible", new Variant(false)); Object docs = app.getProperty("Documents").toDispatch(); Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch(); Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, 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[] {}); } } } 2。
用apache的poi来抽取word,excel。 poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: 下载经过封装后的poi包: http://www.matrix.org.cn/down_view.asp?id=14 下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子: import java.io.*; import org.textmining.text.extraction.WordExtractor; /** * Title: pdf extraction * Description: email:chris@matrix.org.cn * Copyright: Matrix Copyright (c) 2003 * Company: Matrix.org.cn * @author chris * @version 1.0,who use this example pls remain the declare */ public class PdfExtractor { public PdfExtractor() { } public static void main(String args[]) throws Exception { FileInputStream in = new FileInputStream ("c:\\a.doc"); WordExtractor extractor = new WordExtractor(); String str = extractor.extractText(in); System.out.println("the result length is"+str.length()); System.out.println("the result is"+str); } }。
Java读取数据库打印到word文档问题求教各位大侠,java写
为什么非要写道word文档中? 你可以写进txt文件中,然后使用word进行编辑 如果确实需要,请看下面的例子: import java。
io。File; import java。
io。FileInputStream; import java。
io。FileOutputStream; import org。
apache。poi。
hwpf。extractor。
WordExtractor; import org。apache。
poi。hwpf。
model。io。
HWPFOutputStream; public class Word { /** * 读取纯文本的word文件 */ public String readWord(String doc) throws Exception { String context = null; WordExtractor extractor = null; //纯文本的遍历器 try { FileInputStream in = new FileInputStream(new File(doc)); extractor = new WordExtractor(in); context = extractor。 getText(); } catch (Exception e) { e。
printStackTrace(); } return context; } /** * 对word文档做写操作 * */ public boolean writeWord(String path, String content) throws Exception { boolean w = false; try { byte b[] = content。 getBytes(); FileOutputStream fs = new FileOutputStream(path); HWPFOutputStream hos = new HWPFOutputStream(); hos。
write(b, 0, b。length); hos。
writeTo(fs); hos。close(); w=true; } catch (Exception e) { e。
printStackTrace(); } return w; } public static void main(String[] args) { ReadWord rw = new ReadWord(); try{ String text = rw。 readDoc("D:\\workspace\\MyUntil\\t。
doc"); rw。writeDoc("D:\\workspace\\MyUntil\\d。
doc", text); }catch(Exception e){ e。 printStackTrace(); } } } 所需的包请各位自己上apache的官网上poi工程目录下下吧 poi-3。
5-final poi-contrib-3。5-final poi-ooxml-3。
5-final poi-scratchpad-3。5-final 。
求问JAVA怎么读取DOC文件
用jacob. 其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 jacob下载:
下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个例子: import java.io.File; import com.jacob.com.*; import com.jacob.activeX.*; public class FileExtracter{ public static void main(String[] args) { ActiveXComponent app = new ActiveXComponent("Word.Application"); String inFile = "c:\\test.doc"; String tpFile = "c:\\temp.htm"; String otFile = "c:\\temp.xml"; boolean flag = false; try { app.setProperty("Visible", new Variant(false)); Object docs = app.getProperty("Documents").toDispatch(); Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch(); Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, 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[] {}); } } } 2。
转载请注明出处51数据库 » java怎么导入word文档
ZCG曳