zoukankan      html  css  js  c++  java
  • poi导出word时设置兼容性

    接上一篇poi导出word http://www.cnblogs.com/xiufengd/p/4708680.html

    public static void setAuto(XWPFDocument doc) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, ClassNotFoundException{
            List<POIXMLDocumentPart> list = doc.getRelations();
            //设置一些Word文档的兼容属性
            for(POIXMLDocumentPart p:list)
            {
                if(p instanceof XWPFSettings)
                {
                    XWPFSettings settings = (XWPFSettings)p;
                    //反射获得ctsettings的反射对象
                    Field field = null;
                    //反射三种方式
    //                field = settings.getClass().getDeclaredField("ctSettings");
    //                field = XWPFSettings.class.getDeclaredField("ctSettings");
                    field = Class.forName("org.apache.poi.xwpf.usermodel.XWPFSettings").getDeclaredField("ctSettings");
                    //将私有的成员变量公有化,以便能修改。
                    field.setAccessible(true);
                    //生成新的Word设置类CTSettings的对象实例,设置UlTrailSpace,使得word为行尾的空格显示下划线
                    CTSettings att = CTSettings.Factory.newInstance();
                    att.addNewCompat().addNewUlTrailSpace().setVal(STOnOff.ON);
                    //添加对象
                    field.set(settings, att);
                    break;
                }
            }
        }
  • 相关阅读:
    ARC 080
    CodeForces
    [Lydsy1806月赛] 路径统计
    AGC 022 C
    AGC 022 B
    AGC 020 B
    UVA
    AGC 018 A
    python
    python
  • 原文地址:https://www.cnblogs.com/xiufengd/p/4720397.html
Copyright © 2011-2022 走看看