星空网 > 软件开发 > Java

java导出word(带图片)

 1 public class CreateWordDemo {   2 public void createDocContext(String file) throws DocumentException,IOException {  3    // 设置纸张大小    4  Document document = new Document(PageSize.A4);     5 // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中    RtfWriter2.getInstance(document, new FileOutputStream(file));     6 document.open();     7 // 设置中文字体     8 BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);     9 // 标题字体风格    10 Font titleFont = new Font(bfChinese, 12, Font.BOLD);    11 // 正文字体风格    12 Font contextFont = new Font(bfChinese, 10, Font.NORMAL);    13 Paragraph title = new Paragraph("标题");    14 // 设置标题格式对齐方式    15 title.setAlignment(Element.ALIGN_CENTER);    16 title.setFont(titleFont);    17 document.add(title);    18 String contextString = "iText是一个能够快速产生PDF文件的java类库。"        19 + " \n"// 换行        + "iText的java类对于那些要产生包含文本,"        20 + "表格,图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。"        21 + "使用iText与PDF能够使你正确的控制Servlet的输出。";    22 Paragraph context = new Paragraph(contextString);    23 // 正文格式左对齐    24 context.setAlignment(Element.ALIGN_LEFT);    25 context.setFont(contextFont);    26 // 离上一段落(标题)空的行数    27 context.setSpacingBefore(5);    28 // 设置第一行空的列数    29 context.setFirstLineIndent(20);    30 document.add(context);    31 // 利用类FontFactory结合Font和Color可以设置各种各样字体样式    32    33 Paragraph underline = new Paragraph("下划线的实现", FontFactory.getFont(        FontFactory.HELVETICA_BOLDOBLIQUE, 18, Font.UNDERLINE, new Color(0, 0, 255)));    34 document.add(underline);    35 // 设置 Table 表格    36 Table aTable = new Table(3);    37 int width[] = { 25, 25, 50 };    38 aTable.setWidths(width);// 设置每列所占比例    39 aTable.setWidth(90); // 占页面宽度 90%    40 aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示    41 aTable.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示    42 aTable.setAutoFillEmptyCells(true); // 自动填满    43 aTable.setBorderWidth(1); // 边框宽度    44 aTable.setBorderColor(new Color(0, 125, 255)); // 边框颜色    45 aTable.setPadding(2);// 衬距,看效果就知道什么意思了    46 aTable.setSpacing(3);// 即单元格之间的间距    47 aTable.setBorder(2);// 边框    // 设置表头    48     49 Cell haderCell = new Cell("表格表头");    50 haderCell.setHeader(true);    51 haderCell.setColspan(3);    52 aTable.addCell(haderCell);    53 aTable.endHeaders();    54 Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);    55 Cell cell = new Cell(new Phrase("这是一个测试的 3*3 Table 数据", fontChinese));    cell.setVerticalAlignment(Element.ALIGN_TOP);    56 cell.setBorderColor(new Color(255, 0, 0));    57 cell.setRowspan(2);    58 aTable.addCell(cell);    59 aTable.addCell(new Cell("#1"));    60 aTable.addCell(new Cell("#2"));    61 aTable.addCell(new Cell("#3"));    62 aTable.addCell(new Cell("#4"));    63 Cell cell3 = new Cell(new Phrase("一行三列数据", fontChinese));    64 cell3.setColspan(3);    65 cell3.setVerticalAlignment(Element.ALIGN_CENTER);    66 aTable.addCell(cell3);    67 document.add(aTable);    68 document.add(new Paragraph("\n"));    69 // 添加图片 Image.getInstance即可以放路径又可以放二进制字节流   70 Image img = Image.getInstance("d:\\img01800.jpg");    71 img.setAbsolutePosition(0, 0);    72 img.setAlignment(Image.RIGHT);// 设置图片显示位置    73 img.scaleAbsolute(60, 60);// 直接设定显示尺寸    74 // img.scalePercent(50);//表示显示的大小为原尺寸的50%    75 // img.scalePercent(25, 12);//图像高宽的显示比例    76 // img.setRotation(30);//图像旋转一定角度    77 document.add(img);    78 document.close();  79 }  80 81 public static void main(String[] args) {    82    CreateWordDemo word = new CreateWordDemo();    83    String file = "d:/demo1.doc";    84    try {      85           word.createDocContext(file);    86     } catch (DocumentException e) {      87          e.printStackTrace();    88     } catch (IOException e) {      89       e.printStackTrace();    90      }  91   }92 }

 




原标题:java导出word(带图片)

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

檬牛跨境快讯|亚马逊这一政策再次更新!Shopee卖家需完成该操作! :https://www.goluckyvip.com/news/7201.html
速卖通,国际站,Shopee卖家出单速度慢怎么办?(自养号测评经验分享) :https://www.goluckyvip.com/news/7202.html
肯尼亚coc认证费用,肯尼亚coc清关证书:https://www.goluckyvip.com/news/7203.html
中国在德国的海外仓企业有哪些?:https://www.goluckyvip.com/news/7204.html
SONCAP证书是什么,CoC证书,coc清关认证,尼日利亚认证:https://www.goluckyvip.com/news/7205.html
为什么不能错过Tiktok?:https://www.goluckyvip.com/news/7206.html
用户体验至上:独立站脱颖而出的关键要素解析:https://www.goluckyvip.com/news/214219.html
用户体验至上:独立站脱颖而出的关键要素解析:https://www.xlkjsw.com/news/88158.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流