怎用用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
java导出word代码如下:package com.bank.util;import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Cell;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Element;import com.lowagie.text.Font;import com.lowagie.text.FontFactory;import com.lowagie.text.Image;import com.lowagie.text.PageSize;import com.lowagie.text.Paragraph;import com.lowagie.text.Phrase;import com.lowagie.text.Table;import com.lowagie.text.pdf.BaseFont;import com.lowagie.text.rtf.RtfWriter2;public class WordTools { public void createDocContext(String file) throws DocumentException, IOException { // 设置纸张大小 Document document = new Document(PageSize.A4); // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中 RtfWriter2.getInstance(document, new FileOutputStream(file)); document.open(); // 设置中文字体 BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // 标题字体风格 Font titleFont = new Font(bfChinese, 12, Font.BOLD); // 正文字体风格 Font contextFont = new Font(bfChinese, 10, Font.NORMAL); Paragraph title = new Paragraph("标题"); // 设置标题格式对齐方式 title.setAlignment(Element.ALIGN_CENTER); title.setFont(titleFont); document.add(title); String contextString = "iText是一个能够快速产生PDF文件的java类库。
" + " \n"// 换行 + "iText的java类对于那些要产生包含文本," + "表格,图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。
" + "使用iText与PDF能够使你正确的控制Servlet的输出。"; Paragraph context = new Paragraph(contextString); // 正文格式左对齐 context.setAlignment(Element.ALIGN_LEFT); context.setFont(contextFont); // 离上一段落(标题)空的行数 context.setSpacingBefore(5); // 设置第一行空的列数 context.setFirstLineIndent(20); document.add(context); //利用类FontFactory结合Font和Color可以设置各种各样字体样式 /** * Font.UNDERLINE 下划线,Font.BOLD 粗体 */ Paragraph underline = new Paragraph("下划线的实现", FontFactory.getFont( FontFactory.HELVETICA_BOLDOBLIQUE, 18, Font.UNDERLINE, new Color(0, 0, 255))); document.add(underline); // 设置 Table 表格 Table aTable = new Table(3); int width[] = {25,25,50}; aTable.setWidths(width);//设置每列所占比例 aTable.setWidth(90); // 占页面宽度 90% aTable.setAlignment(Element.ALIGN_CENTER);//居中显示 aTable.setAlignment(Element.ALIGN_MIDDLE);//纵向居中显示 aTable.setAutoFillEmptyCells(true); //自动填满 aTable.setBorderWidth(1); //边框宽度 aTable.setBorderColor(new Color(0, 125, 255)); //边框颜色 aTable.setPadding(0);//衬距,看效果就知道什么意思了 aTable.setSpacing(0);//即单元格之间的间距 aTable.setBorder(2);//边框 //设置表头 /** * cell.setHeader(true);是将该单元格作为表头信息显示; * cell.setColspan(3);指定了该单元格占3列; * 为表格添加表头信息时,要注意的是一旦表头信息添加完了之后, \ * 必须调用 endHeaders()方法,否则当表格跨页后,表头信息不会再显示 */ Cell haderCell = new Cell("表格表头"); haderCell.setHeader(true); haderCell.setColspan(3); aTable.addCell(haderCell); aTable.endHeaders(); Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN); Cell cell = new Cell(new Phrase("这是一个测试的 3*3 Table 数据", fontChinese )); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setBorderColor(new Color(255, 0, 0)); cell.setRowspan(2); aTable.addCell(cell); aTable.addCell(new Cell("#1")); aTable.addCell(new Cell("#2")); aTable.addCell(new Cell("#3")); aTable.addCell(new Cell("#4")); Cell cell3 = new Cell(new Phrase("一行三列数据", fontChinese )); cell3.setColspan(3); cell3.setVerticalAlignment(Element.ALIGN_CENTER); aTable.addCell(cell3); document.add(aTable); document.add(new Paragraph("\n")); //添加图片// Image img=Image.getInstance("http://127.0.0.1:8080/testSystem/images/1_r1_c1.png");// img.setAbsolutePosition(0, 0);// img.setAlignment(Image.RIGHT);//设置图片显示位置// img.scaleAbsolute(12,35);//直接设定显示尺寸// img.scalePercent(50);//表示显示的大小为原尺寸的50%// img.scalePercent(25, 12);//图像高宽的显示比例// img.setRotation(30);//图像旋转一定角度// document.add(img); document.close(); } public static void main(String[] args){ WordTools b=new WordTools(); try { b.createDocContext("d:/demo.doc"); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOExceptio。
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导出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中编辑文档完全一样。
怎样才能把word文件读取并显示在java中?请各位指教
java读取word文档时,虽然网上介绍了很多插件poi、java2Word、jacob、itext等等,poi无法读取格式(新的API估计行好像还在处于研发阶段,不太稳定,做项目不太敢用);java2Word、jacob容易报错找不到注册,比较诡异,我曾经在不同的机器上试过,操作方法完全一致,有的机器不报错,有的报错,去他们论坛找高人解决也说不出原因,项目部署用它有点玄;itxt好像写很方便但是我查了好久资料没有见到过关于读的好办法。经过一番选择还是折中点采用rtf最好,毕竟rtf是开源格式,不需要借助任何插件,只需基本IO操作外加编码转换即可。rtf格式文件表面看来和doc没啥区别,都可以用word打开,各种格式都可以设定。
具体代码可以参考:
转载请注明出处51数据库 » word导入导出java