一、复现:
poi版本:
< poi> 3.17 < / poi>
< poi- ooxml> 3.17 < / poi- ooxml>
< poi- ooxml- schemas> 3.17 < / poi- ooxml- schemas>
< dependency>
< groupId> org. apache. poi< / groupId>
< artifactId> poi< / artifactId>
< version> ${ poi} < / version>
< / dependency>
< dependency>
< groupId> org. apache. poi< / groupId>
< artifactId> poi- scratchpad< / artifactId>
< version> ${ poi} < / version>
< / dependency>
< dependency>
< groupId> org. apache. poi< / groupId>
< artifactId> poi- ooxml< / artifactId>
< version> ${ poi- ooxml} < / version>
< / dependency>
< dependency>
< groupId> org. apache. poi< / groupId>
< artifactId> poi- ooxml- schemas< / artifactId>
< version> ${ poi- ooxml- schemas} < / version>
< / dependency>
1、生成word:
2、通过libreoffice转换成pdf:
二、失败尝试:设置单元格长度宽度,设置表格居中均无效。
三、解决思路:
1、在word的单元格中,打几个空格,空格其实在红色指到的地方,然后转换成PDF后发现没问题了。
2、将成功和失败的文档进行对比:最后发现表格中格式不太一样
①成功的
< ? xml version= "1.0" encoding= "UTF-8" standalone= "no" ? >
< xml- fragment xmlns: aink= "http://schemas.microsoft.com/office/drawing/2016/ink" xmlns: am3d= "http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns: cx= "http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns: cx1= "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns: cx2= "http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns: cx3= "http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns: cx4= "http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns: cx5= "http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns: cx6= "http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns: cx7= "http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns: cx8= "http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns: m= "http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns: mc= "http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns: o= "urn:schemas-microsoft-com:office:office" xmlns: oel= "http://schemas.microsoft.com/office/2019/extlst" xmlns: r= "http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns: v= "urn:schemas-microsoft-com:vml" xmlns: w= "http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns: w10= "urn:schemas-microsoft-com:office:word" xmlns: w14= "http://schemas.microsoft.com/office/word/2010/wordml" xmlns: w15= "http://schemas.microsoft.com/office/word/2012/wordml" xmlns: w16= "http://schemas.microsoft.com/office/word/2018/wordml" xmlns: w16cex= "http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns: w16cid= "http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns: w16du= "http://schemas.microsoft.com/office/word/2023/wordml/word16du" xmlns: w16sdtdh= "http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns: w16se= "http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns: wne= "http://schemas.microsoft.com/office/word/2006/wordml" xmlns: wp= "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns: wp14= "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns: wpc= "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns: wpg= "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns: wpi= "http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns: wps= "http://schemas.microsoft.com/office/word/2010/wordprocessingShape" >
< w: tblPr>
< w: tblW w: type= "auto" w: w= "0" / >
< w: tblInd w: type= "dxa" w: w= "10" / >
< w: tblBorders>
< w: top w: color= "auto" w: space= "0" w: sz= "0" w: val= "single" / >
< w: left w: color= "auto" w: space= "0" w: sz= "0" w: val= "single" / >
< w: bottom w: color= "auto" w: space= "0" w: sz= "0" w: val= "single" / >
< w: right w: color= "auto" w: space= "0" w: sz= "0" w: val= "single" / >
< w: insideH w: color= "auto" w: space= "0" w: sz= "0" w: val= "single" / >
< w: insideV w: color= "auto" w: space= "0" w: sz= "0" w: val= "single" / >
< / w: tblBorders>
< w: tblCellMar>
< w: left w: type= "dxa" w: w= "10" / >
< w: right w: type= "dxa" w: w= "10" / >
< / w: tblCellMar>
< w: tblLook w: firstColumn= "0" w: firstRow= "0" w: lastColumn= "0" w: lastRow= "0" w: noHBand= "0" w: noVBand= "0" w: val= "0000" / >
< / w: tblPr>
< w: tblGrid>
< w: gridCol w: w= "26" / >
< / w: tblGrid>
< w: tr w14: paraId= "77529834" w14: textId= "77777777" w: rsidR= "0005540F" >
< w: tblPrEx>
< w: tblCellMar>
< w: top w: type= "dxa" w: w= "0" / >
< w: bottom w: type= "dxa" w: w= "0" / >
< / w: tblCellMar>
< / w: tblPrEx>
< w: tc>
< w: tcPr>
< w: tcW w: type= "auto" w: w= "0" / >
< / w: tcPr>
< w: p w14: paraId= "56EDAEAA" w14: textId= "1DDD1BD7" w: rsidR= "0005540F" w: rsidRDefault= "001E39EF" >
< w: pPr>
< w: rPr>
< w: rFonts w: hint= "eastAsia" / >
< / w: rPr>
< / w: pPr>
< w: r>
< w: rPr>
< w: rFonts w: hint= "eastAsia" / >
< / w: rPr>
< w: t xml: space= "preserve" / >
< / w: r>
< / w: p>
< / w: tc>
< / w: tr>
< / xml- fragment>
②失败的
< ? xml version= "1.0" encoding= "UTF-8" standalone= "no" ? >
< xml- fragment xmlns: main= "http://schemas.openxmlformats.org/wordprocessingml/2006/main" >
< main: tblPr>
< main: tblW main: type= "auto" main: w= "0" / >
< main: tblBorders>
< main: top main: val= "single" / >
< main: left main: val= "single" / >
< main: bottom main: val= "single" / >
< main: right main: val= "single" / >
< main: insideH main: val= "single" / >
< main: insideV main: val= "single" / >
< / main: tblBorders>
< / main: tblPr>
< main: tr>
< main: tc>
< main: p/ >
< / main: tc>
< / main: tr>
< / xml- fragment>
四、解决方案:设置一下样式,自己画的table放入如下方法
private static void setTableStyle ( XWPFTable table) {
CTTbl ctTbl = table. getCTTbl ( ) ;
CTTblPr tblPr = ctTbl. getTblPr ( ) ;
if ( tblPr == null ) {
tblPr = ctTbl. addNewTblPr ( ) ;
}
CTTblWidth tblWidth = tblPr. getTblW ( ) ;
if ( tblWidth == null ) {
tblWidth = tblPr. addNewTblW ( ) ;
}
tblWidth. setType ( STTblWidth . AUTO) ;
tblWidth. setW ( BigInteger . ZERO) ;
CTTblGrid tblGrid = ctTbl. getTblGrid ( ) ;
if ( tblGrid == null ) {
tblGrid = ctTbl. addNewTblGrid ( ) ;
CTTblGridCol ctTblGridCol = tblGrid. addNewGridCol ( ) ;
ctTblGridCol. setW ( BigInteger . valueOf ( 9000 ) ) ;
}
}
五、效果:表格拉宽了。(居中代码就从其他地方抄一下吧,文章很多的)