poi导出word[2]

poi导出word:如果不要导出带图片的word 用此类就可以啦 ,如果需要导出带图片的文件 需要把 poi导出word[1]类也要加入
[java]   vie w plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. "font-size:14px;">package util;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.Date;  
  7.   
  8. import org.apache.poi.xwpf.usermodel.BreakClear;  
  9. import org.apache.poi.xwpf.usermodel.LineSpacingRule;  
  10. import org.apache.poi.xwpf.usermodel.ParagraphAlignment;  
  11. import org.apache.poi.xwpf.usermodel.TextAlignment;  
  12. import org.apache.poi.xwpf.usermodel.XWPFDocument;  
  13. import org.apache.poi.xwpf.usermodel.XWPFParagraph;  
  14. import org.apache.poi.xwpf.usermodel.XWPFRun;  
  15.   
  16.   
  17. public class SimpleDocument  
  18.   
  19.  public static void main(String[] args) throws Exception  
  20. //     XWPFDocument doc new XWPFDocument(); //导不带图片word 
  21. //     OPCPackage pack POIXMLDocument.openPackage("");  
  22.      CustomXWPFDocument doc new CustomXWPFDocument(); //导带图片word  
  23.      XWPFParagraph p1 doc.createParagraph();  
  24.      p1.setAlignment(ParagraphAlignment.CENTER);//设置字体对齐方式  
  25.      p1.setWordWrap(true);  
  26.      p1.setPageBreak(false);//设置是否分页  
  27.      p1.setSpacingLineRule(LineSpacingRule.EXACT);  
  28. //   p1.setIndentationFirstLine(2400);//设置当前行往后缩多少  
  29.      p1.setVerticalAlignment(TextAlignment.CENTER);  
  30.      XWPFRun r1 p1.createRun();  
  31.      r1.setText("现场监理");  
  32.      r1.setFontSize(40);  
  33.      r1.setBold(true);//是否加粗  
  34.      r1.setFontFamily("Courier");//字体类型  
  35. //   r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);//设置下划线  
  36.      r1.setTextPosition(100);//设置上下两行的间距  
  37.   
  38.      XWPFParagraph p2 doc.createParagraph();  
  39.      p2.setAlignment(ParagraphAlignment.CENTER);  
  40.      p2.setWordWrap(true);  
  41.      p2.setPageBreak(false);//设置是否分页  
  42.      p2.setSpacingLineRule(LineSpacingRule.EXACT);  
  43.   
  44.      XWPFRun r2 p2.createRun();  
  45.      r2.setText("监理日记");  
  46.      r2.setFontSize(20);  
  47.      XWPFRun r3 p2.createRun();  
  48.      r3.setStrike(false);//是否在字体上加线  
  49.      r3.setFontSize(20);  
  50. //   r3.setSubscript(VerticalAlign.SUPERSCRIPT);//注释  
  51.        
  52.        
  53. //     int ind doc.addPicture(new FileInputStream("e:\\1.jpg"), XWPFDocument.PICTURE_TYPE_JPEG);   
  54. //     int ind doc.addPicture(new FileInputStream("http:\\121.32.24.243:5050\\mwuploadfiles\\superupload\\8db9d1f868494dfeb7f96b83f6e2e93d\\20131219\\20131219093809884138741631946212816041006258.jpg"), XWPFDocument.PICTURE_TYPE_JPEG);   
  55.      int ind doc.addPicture(new FileInputStream("F:\\apache-tomcat-6.0.29\\webapps\\mwuploadfiles\\superupload\\8db9d1f868494dfeb7f96b83f6e2e93d\\20131219\\20131219093809884138741631946212816041006258.jpg"), XWPFDocument.PICTURE_TYPE_JPEG);   
  56.      doc.createPicture(ind, 259178);   
  57.    
  58.      XWPFParagraph p3 doc.createParagraph();  
  59.      p3.setWordWrap(true);  
  60.      p3.setPageBreak(false);//设置是否分页  
  61.      p3.setAlignment(ParagraphAlignment.BOTH);  
  62.      p3.setSpacingLineRule(LineSpacingRule.EXACT);  
  63.      p3.setIndentationFirstLine(600);//设置当前行往后缩多少  
  64.   
  65.      XWPFRun r4 p3.createRun();  
  66.      r4.setTextPosition(20);  
  67.      r4.setText("To be, or not to be: that is the question: "  
  68.              "Whether 'tis nobler in the mind to suffer "  
  69.              "The slings and arrows of outrageous fortune, "  
  70.              "Or to take arms against sea of troubles, "  
  71.              "And by opposing end them? To die: to sleep; ");  
  72. //   r4.addBreak(BreakType.PAGE);  
  73.      r4.addCarriageReturn();//换行  
  74.      r4.setText("No more; and by sleep to say we end "  
  75.              "The heart-ache and the thousand natural shocks "  
  76.              "That flesh is heir to, 'tis consummation "  
  77.              "Devoutly to be wish'd. To die, to sleep; "  
  78.              "To sleep: perchance to dream: ay, there's the rub; "  
  79.              ".......");  
  80.      r4.setItalic(true);//设置斜体  
  81.        
  82.        
  83.      XWPFRun r5 p3.createRun();  
  84.      r5.setTextPosition(-10);  
  85.      r5.setText("For in that sleep of death what dreams may come");  
  86.      r5.addCarriageReturn();  
  87.      r5.setText("When we have shuffled off this mortal coil,"  
  88.              "Must give us pause: there's the respect"  
  89.              "That makes calamity of so long life;");  
  90.      r5.addBreak();  
  91.      r5.setText("For who would bear the whips and scorns of time,"  
  92.              "The oppressor's wrong, the proud man's contumely,");  
  93.        
  94.      r5.addBreak(BreakClear.ALL);  
  95.      r5.setText("The pangs of despised love, the law's delay,"  
  96.              "The insolence of office and the spurns" ".......");  
  97.      
  98.      SimpleDateFormat format new SimpleDateFormat("yyyyMMddHHmmssSSS");  
  99.      String fileNameString format.format(new Date());  
  100.      FileOutputStream out new FileOutputStream("e:\"+fileNameString+".doc");  
  101.      doc.write(out);  
  102.      System.out.println(doc);  
  103.      out.close();  
  104.   
  105.   
  106. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值