word导出用的docx4j
public static void exportWordImg(String wordpath,List<String> contentlist,String[] imglist) throws Exception{ exportQuestionWord t=new exportQuestionWord(); WordprocessingMLPackage wordMLPackage = t.createWordprocessingMLPackage(); MainDocumentPart mp = wordMLPackage.getMainDocumentPart(); ObjectFactory factory = Context.getWmlObjectFactory(); //图片页眉 //Relationship relationship =t.createHeaderPart(wordMLPackage, mp, factory); //文字页眉 //Relationship relationship =t.createTextHeaderPart(wordMLPackage, mp, factory, "页眉", JcEnumeration.CENTER); //t.createHeaderReference(wordMLPackage, mp, factory, relationship); t.addParagraphTest(wordMLPackage, mp, factory,contentlist,imglist); //t.addPageBreak(wordMLPackage, factory); //t.createNormalTableTest(wordMLPackage, mp, factory); //页脚 //relationship =t.createFooterPageNumPart(wordMLPackage, mp, factory); //t.createFooterReference(wordMLPackage, mp, factory, relationship); t.saveWordPackage(wordMLPackage, new File(wordpath)); } public void addParagraphTest(WordprocessingMLPackage wordMLPackage, MainDocumentPart t, ObjectFactory factory,List<String> contentlist,String[] imglist) throws Exception { RPr titleRPr = getRPr(factory, "黑体", "000000", "30", STHint.EAST_ASIA, true, false, false, false); RPr boldRPr = getRPr(factory, "宋体", "000000", "24", STHint.EAST_ASIA, true, false, false, false); RPr fontRPr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA, false, false, false, false); P paragraph=factory.createP(); Text txt = null; R run=null; File file=null; InputStream is=null; if(contentlist!=null || contentlist.size()>0){ for (int i = 0; i < contentlist.size(); i ) { if(contentlist.get(i).contains("22.发生肺水肿时的应急处理错误的是")){ paragraph = factory.createP(); file = new File(imglist[0]); is = new FileInputStream(file); txt = factory.createText(); txt.setValue(contentlist.get(i)); run = factory.createR(); run.getContent().add(txt); run.setRPr(boldRPr); paragraph.getContent().add(run); setParagraphSpacing(factory, paragraph,JcEnumeration.LEFT, "0", "3"); createImageParagraph(wordMLPackage, factory, paragraph, "img_1",null, BufferUtil.getBytesFromInputStream(is), JcEnumeration.LEFT); t.addObject(paragraph); }else{ paragraph = factory.createP(); txt = factory.createText(); txt.setValue(contentlist.get(i)); run = factory.createR(); run.getContent().add(txt); run.setRPr(boldRPr); paragraph.getContent().add(run); setParagraphSpacing(factory, paragraph,JcEnumeration.LEFT, "0", "3"); t.addObject(paragraph); } } } //段前8磅 段后0.5磅 /*setParagraphSpacing(factory, paragraph,JcEnumeration.LEFT, "160", "10"); t.addObject(paragraph);*/ /* file = new File(""); is = new java.io.FileInputStream(file); createImageParagraph(wordMLPackage, factory, paragraph, "img_2", StringUtils.leftPad("B.", 20) , BufferUtil.getBytesFromInputStream(is), JcEnumeration.LEFT); setParagraphSpacing(factory, paragraph,JcEnumeration.LEFT, "1", "3"); t.addObject(paragraph);*/ } public WordprocessingMLPackage createWordprocessingMLPackage() throws Exception { return WordprocessingMLPackage.createPackage(); } public void saveWordPackage(WordprocessingMLPackage wordPackage, File file) throws Exception { wordPackage.save(file); } // 分页 public void addPageBreak(WordprocessingMLPackage wordMLPackage, ObjectFactory factory) { MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); Br breakObj = new Br(); breakObj.setType(STBrType.PAGE); P paragraph = factory.createP(); paragraph.getContent().add(breakObj); documentPart.addObject(paragraph); } //段落中插入文字和图片 public P createImageParagraph(WordprocessingMLPackage wordMLPackage, ObjectFactory factory,P p,String fileName, String content,byte[] bytes, JcEnumeration jcEnumeration) throws Exception { BinaryPartAbstractImage imagePart = BinaryPartAbstractImage .createImagePart(wordMLPackage, bytes); Inline inline = imagePart.createImageInline(fileName, "这是图片", 1, 2, false); Text text = factory.createText(); text.setValue(content); text.setSpace("preserve"); R run = factory.createR(); p.getContent().add(run); run.getContent().add(text); Drawing drawing = factory.createDrawing(); run.getContent().add(drawing); drawing.getAnchorOrInline().add(inline); PPr pPr = p.getPPr(); if (pPr == null) { pPr = factory.createPPr(); } Jc jc = pPr.getJc(); if (jc == null) { jc = new Jc(); } jc.setVal(jcEnumeration); pPr.setJc(jc); p.setPPr(pPr); setParagraphSpacing(factory, p, jcEnumeration, "0", "0"); return p; } /** * 创建字体 * * @param isBlod * 粗体 * @param isUnderLine * 下划线 * @param isItalic * 斜体 * @param isStrike * 删除线 */ public RPr getRPr(ObjectFactory factory, String fontFamily, String colorVal, String fontSize, STHint sTHint, boolean isBlod, boolean isUnderLine, boolean isItalic, boolean isStrike) { RPr rPr = factory.createRPr(); RFonts rf = new RFonts(); rf.setHint(sTHint); rf.setAscii(fontFamily); rf.setHAnsi(fontFamily); rPr.setRFonts(rf); BooleanDefaultTrue bdt = factory.createBooleanDefaultTrue(); rPr.setBCs(bdt); if (isBlod) { rPr.setB(bdt); } if (isItalic) { rPr.setI(bdt); } if (isStrike) { rPr.setStrike(bdt); } if (isUnderLine) { U underline = new U(); underline.setVal(UnderlineEnumeration.SINGLE); rPr.setU(underline); } Color color = new Color(); color.setVal(colorVal); rPr.setColor(color); HpsMeasure sz = new HpsMeasure(); sz.setVal(new BigInteger(fontSize)); rPr.setSz(sz); rPr.setSzCs(sz); return rPr; } // 水平对齐方式 // TODO 垂直对齐没写 public void setParagraphAlign(ObjectFactory factory, P p, JcEnumeration jcEnumeration) { PPr pPr = p.getPPr(); if (pPr == null) { pPr = factory.createPPr(); } Jc jc = pPr.getJc(); if (jc == null) { jc = new Jc(); } jc.setVal(jcEnumeration); pPr.setJc(jc); p.setPPr(pPr); } //设置段落间距 public void setParagraphSpacing(ObjectFactory factory, P p, JcEnumeration jcEnumeration,String before,String after) { PPr pPr = p.getPPr(); if (pPr == null) { pPr = factory.createPPr(); } Jc jc = pPr.getJc(); if (jc == null) { jc = new Jc(); } jc.setVal(jcEnumeration); pPr.setJc(jc); PPrBase.Spacing spacing=new PPrBase.Spacing(); spacing.setBefore(new BigInteger(before)); spacing.setAfter(new BigInteger(after)); spacing.setLineRule(STLineSpacingRule.AUTO); pPr.setSpacing(spacing); p.setPPr(pPr); } public P createHeaderBlankP(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, JcEnumeration jcEnumeration) throws Exception{ P p = factory.createP(); R run = factory.createR(); p.getContent().add(run); PPr pPr = p.getPPr(); if (pPr == null) { pPr = factory.createPPr(); } Jc jc = pPr.getJc(); if (jc == null) { jc = new Jc(); } jc.setVal(jcEnumeration); pPr.setJc(jc); PPrBase.PBdr pBdr=pPr.getPBdr(); if(pBdr==null){ pBdr=factory.createPPrBasePBdr(); } CTBorder value=new CTBorder(); value.setVal(STBorder.SINGLE); value.setColor("000000"); value.setSpace(new BigInteger("0")); value.setSz(new BigInteger("3")); pBdr.setBetween(value); pPr.setPBdr(pBdr); p.setPPr(pPr); setParagraphSpacing(factory, p, jcEnumeration, "0", "0"); return p; } /** * 标题样式 */ public static void formatTitleWord(XWPFDocument document,String title){ //添加标题 XWPFParagraph titleParagraph = document.createParagraph(); //设置段落居中 titleParagraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun titleParagraphRun = titleParagraph.createRun(); titleParagraphRun.setText(title); titleParagraphRun.setColor("000000"); titleParagraphRun.setFontSize(16); } /** * 段落样式 */ public static void formatParagraphWord(XWPFDocument document,List<String> contentList){ //段落 XWPFParagraph firstParagraph = document.createParagraph(); XWPFRun run = firstParagraph.createRun(); if(contentList!=null && contentList.size()>0){ for (int i = 0; i < contentList.size(); i ) { run.setText(contentList.get(i)); } } run.setColor("000000"); run.setFontSize(12); //设置段落背景颜色 /*CTShd cTShd = run.getCTR().addNewRPr().addNewShd(); cTShd.setVal(STShd.CLEAR); cTShd.setFill("");*/ } /** * 小标题段落样式 */ public static void formatParagraphWord2(XWPFDocument document,List<String> titleList){ //段落 XWPFParagraph firstParagraph = document.createParagraph(); XWPFRun run = firstParagraph.createRun(); if(titleList!=null && titleList.size()>0){ for (int i = 0; i < titleList.size(); i ) { run.setText(titleList.get(i)); } } run.setColor("000000"); run.setFontSize(16); //设置段落背景颜色 /*CTShd cTShd = run.getCTR().addNewRPr().addNewShd(); cTShd.setVal(STShd.CLEAR); cTShd.setFill("");*/ } /** * 设置页眉,页脚 */ public static void formatHeaderFooterWord(XWPFDocument document){ CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr); //添加页眉 CTP ctpHeader = CTP.Factory.newInstance(); CTR ctrHeader = ctpHeader.addNewR(); CTText ctHeader = ctrHeader.addNewT(); String headerText = ""; ctHeader.setStringValue(headerText); XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document); //设置为右对齐 headerParagraph.setAlignment(ParagraphAlignment.RIGHT); XWPFParagraph[] parsHeader = new XWPFParagraph[1]; parsHeader[0] = headerParagraph; try { policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader); } catch (IOException e) { e.printStackTrace(); } //添加页脚 CTP ctpFooter = CTP.Factory.newInstance(); CTR ctrFooter = ctpFooter.addNewR(); CTText ctFooter = ctrFooter.addNewT(); String footerText = ""; ctFooter.setStringValue(footerText); XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document); headerParagraph.setAlignment(ParagraphAlignment.CENTER); XWPFParagraph[] parsFooter = new XWPFParagraph[1]; parsFooter[0] = footerParagraph; try { policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter); } catch (IOException e) { e.printStackTrace(); } }
public static void main(String[] args) { try { //list1数据,imglist要插入的图片 String[] imglist={"/Users/limeng/word/ccc0.jpg"}; exportWordImg("/Users/limeng/word/bbb5.docx",list1,imglist); } catch (Exception e) { e.printStackTrace(); } }