首页 > 学院 > 开发设计 > 正文

poi导出word时设置兼容性

2019-11-15 01:05:49
字体:
来源:转载
供稿:网友
poi导出Word时设置兼容性

接上一篇poi导出wordhttp://www.VEVb.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;            }        }    }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表