poi對於word文本的底紋和下劃線的樣式的展示java
1 package poi.test; 2 import java.io.FileOutputStream; 3 import java.math.BigInteger; 4 import java.util.Random; 5 6 import org.apache.poi.xwpf.usermodel.BreakType; 7 import org.apache.poi.xwpf.usermodel.ParagraphAlignment; 8 import org.apache.poi.xwpf.usermodel.TextAlignment; 9 import org.apache.poi.xwpf.usermodel.XWPFDocument; 10 import org.apache.poi.xwpf.usermodel.XWPFParagraph; 11 import org.apache.poi.xwpf.usermodel.XWPFRun; 12 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; 13 //import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHighlight; 14 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMeasure; 15 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr; 16 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; 17 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd; 18 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing; 19 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline; 20 //import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor; 21 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STLineSpacingRule; 22 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd; 23 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline; 24 25 public class StyleTest2 { 26 public static void main(String[] args) throws Exception { 27 StyleTest2 t = new StyleTest2(); 28 XWPFDocument doc = new XWPFDocument(); 29 // 需關閉護眼色才能看到效果 30 //t.setDocumentbackground(doc, "FDE9D9");//設置頁面背景色 31 t.testSetUnderLineStyle(doc);//設置下劃線樣式以及突出顯示文本 32 t.addNewPage(doc, BreakType.PAGE); 33 t.testSetShdStyle(doc);//設置文字底紋 34 t.saveDocument(doc,"e:/"+ System.currentTimeMillis() + ".docx"); 35 } 36 37 public void testSetUnderLineStyle(XWPFDocument doc) { 38 String[] colors = new String[] { "CCA6EF", "DD999D", "4FCEF0", 39 "7A7A7A", "F3C917", "FFA932", "C7B571", "535354", "5FD2F1", 40 "B5E900", "FEF8B6" }; 41 Random random = new Random(); 42 // 這裏爲了方便測試寫了數字,推薦寫英文樣式 43 for (int i = 1; i <= 18; i++) { 44 XWPFParagraph p = doc.createParagraph(); 45 setParagraphFontInfoAndUnderLineStyle(p, 46 "本文是以poi3.9讀寫2010word、2010excel、2010ppt", "華文行楷", "000000","22", 47 false, false, false, true, 48 i,colors[Math.abs(random.nextInt(colors.length))], false, 0,null); 49 setParagraphSpacingInfo(p, true, "0", "50", false, "0", "0", 50 true,"240", STLineSpacingRule.Enum.forString("auto")); 51 setParagraphAlignInfo(p, ParagraphAlignment.LEFT,TextAlignment.CENTER); 52 } 53 } 54 55 public void testSetShdStyle(XWPFDocument doc) { 56 String[] colors = new String[] { "CCA6EF", "DD999D", "4FCEF0", 57 "7A7A7A", "F3C917", "FFA932", "C7B571", "535354", "5FD2F1", 58 "B5E900", "FEF8B6" }; 59 Random random = new Random(); 60 // 這裏爲了方便測試寫了數字,推薦寫英文樣式 61 for (int i = 1; i <= 38; i++) { 62 XWPFParagraph p = doc.createParagraph(); 63 setParagraphFontInfoAndUnderLineStyle(p, 64 "本文是以poi3.9讀寫2010word、2010excel、2010ppt", "華文行楷", "1D8C56","22", 65 false, false, false, false, 66 i, null, true, i,colors[Math.abs(random.nextInt(colors.length))]); 67 setParagraphSpacingInfo(p, true, "0", "50", false, "0", "0", 68 true,"240", STLineSpacingRule.Enum.forString("auto")); 69 setParagraphAlignInfo(p, ParagraphAlignment.LEFT,TextAlignment.CENTER); 70 } 71 } 72 73 //設定水平對齊方式、垂直對齊方式 74 public void setParagraphAlignInfo(XWPFParagraph p, 75 ParagraphAlignment pAlign, TextAlignment valign) { 76 p.setAlignment(pAlign); 77 p.setVerticalAlignment(valign); 78 } 79 80 //三組數,分別設定 ★段前段後磅數★段前段後行數★間距★ 81 public void setParagraphSpacingInfo(XWPFParagraph p, boolean isSpace,String before, String after, 82 boolean isPLine, String beforeLines,String afterLines, 83 boolean isLine, String line,STLineSpacingRule.Enum lineValue) { 84 CTPPr pPPr = null; 85 if (p.getCTP() != null) { 86 if (p.getCTP().getPPr() != null) { 87 pPPr = p.getCTP().getPPr(); 88 } else { 89 pPPr = p.getCTP().addNewPPr(); 90 } 91 } 92 /** 93 * CTSpacing設置段落 94 */ 95 CTSpacing pSpacing = pPPr.getSpacing() != null ? pPPr.getSpacing() 96 : pPPr.addNewSpacing(); 97 if (isSpace) { 98 // 段前磅數 99 if (before != null) { 100 pSpacing.setBefore(new BigInteger(before)); 101 } 102 // 段後磅數 103 if (after != null) { 104 pSpacing.setAfter(new BigInteger(after)); 105 } 106 } 107 if (isPLine) { 108 // 段前行數 109 if (beforeLines != null) { 110 pSpacing.setBeforeLines(new BigInteger(beforeLines)); 111 } 112 // 段後行數 113 if (afterLines != null) { 114 pSpacing.setAfterLines(new BigInteger(afterLines)); 115 } 116 } 117 // 間距 118 if (isLine) { 119 if (line != null) { 120 pSpacing.setLine(new BigInteger(line)); 121 } 122 if (lineValue != null) { 123 pSpacing.setLineRule(lineValue); 124 } 125 } 126 } 127 128 @SuppressWarnings("deprecation") 129 public void setParagraphFontInfoAndUnderLineStyle(XWPFParagraph p, 130 String content, String fontFamily, String colorVal,String fontSize, 131 boolean isBlod, boolean isItalic,boolean isStrike, boolean isUnderLine, 132 int underLineStyle,String underLineColor, boolean isShd, int shdValue, String shdColor) { 133 XWPFRun pRun = null; 134 if (p.getRuns() != null && p.getRuns().size() > 0) { 135 pRun = p.getRuns().get(0); 136 } else { 137 pRun = p.createRun(); 138 } 139 pRun.setText(content); 140 /** 141 * CTRPr設置頁 142 */ 143 CTRPr pRpr = null; 144 if (pRun.getCTR() != null) { 145 pRpr = pRun.getCTR().getRPr(); 146 if (pRpr == null) { 147 pRpr = pRun.getCTR().addNewRPr(); 148 } 149 } 150 /** 151 * CTFonts設置字體 152 */ 153 // 設置字體 154 CTFonts fonts = pRpr.isSetRFonts() ? pRpr.getRFonts() : pRpr 155 .addNewRFonts(); 156 fonts.setAscii(fontFamily);//---只改變Ascii中的(字母和數字) 157 fonts.setEastAsia(fontFamily);//---只改變中文EastAsia 158 fonts.setHAnsi(fontFamily);//--- 159 160 /** 161 * CTHpsMeasure設置大小 162 */ 163 // 設置字體大小 164 CTHpsMeasure sz = pRpr.isSetSz() ? pRpr.getSz() : pRpr.addNewSz(); 165 sz.setVal(new BigInteger(fontSize)); 166 167 CTHpsMeasure szCs = pRpr.isSetSzCs() ? pRpr.getSzCs() : pRpr 168 .addNewSzCs(); 169 szCs.setVal(new BigInteger(fontSize));//---字體大小 170 171 // 設置字體樣式 172 if (isBlod) { 173 pRun.setBold(isBlod);//---是否加黑加粗 174 } 175 if (isItalic) { 176 pRun.setItalic(isItalic);//---是否傾斜 177 } 178 if (isStrike) { 179 pRun.setStrike(isStrike);//是否有中劃線 180 } 181 if (colorVal != null) { 182 pRun.setColor(colorVal);//---字體顏色1D8C56 183 } 184 185 // // 設置字突出顯示文本---設置的文字的背景顏色,太難看了!! 186 // if (underLineStyle > 0 && underLineStyle < 17) { 187 // CTHighlight hightLight = pRpr.isSetHighlight() ? pRpr 188 // .getHighlight() : pRpr.addNewHighlight(); 189 // hightLight.setVal(STHighlightColor.Enum.forInt(underLineStyle)); 190 // } 191 // 192 // 設置下劃線樣式 193 if (isUnderLine) { 194 CTUnderline u = pRpr.isSetU() ? pRpr.getU() : pRpr.addNewU(); 195 u.setVal(STUnderline.Enum.forInt(Math.abs(underLineStyle % 19))); 196 if (underLineColor != null) { 197 u.setColor(underLineColor); 198 } 199 } 200 /** 201 * CTShd設置底紋 202 */ 203 if (isShd) { 204 // 設置底紋 205 CTShd shd = pRpr.isSetShd() ? pRpr.getShd() : pRpr.addNewShd(); 206 if (shdValue > 0 && shdValue <= 38) { 207 shd.setVal(STShd.Enum.forInt(underLineStyle)); 208 } 209 if (shdColor != null) { 210 shd.setColor(shdColor); 211 } 212 } 213 } 214 215 // // 設置頁面背景色 216 // public void setDocumentbackground(XWPFDocument document, String bgColor) { 217 // CTBackground bg = null; 218 // if( document.getDocument().isSetBackground()){ 219 // bg = document.getDocument().getBackground(); 220 // }else{ 221 // bg = document.getDocument().addNewBackground(); 222 // } 223 // bg.setColor(bgColor); 224 // } 225 226 public void addNewPage(XWPFDocument document, BreakType breakType) { 227 XWPFParagraph xp = document.createParagraph(); 228 xp.createRun().addBreak(breakType); 229 } 230 231 public void saveDocument(XWPFDocument document, String savePath) 232 throws Exception { 233 FileOutputStream fos = new FileOutputStream(savePath); 234 document.write(fos); 235 fos.close(); 236 } 237 }