poi导出word时设置兼容性

时间:2022-04-16 10:25:00

接上一篇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;
}
}
}