poi导出word:如果不要导出带图片的word 用此类就可以啦 ,如果需要导出带图片的文件 需要把 poi导出word[1]类也要加入
- "font-size:14px;">package
util; -
- import
java.io.FileInputStream; - import
java.io.FileOutputStream; - import
java.text.SimpleDateFormat; - import
java.util.Date; -
- import
org.apache.poi.xwpf.usermodel.BreakClear; - import
org.apache.poi.xwpf.usermodel.LineSpacingRule; - import
org.apache.poi.xwpf.usermodel.ParagraphAlignment; - import
org.apache.poi.xwpf.usermodel.TextAlignment; - import
org.apache.poi.xwpf.usermodel.XWPFDocument; - import
org.apache.poi.xwpf.usermodel.XWPFParagraph; - import
org.apache.poi.xwpf.usermodel.XWPFRun; -
-
- public
class SimpleDocument { -
-
public static void main(String[] args) throws Exception { - //
XWPFDocument doc = new XWPFDocument(); //导不带图片word - //
OPCPackage pack = POIXMLDocument.openPackage(""); -
CustomXWPFDocument doc = new CustomXWPFDocument(); //导带图片word -
XWPFParagraph p1 = doc.createParagraph(); -
p1.setAlignment(ParagraphAlignment.CENTER);//设置字体对齐方式 -
p1.setWordWrap(true); -
p1.setPageBreak(false);//设置是否分页 -
p1.setSpacingLineRule(LineSpacingRule.EXACT); - //
p1.setIndentationFirstLine(2400);//设置当前行往后缩多少 -
p1.setVerticalAlignment(TextAlignment.CENTER); -
XWPFRun r1 = p1.createRun(); -
r1.setText("现场监理"); -
r1.setFontSize(40); -
r1.setBold(true);//是否加粗 -
r1.setFontFamily("Courier");//字体类型 - //
r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);//设置下划线 -
r1.setTextPosition(100);//设置上下两行的间距 -
-
XWPFParagraph p2 = doc.createParagraph(); -
p2.setAlignment(ParagraphAlignment.CENTER); -
p2.setWordWrap(true); -
p2.setPageBreak(false);//设置是否分页 -
p2.setSpacingLineRule(LineSpacingRule.EXACT); -
-
XWPFRun r2 = p2.createRun(); -
r2.setText("监理日记"); -
r2.setFontSize(20); -
XWPFRun r3 = p2.createRun(); -
r3.setStrike(false);//是否在字体上加线 -
r3.setFontSize(20); - //
r3.setSubscript(VerticalAlign.SUPERSCRIPT);//注释 -
-
- //
int ind = doc.addPicture(new FileInputStream("e:\\1.jpg"), XWPFDocument.PICTURE_TYPE_JPEG); - //
int ind = doc.addPicture(new FileInputStream("http:\\121.32.24.243:5050\\mwuploadfiles\\superupload\\8db9d1f868494dfeb7f96b83 f6e2e93d\\20131219\\201312190938098841387416 31946212816041006258.jpg"), XWPFDocument.PICTURE_TYPE_JPEG); -
int ind = doc.addPicture(new FileInputStream("F:\\apache-tomcat-6.0.29\\webapps\\mwuploadfiles\\superupload\\8db9d1f868494dfeb7f96b83 f6e2e93d\\20131219\\201312190938098841387416 31946212816041006258.jpg"), XWPFDocument.PICTURE_TYPE_JPEG); -
doc.createPicture(ind, 259, 178); -
-
XWPFParagraph p3 = doc.createParagraph(); -
p3.setWordWrap(true); -
p3.setPageBreak(false);//设置是否分页 -
p3.setAlignment(ParagraphAlignment.BOTH); -
p3.setSpacingLineRule(LineSpacingRule.EXACT); -
p3.setIndentationFirstLine(600);//设置当前行往后缩多少 -
-
XWPFRun r4 = p3.createRun(); -
r4.setTextPosition(20); -
r4.setText("To be, or not to be: that is the question: " -
+ "Whether 'tis nobler in the mind to suffer " -
+ "The slings and arrows of outrageous fortune, " -
+ "Or to take arms against a sea of troubles, " -
+ "And by opposing end them? To die: to sleep; "); - //
r4.addBreak(BreakType.PAGE); -
r4.addCarriageReturn();//换行 -
r4.setText("No more; and by a sleep to say we end " -
+ "The heart-ache and the thousand natural shocks " -
+ "That flesh is heir to, 'tis a consummation " -
+ "Devoutly to be wish'd. To die, to sleep; " -
+ "To sleep: perchance to dream: ay, there's the rub; " -
+ "......."); -
r4.setItalic(true);//设置斜体 -
-
-
XWPFRun r5 = p3.createRun(); -
r5.setTextPosition(-10); -
r5.setText("For in that sleep of death what dreams may come"); -
r5.addCarriageReturn(); -
r5.setText("When we have shuffled off this mortal coil," -
+ "Must give us pause: there's the respect" -
+ "That makes calamity of so long life;"); -
r5.addBreak(); -
r5.setText("For who would bear the whips and scorns of time," -
+ "The oppressor's wrong, the proud man's contumely,"); -
-
r5.addBreak(BreakClear.ALL); -
r5.setText("The pangs of despised love, the law's delay," -
+ "The insolence of office and the spurns" + "......."); -
-
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS"); -
String fileNameString = format.format(new Date()); -
FileOutputStream out = new FileOutputStream("e:\"+fileNameString+".doc"); -
doc.write(out); -
System.out.println(doc); -
out.close(); -
-
} - }