poi-对于word的操作(二)

poi对于word文本的底纹和下划线的样式的展现

  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 }
原文地址:https://www.cnblogs.com/dayuruozhi/p/6490868.html