怎用用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,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大致有6种解决方案:1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。
使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。DLL动态链接库的生成需要windows平台的支持。
该方案只能在windows平台实现,是其局限性。2:Apache POI包括一系列的API,它们可以操作基于MicroSoft OLE 2 Compound Document Format的各种格式文件,可以通过这些API在Java中读写Excel、Word等文件。
他的excel处理很强大,对于word还局限于读取,目前只能实现一些简单文件的操作,不能设置样式。3:Java2word是一个在java程序中调用 MS Office Word 文档的组件(类库)。
该组件提供了一组简单的接口,以便java程序调用他的服务操作Word 文档。 这些服务包括: 打开文档、新建文档、查找文字、替换文字,插入文字、插入图片、插入表格,在书签处插入文字、插入图片、插入表格等。
填充数据到表格中读取表格数据 ,1.1版增强的功能: 指定文本样式,指定表格样式。如此,则可动态排版word文档。
是一种不错的解决方案。4:iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。
通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。功能强大。
5:JSP输出样式,该方案实现简单,但是处理样式有点缺陷,简单的导出可以使用。6:用XML做就很简单了。
Word从2003开始支持XML格式,大致的思路是先用office2003或者2007编辑好word的样式,然后另存为xml,将xml翻译为FreeMarker模板,最后用java来解析FreeMarker模板并输出Doc。经测试这样方式生成的word文档完全符合office标准,样式、内容控制非常便利,打印也不会变形,生成的文档和office中编辑文档完全一样。
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 。
转载请注明出处51数据库 » java导入word文档格式
SIR113583291