Java结合docx4j生成docx文件

尊重原创,原文链接


平时项目中经常碰到报表的生成,在此简单描述下使用java结合docx4j技术,生成docx文件的一些基本方法,仅供参考,代码如下: 

Java代码   收藏代码
  1. import java.awt.image.BufferedImage;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.math.BigInteger;  
  6. import java.text.SimpleDateFormat;  
  7. import java.util.Date;  
  8. import java.util.List;  
  9.   
  10. import javax.imageio.ImageIO;  
  11. import javax.xml.bind.JAXBException;  
  12.   
  13. import org.docx4j.XmlUtils;  
  14. import org.docx4j.dml.wordprocessingDrawing.Inline;  
  15. import org.docx4j.jaxb.Context;  
  16. import org.docx4j.model.structure.SectionWrapper;  
  17. import org.docx4j.model.table.TblFactory;  
  18. import org.docx4j.openpackaging.exceptions.InvalidFormatException;  
  19. import org.docx4j.openpackaging.packages.WordprocessingMLPackage;  
  20. import org.docx4j.openpackaging.parts.Part;  
  21. import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;  
  22. import org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart;  
  23. import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart;  
  24. import org.docx4j.openpackaging.parts.relationships.Namespaces;  
  25. import org.docx4j.relationships.Relationship;  
  26. import org.docx4j.wml.BooleanDefaultTrue;  
  27. import org.docx4j.wml.CTBorder;  
  28. import org.docx4j.wml.CTDocProtect;  
  29. import org.docx4j.wml.CTSettings;  
  30. import org.docx4j.wml.CTTblLayoutType;  
  31. import org.docx4j.wml.CTVerticalJc;  
  32. import org.docx4j.wml.FooterReference;  
  33. import org.docx4j.wml.Ftr;  
  34. import org.docx4j.wml.HdrFtrRef;  
  35. import org.docx4j.wml.Jc;  
  36. import org.docx4j.wml.JcEnumeration;  
  37. import org.docx4j.wml.ObjectFactory;  
  38. import org.docx4j.wml.P;  
  39. import org.docx4j.wml.PPr;  
  40. import org.docx4j.wml.RPr;  
  41. import org.docx4j.wml.STAlgClass;  
  42. import org.docx4j.wml.STAlgType;  
  43. import org.docx4j.wml.STBorder;  
  44. import org.docx4j.wml.STCryptProv;  
  45. import org.docx4j.wml.STDocProtect;  
  46. import org.docx4j.wml.STHint;  
  47. import org.docx4j.wml.STTblLayoutType;  
  48. import org.docx4j.wml.STVerticalJc;  
  49. import org.docx4j.wml.SectPr;  
  50. import org.docx4j.wml.Tbl;  
  51. import org.docx4j.wml.TblGrid;  
  52. import org.docx4j.wml.TblGridCol;  
  53. import org.docx4j.wml.TblPr;  
  54. import org.docx4j.wml.TblWidth;  
  55. import org.docx4j.wml.Tc;  
  56. import org.docx4j.wml.TcPr;  
  57. import org.docx4j.wml.Tr;  
  58. import org.docx4j.wml.TrPr;  
  59. import org.docx4j.wml.SectPr.PgMar;  
  60. import org.docx4j.wml.TcPrInner.HMerge;  
  61. import org.docx4j.wml.TcPrInner.TcBorders;  
  62. import org.docx4j.wml.TcPrInner.VMerge;  
  63. import org.krysalis.barcode4j.impl.code39.Code39Bean;  
  64. import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;  
  65.   
  66. /** 
  67.  * 功能描述:报表的工具类 
  68.  * @author myclover 
  69.  * 
  70.  */  
  71. @SuppressWarnings("all")  
  72. public class Docx4jUtils {  
  73.       
  74.     /** 
  75.      * 功能描述:创建文档处理包对象 
  76.      * @return  返回值:返回文档处理包对象 
  77.      * @throws Exception 
  78.      * @author myclover 
  79.      */  
  80.     public static WordprocessingMLPackage createWordprocessingMLPackage() throws Exception{  
  81.         return WordprocessingMLPackage.createPackage();  
  82.     }  
  83.       
  84.     /** 
  85.      * 功能描述:获取文档的可用宽度 
  86.      * @param wordPackage 文档处理包对象 
  87.      * @return            返回值:返回值文档的可用宽度 
  88.      * @throws Exception 
  89.      * @author myclover 
  90.      */  
  91.     private static int getWritableWidth(WordprocessingMLPackage wordPackage)throws Exception{  
  92.         return wordPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();  
  93.     }  
  94.       
  95.     /** 
  96.      * 功能描述:创建文档表格,上下双边框,左右不封口 
  97.      * @param rows    行数 
  98.      * @param cols    列数 
  99.      * @param widths  每列的宽度 
  100.      * @return   返回值:返回表格对象 
  101.      * @author myclover 
  102.      */  
  103.     public static Tbl createTable(int rows, int cols, int[] widths) {  
  104.         ObjectFactory factory = Context.getWmlObjectFactory();  
  105.         Tbl tbl = factory.createTbl();  
  106.         // w:tblPr  
  107.         StringBuffer tblSb = new StringBuffer();  
  108.         tblSb.append("<w:tblPr ").append(Namespaces.W_NAMESPACE_DECLARATION).append(">");  
  109.         tblSb.append("<w:tblStyle w:val=\"TableGrid\"/>");  
  110.         tblSb.append("<w:tblW w:w=\"0\" w:type=\"auto\"/>");  
  111.         //上边框双线  
  112.         tblSb.append("<w:tblBorders><w:top w:val=\"double\" w:sz=\"4\" w:space=\"0\" w:color=\"auto\"/>");  
  113.         //左边无边框  
  114.         tblSb.append("<w:left w:val=\"none\" w:sz=\"0\" w:space=\"0\" w:color=\"auto\"/>");  
  115.         //下边框双线  
  116.         tblSb.append("<w:bottom w:val=\"double\" w:sz=\"4\" w:space=\"0\" w:color=\"auto\"/>");  
  117.         //右边无边框  
  118.         tblSb.append("<w:right w:val=\"none\" w:sz=\"0\" w:space=\"0\" w:color=\"auto\"/>");  
  119.         tblSb.append("</w:tblBorders>");  
  120.         tblSb.append("<w:tblLook w:val=\"04A0\"/>");  
  121.         tblSb.append("</w:tblPr>");  
  122.         TblPr tblPr = null;  
  123.         try {  
  124.             tblPr = (TblPr) XmlUtils.unmarshalString(tblSb.toString());  
  125.         } catch (JAXBException e) {  
  126.             e.printStackTrace();  
  127.         }  
  128.         tbl.setTblPr(tblPr);  
  129.         if (tblPr != null) {  
  130.             Jc jc = factory.createJc();  
  131.             //单元格居中对齐  
  132.             jc.setVal(JcEnumeration.CENTER);  
  133.             tblPr.setJc(jc);  
  134.             CTTblLayoutType tbll = factory.createCTTblLayoutType();  
  135.             // 固定列宽  
  136.             tbll.setType(STTblLayoutType.FIXED);  
  137.             tblPr.setTblLayout(tbll);  
  138.         }  
  139.         // <w:tblGrid><w:gridCol w:w="4788"/>  
  140.         TblGrid tblGrid = factory.createTblGrid();  
  141.         tbl.setTblGrid(tblGrid);  
  142.         // Add required <w:gridCol w:w="4788"/>  
  143.         for (int i = 1; i <= cols; i++) {  
  144.             TblGridCol gridCol = factory.createTblGridCol();  
  145.             gridCol.setW(BigInteger.valueOf(widths[i - 1]));  
  146.             tblGrid.getGridCol().add(gridCol);  
  147.         }  
  148.         // Now the rows  
  149.         for (int j = 1; j <= rows; j++) {  
  150.             Tr tr = factory.createTr();  
  151.             tbl.getContent().add(tr);  
  152.             // The cells  
  153.             for (int i = 1; i <= cols; i++) {  
  154.                 Tc tc = factory.createTc();  
  155.                 tr.getContent().add(tc);  
  156.                 TcPr tcPr = factory.createTcPr();  
  157.                 tc.setTcPr(tcPr);  
  158.                 // <w:tcW w:w="4788" w:type="dxa"/>  
  159.                 TblWidth cellWidth = factory.createTblWidth();  
  160.                 tcPr.setTcW(cellWidth);  
  161.                 cellWidth.setType("dxa");  
  162.                 cellWidth.setW(BigInteger.valueOf(widths[i - 1]));  
  163.                 tc.getContent().add(factory.createP());  
  164.             }  
  165.   
  166.         }  
  167.         return tbl;  
  168.     }  
  169.       
  170.     /** 
  171.      * 功能描述:创建文档表格,全部表格边框都为1 
  172.      * @param rows    行数 
  173.      * @param cols    列数 
  174.      * @param widths  每列的宽度 
  175.      * @return   返回值:返回表格对象 
  176.      * @author myclover 
  177.      */  
  178.     public static Tbl createBorderTable(int rows, int cols, int[] widths) {  
  179.         ObjectFactory factory = Context.getWmlObjectFactory();  
  180.         Tbl tbl = factory.createTbl();  
  181.         // w:tblPr  
  182.         StringBuffer tblSb = new StringBuffer();  
  183.         tblSb.append("<w:tblPr ").append(Namespaces.W_NAMESPACE_DECLARATION).append(">");  
  184.         tblSb.append("<w:tblStyle w:val=\"TableGrid\"/>");  
  185.         tblSb.append("<w:tblW w:w=\"0\" w:type=\"auto\"/>");  
  186.         tblSb.append("<w:tblBorders><w:top w:val=\"single\" w:sz=\"4\" w:space=\"0\" w:color=\"auto\"/>");  
  187.         tblSb.append("<w:left w:val=\"single\" w:sz=\"0\" w:space=\"0\" w:color=\"auto\"/>");  
  188.         tblSb.append("<w:bottom w:val=\"single\" w:sz=\"4\" w:space=\"0\" w:color=\"auto\"/>");  
  189.         tblSb.append("<w:right w:val=\"single\" w:sz=\"0\" w:space=\"0\" w:color=\"auto\"/>");  
  190.         tblSb.append("</w:tblBorders>");  
  191.         tblSb.append("<w:tblLook w:val=\"04A0\"/>");  
  192.         tblSb.append("</w:tblPr>");  
  193.         TblPr tblPr = null;  
  194.         try {  
  195.             tblPr = (TblPr) XmlUtils.unmarshalString(tblSb.toString());  
  196.         } catch (JAXBException e) {  
  197.             e.printStackTrace();  
  198.         }  
  199.         tbl.setTblPr(tblPr);  
  200.         if (tblPr != null) {  
  201.             Jc jc = factory.createJc();  
  202.             //单元格居中对齐  
  203.             jc.setVal(JcEnumeration.CENTER);  
  204.             tblPr.setJc(jc);  
  205.             CTTblLayoutType tbll = factory.createCTTblLayoutType();  
  206.             // 固定列宽  
  207.             tbll.setType(STTblLayoutType.FIXED);  
  208.             tblPr.setTblLayout(tbll);  
  209.         }  
  210.         // <w:tblGrid><w:gridCol w:w="4788"/>  
  211.         TblGrid tblGrid = factory.createTblGrid();  
  212.         tbl.setTblGrid(tblGrid);  
  213.         // Add required <w:gridCol w:w="4788"/>  
  214.         for (int i = 1; i <= cols; i++) {  
  215.             TblGridCol gridCol = factory.createTblGridCol();  
  216.             gridCol.setW(BigInteger.valueOf(widths[i - 1]));  
  217.             tblGrid.getGridCol().add(gridCol);  
  218.         }  
  219.         // Now the rows  
  220.         for (int j = 1; j <= rows; j++) {  
  221.             Tr tr = factory.createTr();  
  222.             tbl.getContent().add(tr);  
  223.             // The cells  
  224.             for (int i = 1; i <= cols; i++) {  
  225.                 Tc tc = factory.createTc();  
  226.                 tr.getContent().add(tc);  
  227.                 TcPr tcPr = factory.createTcPr();  
  228.                 tc.setTcPr(tcPr);  
  229.                 // <w:tcW w:w="4788" w:type="dxa"/>  
  230.                 TblWidth cellWidth = factory.createTblWidth();  
  231.                 tcPr.setTcW(cellWidth);  
  232.                 cellWidth.setType("dxa");  
  233.                 cellWidth.setW(BigInteger.valueOf(widths[i - 1]));  
  234.                 tc.getContent().add(factory.createP());  
  235.             }  
  236.               
  237.         }  
  238.         return tbl;  
  239.     }  
  240.       
  241.     /** 
  242.      * 功能描述:创建文档表格,上下双边框,左右不封口 
  243.      * @param wordPackage  文档处理包对象 
  244.      * @param rows         表格行数 
  245.      * @param cols         表格列数 
  246.      * @param tableWidth   表格的宽度 
  247.      * @param style        表格的样式 
  248.      * @param jcEnumerationVal 表格的对齐方式 
  249.      * @return             返回值:返回表格对象 
  250.      * @throws Exception 
  251.      * @author myclover 
  252.      */  
  253.     private static Tbl createTable(WordprocessingMLPackage wordPackage , int rows , int cols , String tableWidth , String style , JcEnumeration jcEnumerationVal)throws Exception{  
  254.         int writableWidthTwips = getWritableWidth(wordPackage);  
  255.         if(cols == 0){  
  256.             cols = 1;  
  257.         }  
  258.         int cellWidth = new Double(Math.floor( (writableWidthTwips/cols ))).intValue();  
  259.         int[] widths = new int[cols];  
  260.         for(int i = 0 ; i < cols ; i++){  
  261.             widths[i] = cellWidth;  
  262.         }  
  263.         return createTable(rows, cols, widths);  
  264.     }  
  265.       
  266.     /** 
  267.      * 功能描述:创建文档表格,表格的默认宽度为:表格样式:dxa,对齐方式:居中 
  268.      * @param wordPackage 文档处理包对象 
  269.      * @param rows        表格行数 
  270.      * @param cols        表格列数 
  271.      * @param tableWidth  表格宽度 
  272.      * @return            返回值:返回表格对象 
  273.      * @throws Exception 
  274.      * @author myclover 
  275.      */  
  276.     public static Tbl createTable(WordprocessingMLPackage wordPackage , int rows , int cols , String tableWidth) throws Exception{  
  277.         return createTable(wordPackage, rows, cols, tableWidth , "dxa", JcEnumeration.CENTER);  
  278.     }  
  279.       
  280.     /** 
  281.      * 功能描述:创建文档表格,表格的默认宽度为:9328,表格样式:dxa,对齐方式:居中 
  282.      * @param wordPackage 文档处理包对象 
  283.      * @param rows        表格行数 
  284.      * @param cols        表格列数 
  285.      * @return            返回值:返回表格对象 
  286.      * @throws Exception 
  287.      * @author myclover 
  288.      */  
  289.     public static Tbl createTable(WordprocessingMLPackage wordPackage , int rows , int cols) throws Exception{  
  290.         return createTable(wordPackage, rows, cols, "9328""dxa", JcEnumeration.CENTER);  
  291.     }  
  292.       
  293.     /** 
  294.      * 功能描述:往文档对象中添加相应的内容 
  295.      * @param wordPackage  文档处理包对象 
  296.      * @param info         需要添加的信息 
  297.      * @param unmarshal    是否有样式,表格对象默认不用 
  298.      * @throws Exception 
  299.      * @author myclover 
  300.      */  
  301.     private static void addObject(WordprocessingMLPackage wordPackage , Object info , boolean unmarshal)throws Exception{  
  302.         if(unmarshal){  
  303.             wordPackage.getMainDocumentPart().addObject(org.docx4j.XmlUtils.unmarshalString(String.valueOf(info)));  
  304.         }else{  
  305.             wordPackage.getMainDocumentPart().addObject(info);  
  306.         }  
  307.     }  
  308.       
  309.     /** 
  310.      * 功能描述:往文档中添加内容 
  311.      * @param wordPackage  文档处理包对象 
  312.      * @param info         文档内容 
  313.      * @throws Exception 
  314.      * @author myclover 
  315.      */  
  316.     public static void addObject(WordprocessingMLPackage wordPackage , Object info)throws Exception{  
  317.         addObject(wordPackage, info, true);  
  318.     }  
  319.       
  320.     /** 
  321.      * 功能描述:往文档中添加表格 
  322.      * @param wordPackage  文档处理包对象 
  323.      * @param tbl          表格 
  324.      * @throws Exception 
  325.      * @author myclover 
  326.      */  
  327.     public static void addObjectForTbl(WordprocessingMLPackage wordPackage , Tbl tbl)throws Exception{  
  328.         addObject(wordPackage, tbl, false);  
  329.     }  
  330.       
  331.     /** 
  332.      * 功能描述:保存文档信息 
  333.      * @param wordPackage  文档处理包对象 
  334.      * @param fileName     完整的输出文件名称,包括路径 
  335.      * @throws Exception 
  336.      * @author myclover 
  337.      */  
  338.     public static void saveWordPackage(WordprocessingMLPackage wordPackage , String fileName)throws Exception{  
  339.         saveWordPackage(wordPackage, new File(fileName));  
  340.     }  
  341.       
  342.     /** 
  343.      * 功能描述:保存文档信息 
  344.      * @param wordPackage  文档处理包对象 
  345.      * @param file         文件 
  346.      * @throws Exception 
  347.      * @author myclover 
  348.      */  
  349.     public static void saveWordPackage(WordprocessingMLPackage wordPackage , File file)throws Exception{  
  350.         wordPackage.save(file);  
  351.     }  
  352.       
  353.     /** 
  354.      * 功能描述:设置页边距 
  355.      * @param wordPackage 文档处理包对象 
  356.      * @param top    上边距 
  357.      * @param bottom 下边距 
  358.      * @param left   左边距 
  359.      * @param right  右边距 
  360.      * @author myclover 
  361.      */  
  362.     public static void setMarginSpace(WordprocessingMLPackage wordPackage , String top , String bottom , String left , String right ){  
  363.         ObjectFactory factory = Context.getWmlObjectFactory();  
  364.         PgMar pg = factory.createSectPrPgMar();  
  365.         pg.setTop(new BigInteger(top));  
  366.         pg.setBottom(new BigInteger(bottom));  
  367.         pg.setLeft(new BigInteger(left));  
  368.         pg.setRight(new BigInteger(right));  
  369.         wordPackage.getDocumentModel().getSections().get(0).getSectPr().setPgMar(pg);  
  370.     }  
  371.       
  372.     /** 
  373.      * 功能描述:设置页边距,上下边距都为1440,2.54厘米 
  374.      * @param wordPackage 文档处理包对象 
  375.      * @param left   左边距 
  376.      * @param right  右边距 
  377.      * @author myclover 
  378.      */  
  379.     public static void setMarginSpace(WordprocessingMLPackage wordPackage , String left , String right ){  
  380.         setMarginSpace(wordPackage, "1440""1440", left, right);  
  381.     }  
  382.       
  383.     /** 
  384.      * 功能描述:设置页边距,上下边距都为1440,2.54厘米,左右边距都为1797,3.17厘米 
  385.      * @param wordPackage 文档处理包对象 
  386.      * @author myclover 
  387.      */  
  388.     public static void setMarginSpace(WordprocessingMLPackage wordPackage){  
  389.         setMarginSpace(wordPackage, "1440""1440""1797""1797");  
  390.     }  
  391.       
  392.     /** 
  393.      * 功能描述:设置字体的样式 
  394.      * @param fontFamily      字体类型 
  395.      * @param colorVal        字体颜色 
  396.      * @param hpsMeasureSize  字号大小 
  397.      * @param sTHint          字体格式 
  398.      * @param isBlod          是否加粗 
  399.      * @return                返回值:返回字体样式对象 
  400.      * @throws Exception 
  401.      * @author myclover 
  402.      */  
  403.     private static RPr getRPr(String fontFamily , String colorVal , String hpsMeasureSize , STHint sTHint , boolean isBlod){  
  404.         ObjectFactory factory = Context.getWmlObjectFactory();  
  405.         RPr rPr = factory.createRPr();  
  406.           
  407.         org.docx4j.wml.RFonts rf = new org.docx4j.wml.RFonts();  
  408.         rf.setHint(sTHint);  
  409.         rf.setAscii(fontFamily);  
  410.         rf.setHAnsi(fontFamily);  
  411.         rPr.setRFonts(rf);  
  412.           
  413.         BooleanDefaultTrue bdt = Context.getWmlObjectFactory().createBooleanDefaultTrue();  
  414.         rPr.setBCs(bdt);  
  415.         if(isBlod){  
  416.             rPr.setB(bdt);  
  417.         }  
  418.         org.docx4j.wml.Color color = new org.docx4j.wml.Color();  
  419.         color.setVal(colorVal);  
  420.         rPr.setColor(color);  
  421.   
  422.         org.docx4j.wml.HpsMeasure sz = new org.docx4j.wml.HpsMeasure();  
  423.         sz.setVal(new BigInteger(hpsMeasureSize));  
  424.         rPr.setSz(sz);  
  425.         rPr.setSzCs(sz);  
  426.           
  427.         return rPr;  
  428.     }  
  429.       
  430.     /** 
  431.      * 功能描述:设置字体的样式,宋体,黑色,18号 
  432.      * @param isBlod          是否加粗 
  433.      * @return                返回值:返回字体样式对象 
  434.      * @throws Exception 
  435.      * @author myclover 
  436.      */  
  437.     private static RPr getRPr(boolean isBlod){  
  438.         return getRPr("宋体""000000""18", STHint.EAST_ASIA, isBlod);  
  439.     }  
  440.       
  441.     /** 
  442.      * 功能描述:设置字体的样式,黑色,18号 
  443.      * @param fontFamily      字体 
  444.      * @param isBlod          是否加粗 
  445.      * @return                返回值:返回字体样式对象 
  446.      * @throws Exception 
  447.      * @author myclover 
  448.      */  
  449.     private static RPr getRPr(String fontFamily , boolean isBlod){  
  450.         return getRPr(fontFamily, "000000""18", STHint.EAST_ASIA, isBlod);  
  451.     }  
  452.       
  453.     /** 
  454.      * 功能描述:设置字体的样式,黑色 
  455.      * @param fontFamily      字体 
  456.      * @param hpsMeasureSize  字号的大小 
  457.      * @param isBlod          是否加粗 
  458.      * @return                返回值:返回字体样式对象 
  459.      * @throws Exception 
  460.      * @author myclover 
  461.      */  
  462.     private static RPr getRPr(String fontFamily , String hpsMeasureSize , boolean isBlod){  
  463.         return getRPr(fontFamily, "000000", hpsMeasureSize, STHint.EAST_ASIA, isBlod);  
  464.     }  
  465.       
  466.     /** 
  467.      * 功能描述:获取单元格边框样式 
  468.      * @param type    单元格类型,0表示无边框,2表示双线边框,其他表示单线边框 
  469.      * @param color   边框颜色 
  470.      * @param border  边框大小 
  471.      * @param space   间距 
  472.      * @return        返回值:返回边框对象 
  473.      * @author myclover 
  474.      */  
  475.     private static CTBorder getCTBorder(int type , String color , String border , String space){  
  476.         CTBorder ctb = new CTBorder();  
  477.         if(type == 0){  
  478.             ctb.setVal(STBorder.NIL);  
  479.         }else{  
  480.             ctb.setColor(color);  
  481.             ctb.setSz(new BigInteger(border));  
  482.             ctb.setSpace(new BigInteger(space));  
  483.             if(type == 2){  
  484.                 ctb.setVal(STBorder.DOUBLE);  
  485.             }else{  
  486.                 ctb.setVal(STBorder.SINGLE);  
  487.             }  
  488.         }  
  489.         return ctb;  
  490.     }  
  491.       
  492.     /** 
  493.      * 功能描述:设置单元格内容对齐方式 
  494.      * @param p  内容 
  495.      * @param jcEnumeration  对齐方式 
  496.      * @author myclover 
  497.      */  
  498.     public static void setCellContentStyle(P p , JcEnumeration jcEnumeration){  
  499.         PPr pPr = p.getPPr();  
  500.         if(pPr == null){  
  501.             ObjectFactory factory = Context.getWmlObjectFactory();  
  502.             pPr = factory.createPPr();  
  503.         }  
  504.         org.docx4j.wml.Jc jc = pPr.getJc();  
  505.         if(jc == null){  
  506.             jc = new org.docx4j.wml.Jc();  
  507.         }  
  508.         jc.setVal(jcEnumeration);  
  509.         pPr.setJc(jc);  
  510.         p.setPPr(pPr);  
  511.     }  
  512.       
  513.     /** 
  514.      * 功能描述:设置单元格内容对齐方式,居中对齐 
  515.      * @param p  内容 
  516.      * @author myclover 
  517.      */  
  518.     public static void setCellContentStyle(P p){  
  519.         setCellContentStyle(p, JcEnumeration.CENTER);  
  520.     }  
  521.       
  522.       
  523.     /** 
  524.      * 功能描述:填充表格内容 
  525.      * @param wordPackage  文档处理包对象 
  526.      * @param tbl          表格对象 
  527.      * @param dataList     表格数据 
  528.      * @param titleList    表头数据 
  529.      * @param isFixedTitle 是否固定表头 
  530.      * @param tFontFamily  表头字体 
  531.      * @param tFontSize    表头字体大小 
  532.      * @param tIsBlod      表头是否加粗 
  533.      * @param tJcEnumeration 表头对齐方式 
  534.      * @param fontFamily   表格字体 
  535.      * @param fontSize     表格字号 
  536.      * @param isBlod       表格内容是否加粗 
  537.      * @param jcEnumeration 表格对齐方式 
  538.      * @author myclover 
  539.      */  
  540.     private static void fillTableData(WordprocessingMLPackage wordPackage , Tbl tbl , List dataList , List titleList , boolean isFixedTitle,String tFontFamily , String tFontSize , boolean tIsBlod , JcEnumeration tJcEnumeration,String fontFamily , String fontSize , boolean isBlod , JcEnumeration jcEnumeration){  
  541.         List rowList = tbl.getContent();  
  542.         //整个表格的行数  
  543.         int rows = rowList.size();  
  544.         int tSize = 0;  
  545.         //表头  
  546.         if(titleList != null && titleList.size() > 0){  
  547.             tSize = titleList.size();  
  548.             for(int t = 0 ; t < tSize ; t++){  
  549.                 Object[] tobj = (Object[]) titleList.get(t);  
  550.                 Tr tr0 = (Tr) XmlUtils.unwrap(rowList.get(t));  
  551.                 List colList = tr0.getContent();  
  552.                 for(int c = 0 ; c < colList.size() ; c++){  
  553.                     Tc tc0 = (Tc) XmlUtils.unwrap(colList.get(c));  
  554.                     //填充表头数据  
  555.                     fillCellData(tc0, converObjToStr(tobj[c], ""), tFontFamily, tFontSize, tIsBlod, tJcEnumeration);  
  556.                 }  
  557.                 if(isFixedTitle){  
  558.                     //设置固定表头  
  559.                     fixedTitle(tr0);  
  560.                 }  
  561.             }  
  562.         }  
  563.         int colsSize = 1;  
  564.         //表格数据,去掉表头之后的表格数据  
  565.         for(int r = tSize ; r < rows ; r++){  
  566.             Tr tr = (Tr) XmlUtils.unwrap(rowList.get(r));  
  567.             Object[] objs = null;  
  568.             //如果表格内容不为空,则取出相应的数据进行填充  
  569.             if(dataList != null && (dataList.size() >= (rows - tSize))){  
  570.                 objs = (Object[]) dataList.get(r - tSize);  
  571.             }  
  572.             List colsList = tr.getContent();  
  573.             //整个表格的列数  
  574.             colsSize = colsList.size();  
  575.             for(int i = 0 ; i < colsSize ; i++){  
  576.                 Tc tc = (Tc) XmlUtils.unwrap(colsList.get(i));  
  577.                 //填充表格数据  
  578.                 if(objs != null){  
  579.                     fillCellData(tc, converObjToStr(objs[i], ""), fontFamily, fontSize, isBlod, jcEnumeration);  
  580.                 }else{  
  581.                     fillCellData(tc, "", fontFamily, fontSize, isBlod, jcEnumeration);  
  582.                 }  
  583.             }  
  584.         }  
  585.     }  
  586.       
  587.     /** 
  588.      * 功能描述:填充表格内容,固定表头,表头宋体加粗,小五号,表格内容宋体,小五号,表格居中对齐</BR> 
  589.      *          其中表格数据跟表头数据结构要一致,适用于简单的n行m列的普通表格 
  590.      * @param wordPackage  文档处理包对象 
  591.      * @param tbl          表格对象 
  592.      * @param dataList     表格数据 
  593.      * @param titleList    表头数据,如果不需要表头信息,则只要传入null即可 
  594.      * @author myclover 
  595.      */  
  596.     public static void fillTableData(WordprocessingMLPackage wordPackage , Tbl tbl , List dataList , List titleList){  
  597.         fillTableData(wordPackage, tbl, dataList, titleList, true"宋体""18"true, JcEnumeration.CENTER, "宋体""18"false, JcEnumeration.CENTER);  
  598.     }  
  599.       
  600.     /** 
  601.      * 功能描述:固定表头 
  602.      * @param tr 行对象 
  603.      * @author myclover 
  604.      */  
  605.     public static void fixedTitle(Tr tr){  
  606.         ObjectFactory factory = Context.getWmlObjectFactory();  
  607.         BooleanDefaultTrue bdt = factory.createBooleanDefaultTrue();  
  608.         //表示固定表头  
  609.         bdt.setVal(true);  
  610.         TrPr trpr = tr.getTrPr();  
  611.         if(trpr == null){  
  612.             trpr = factory.createTrPr();  
  613.         }  
  614.         trpr.getCnfStyleOrDivIdOrGridBefore().add(factory.createCTTrPrBaseTblHeader(bdt));  
  615.         tr.setTrPr(trpr);  
  616.     }  
  617.       
  618.     /** 
  619.      * 功能描述:填充单元格内容 
  620.      * @param tc          单元格对象 
  621.      * @param data        内容 
  622.      * @param fontFamily  字体 
  623.      * @param fontSize    字号 
  624.      * @param isBlod      是否加粗 
  625.      * @param jcEnumeration 对齐方式 
  626.      * @author myclover 
  627.      */  
  628.     private static void fillCellData(Tc tc , String data , String fontFamily , String fontSize , boolean isBlod , JcEnumeration jcEnumeration){  
  629.         ObjectFactory factory = Context.getWmlObjectFactory();  
  630.         org.docx4j.wml.P p = (P) XmlUtils.unwrap(tc.getContent().get(0));  
  631.         //设置表格内容的对齐方式  
  632.         setCellContentStyle(p , jcEnumeration);  
  633.         org.docx4j.wml.Text t = factory.createText();  
  634.         t.setValue(data);  
  635.         org.docx4j.wml.R run = factory.createR();  
  636.         //设置表格内容字体样式  
  637.         run.setRPr(getRPr(fontFamily, fontSize, isBlod));  
  638.         TcPr tcpr = tc.getTcPr();  
  639.         if(tcpr == null){  
  640.             tcpr = factory.createTcPr();  
  641.         }  
  642.         //设置内容垂直居中  
  643.         CTVerticalJc valign = factory.createCTVerticalJc();  
  644.         valign.setVal(STVerticalJc.CENTER);  
  645.         tcpr.setVAlign(valign);  
  646.         run.getContent().add(t);  
  647.         p.getContent().add(run);  
  648.     }  
  649.       
  650.     /** 
  651.      * 功能描述:填充单元格内容,小五号,宋体,内容居中 
  652.      * @param tc     单元格对象 
  653.      * @param data   数据 
  654.      * @param isBlod 是否加粗 
  655.      * @author myclover 
  656.      */  
  657.     public static void fillCellData(Tc tc , String data , boolean isBlod ){  
  658.         fillCellData(tc, data, "宋体""18", isBlod, JcEnumeration.CENTER);  
  659.     }  
  660.       
  661.       
  662.     /** 
  663.      * 功能描述:获取文字信息 
  664.      * @param content  文字信息 
  665.      * @return  返回值:返回值指定样式的信息 
  666.      * @author myclover 
  667.      */  
  668.     public static String getContentInfo(String content){  
  669.         StringBuffer sb = new StringBuffer();  
  670.         sb.append("<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" >");  
  671.         sb.append("<w:pPr><w:spacing w:line=\"360\" w:lineRule=\"auto\"/><w:ind w:firstLineChars=\"200\" w:firstLine=\"480\"/>");  
  672.         sb.append("<w:rPr><w:rFonts w:asciiTheme=\"minorEastAsia\" w:hAnsiTheme=\"minorEastAsia\"/>");  
  673.         sb.append("<w:sz w:val=\"24\"/><w:szCs w:val=\"24\"/></w:rPr></w:pPr><w:r w:rsidRPr=\"00792470\"><w:rPr>");  
  674.         sb.append("<w:rFonts w:asciiTheme=\"minorEastAsia\" w:hAnsiTheme=\"minorEastAsia\" w:hint=\"eastAsia\"/>");  
  675.         sb.append("<w:sz w:val=\"24\"/><w:szCs w:val=\"24\"/></w:rPr><w:t>");  
  676.         sb.append(content);  
  677.         sb.append("</w:t></w:r></w:p>");  
  678.         return sb.toString();  
  679.     }  
  680.       
  681.     /** 
  682.      * 功能描述:合并单元格</BR> 
  683.      *          表示合并第startRow(开始行)行中的第startCol(开始列)列到(startCol + colSpan - 1)列 </BR> 
  684.      *          表示合并第startCol(开始列)行中的第startRow(开始行)列到(startRow + rowSpan - 1)行 
  685.      * @param tc          单元格对象 
  686.      * @param currentRow  当前行号,传入的是遍历表格时的行索引参数 
  687.      * @param startRow    开始行 
  688.      * @param rowSpan     合并的行数,大于1才表示合并 
  689.      * @param currentCol  当前列号,传入的是遍历表格时的列索引参数 
  690.      * @param startCol    开始列 
  691.      * @param colSpan     合并的列数,大于1才表示合并 
  692.      * @author myclover 
  693.      */  
  694.     public static void setCellMerge(Tc tc , int currentRow , int startRow , int rowSpan , int currentCol , int startCol , int colSpan){  
  695.         ObjectFactory factory = Context.getWmlObjectFactory();  
  696.         TcPr tcpr = tc.getTcPr();  
  697.         if(tcpr == null){  
  698.             tcpr = factory.createTcPr();  
  699.         }  
  700.         //表示合并列  
  701.         if(colSpan > 1){  
  702.             //表示从第startRow行开始  
  703.             if(currentRow == startRow){  
  704.                 //表示从第startRow行的第startCol列开始合并,合并到第startCol + colSpan - 1列  
  705.                 if(currentCol == startCol){  
  706.                     HMerge hm = factory.createTcPrInnerHMerge();  
  707.                     hm.setVal("restart");  
  708.                     tcpr.setHMerge(hm);  
  709.                     tc.setTcPr(tcpr);  
  710.                 }else if(currentCol > startCol && currentCol <= (startCol + colSpan - 1)){  
  711.                     HMerge hm = factory.createTcPrInnerHMerge();  
  712.                     tcpr.setHMerge(hm);  
  713.                     tc.setTcPr(tcpr);  
  714.                 }  
  715.             }  
  716.         }  
  717.         //表示合并行  
  718.         if(rowSpan > 1){  
  719.             //表示从第startCol列开始  
  720.             if(currentCol == startCol){  
  721.                 //表示从第startCol列的第startRow行始合并,合并到第startRow + rowSpan - 1行  
  722.                 if(currentRow == startRow){  
  723.                     VMerge vm = factory.createTcPrInnerVMerge();  
  724.                     vm.setVal("restart");  
  725.                     tcpr.setVMerge(vm);  
  726.                     tc.setTcPr(tcpr);  
  727.                 }else if(currentRow > startRow && currentRow <= (startRow + rowSpan - 1)){  
  728.                     VMerge vm = factory.createTcPrInnerVMerge();  
  729.                     tcpr.setVMerge(vm);  
  730.                     tc.setTcPr(tcpr);  
  731.                 }  
  732.             }  
  733.         }  
  734.           
  735.     }  
  736.       
  737.     /** 
  738.      * 功能描述:合并单元格,相当于跨列的效果</BR> 
  739.      *          表示合并第startRow(开始行)行中的第startCol(开始列)列到(startCol + colSpan - 1)列 </BR> 
  740.      * @param tc          单元格对象 
  741.      * @param currentRow  当前行号,传入的是遍历表格时的行索引参数 
  742.      * @param startRow    开始行 
  743.      * @param currentCol  当前列号,传入的是遍历表格时的列索引参数 
  744.      * @param startCol    开始列 
  745.      * @param colSpan     合并的列数,大于1才表示合并 
  746.      * @author myclover 
  747.      */  
  748.     public static void setCellHMerge(Tc tc , int currentRow , int startRow , int currentCol , int startCol , int colSpan){  
  749.         setCellMerge(tc, currentRow, startRow, 1, currentCol, startCol, colSpan);  
  750.     }  
  751.       
  752.     /** 
  753.      * 功能描述:合并单元格,相当于跨行的效果</BR> 
  754.      *          表示合并第startCol(开始列)行中的第startRow(开始行)列到(startRow + rowSpan - 1)行 
  755.      * @param tc          单元格对象 
  756.      * @param currentRow  当前行号,传入的是遍历表格时的行索引参数 
  757.      * @param startRow    开始行 
  758.      * @param rowSpan     合并的行数,大于1才表示合并 
  759.      * @param currentCol  当前列号,传入的是遍历表格时的列索引参数 
  760.      * @param startCol    开始列 
  761.      * @author myclover 
  762.      */  
  763.     public static void setCellVMerage(Tc tc , int currentRow , int startRow , int rowSpan , int currentCol , int startCol){  
  764.         setCellMerge(tc, currentRow, startRow, rowSpan, currentCol, startCol, 1);  
  765.     }  
  766.       
  767.     /** 
  768.      * 功能描述:设置文档是否只读,包括内容和样式 
  769.      * @param wordPackage  文档处理包对象 
  770.      * @param isReadOnly   是否只读 
  771.      * @throws Exception 
  772.      * @author myclover 
  773.      */  
  774.     public static void setReadOnly(WordprocessingMLPackage wordPackage , boolean isReadOnly)throws Exception{  
  775.         byte[] bt = "".getBytes();  
  776.         if(isReadOnly){  
  777.             bt = "123456".getBytes();  
  778.         }  
  779.         ObjectFactory factory = Context.getWmlObjectFactory();  
  780.         //创建设置文档对象  
  781.         DocumentSettingsPart ds = wordPackage.getMainDocumentPart().getDocumentSettingsPart();  
  782.         if(ds == null){  
  783.             ds = new DocumentSettingsPart();  
  784.         }  
  785.         CTSettings cs = ds.getJaxbElement();  
  786.         if(cs == null){  
  787.             cs = factory.createCTSettings();  
  788.         }  
  789.         //创建文档保护对象  
  790.         CTDocProtect cp = cs.getDocumentProtection();  
  791.         if(cp == null){  
  792.             cp = new CTDocProtect();  
  793.         }  
  794.         //设置加密方式  
  795.         cp.setCryptProviderType(STCryptProv.RSA_AES);  
  796.         cp.setCryptAlgorithmClass(STAlgClass.HASH);  
  797.         //设置任何用户  
  798.         cp.setCryptAlgorithmType(STAlgType.TYPE_ANY);  
  799.         cp.setCryptAlgorithmSid(new BigInteger("4"));  
  800.         cp.setCryptSpinCount(new BigInteger("50000"));  
  801.         //只读  
  802.         if(isReadOnly){  
  803.             cp.setEdit(STDocProtect.READ_ONLY);  
  804.             cp.setHash(bt);  
  805.             cp.setSalt(bt);  
  806.             //设置内容不可编辑  
  807.             cp.setEnforcement(true);  
  808.             //设置格式不可编辑  
  809.             cp.setFormatting(true);  
  810.         }else{  
  811.             cp.setEdit(STDocProtect.NONE);  
  812.             cp.setHash(null);  
  813.             cp.setSalt(null);  
  814.             //设置内容不可编辑  
  815.             cp.setEnforcement(false);  
  816.             //设置格式不可编辑  
  817.             cp.setFormatting(false);  
  818.         }  
  819.           
  820.         cs.setDocumentProtection(cp);  
  821.         ds.setJaxbElement(cs);  
  822.         //添加到文档主体中  
  823.         wordPackage.getMainDocumentPart().addTargetPart(ds);  
  824.     }  
  825.       
  826.     /** 
  827.      * 功能描述:设置文档是否只读,包括内容和样式 
  828.      * @param fileName    文件 
  829.      * @param isReadOnly  是否只读 
  830.      * @return            返回值:设置成功,则返回true,否则返回false 
  831.      * @throws Exception 
  832.      * @author myclover 
  833.      */  
  834.     public static boolean setReadOnly(String fileName , boolean isReadOnly)throws Exception{  
  835.         try {  
  836.             File file = new File(fileName);  
  837.             if (!file.exists()) {  
  838.                 return false;  
  839.             }  
  840.             //加载需要设置只读的文件  
  841.             WordprocessingMLPackage wordPackage = WordprocessingMLPackage.load(file);  
  842.             //设置只读  
  843.             setReadOnly(wordPackage, isReadOnly);  
  844.             //保存文件  
  845.             saveWordPackage(wordPackage, file);  
  846.         } catch (Exception e) {  
  847.             e.printStackTrace();  
  848.             return false;  
  849.         }  
  850.         return true;  
  851.     }  
  852.       
  853.     /** 
  854.      * 功能描述:插入页脚条形码 
  855.      * @param wordPackage 文档处理包对象 
  856.      * @param code        条形码编码 
  857.      * @author myclover 
  858.      */  
  859.     public static void insertFooterImage(WordprocessingMLPackage wordPackage , String code)throws Exception {  
  860.         try {  
  861.             createFooterReference(wordPackage, createFooterPart(wordPackage, code));  
  862.         } catch (Exception e) {  
  863.             e.printStackTrace();  
  864.             throw new Exception("insertFooterImage 插入页脚的条形码失败:" , e);  
  865.         }  
  866.     }  
  867.       
  868.     /** 
  869.      * 功能描述:创建页脚信息  
  870.      * @param wordPackage  文档处理包对象 
  871.      * @param relationship 关联对象 
  872.      * @throws Exception 
  873.      * @author myclover 
  874.      */  
  875.     private static void createFooterReference(WordprocessingMLPackage wordPackage,Relationship relationship )throws Exception {  
  876.         org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();  
  877.         List<SectionWrapper> sections = wordPackage.getDocumentModel().getSections();  
  878.         SectPr sectPr = sections.get(sections.size() - 1).getSectPr();  
  879.         if (sectPr == null ) {  
  880.             sectPr = factory.createSectPr();  
  881.             wordPackage.getMainDocumentPart().addObject(sectPr);  
  882.             sections.get(sections.size() - 1).setSectPr(sectPr);  
  883.         }  
  884.         FooterReference headerReference = factory.createFooterReference();  
  885.         headerReference.setId(relationship.getId());  
  886.         headerReference.setType(HdrFtrRef.DEFAULT);  
  887.         sectPr.getEGHdrFtrReferences().add(headerReference);  
  888.     }  
  889.       
  890.     /** 
  891.      * 功能描述:创建页脚部分 
  892.      * @param wordPackage  文档处理包对象 
  893.      * @param code         条形码编码 
  894.      * @return 
  895.      * @throws Exception 
  896.      * @author myclover 
  897.      */  
  898.     private static Relationship createFooterPart(WordprocessingMLPackage wordPackage , String code)throws Exception {  
  899.         FooterPart footerPart = new FooterPart();  
  900.         footerPart.setPackage(wordPackage);  
  901.         footerPart.setJaxbElement(getFtr(wordPackage , footerPart , code));  
  902.         return wordPackage.getMainDocumentPart().addTargetPart(footerPart);  
  903.     }  
  904.       
  905.     /** 
  906.      * 功能描述:获取页脚内容 
  907.      * @param wordPackage  文档处理包对象 
  908.      * @param sourcePart   源部分 
  909.      * @param code         条形码编码 
  910.      * @return 
  911.      * @throws Exception 
  912.      * @author myclover 
  913.      */  
  914.     private static Ftr getFtr(WordprocessingMLPackage wordPackage,Part sourcePart , String code) throws Exception {  
  915.         org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();  
  916.         Ftr ftr = factory.createFtr();  
  917.         byte[] bt = generateCode39Barcode(code);  
  918.         ftr.getContent().add(getPImage(wordPackage,sourcePart, bt, "filename""alttext"12));  
  919.         return ftr;  
  920.     }  
  921.       
  922.     /** 
  923.      * 功能描述:创建页脚条形码图片信息 
  924.      * @param wordPackage  文档处理包对象 
  925.      * @param sourcePart   源部分 
  926.      * @param bytes        字节数组 
  927.      * @param filenameHint 图片名称 
  928.      * @param altText      提示信息 
  929.      * @param id1          id1 
  930.      * @param id2          id2 
  931.      * @return 
  932.      * @throws Exception 
  933.      * @author myclover 
  934.      */  
  935.     private static org.docx4j.wml.P getPImage( WordprocessingMLPackage wordPackage,Part sourcePart,byte[] bytes,  
  936.             String filenameHint, String altText,   
  937.             int id1, int id2) throws Exception {  
  938.         BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage,sourcePart, bytes);  
  939.         Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false);  
  940.         org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();  
  941.         org.docx4j.wml.P  p = factory.createP();  
  942.         org.docx4j.wml.R  run = factory.createR();        
  943.         p.getContent().add(run);          
  944.         org.docx4j.wml.Drawing drawing = factory.createDrawing();         
  945.         run.getContent().add(drawing);        
  946.         drawing.getAnchorOrInline().add(inline);  
  947.         PPr pPr = p.getPPr();  
  948.         if(pPr == null){  
  949.             pPr = factory.createPPr();  
  950.         }  
  951.         Jc jc = pPr.getJc();  
  952.         if(jc == null){  
  953.             jc = new org.docx4j.wml.Jc();  
  954.         }  
  955.         //页脚条形码所处位置  
  956.         jc.setVal(JcEnumeration.RIGHT);  
  957.         pPr.setJc(jc);  
  958.         p.setPPr(pPr);  
  959.         return p;  
  960.     }     
  961.       
  962.     /** 
  963.      * 功能描述:生成条形码 
  964.      * @param code 条形码编码 
  965.      * @return 
  966.      * @throws Exception 
  967.      * @author myclover 
  968.      */  
  969.     private static byte[] generateCode39Barcode(String code)throws Exception{  
  970.         Code39Bean bean = new Code39Bean();     
  971.         //每英寸所打印的点数或线数,用来表示打印机打印分辨率。     
  972.         final int dpi = 200;     
  973.         bean.setModuleWidth(0.15);     
  974.         bean.setHeight(10);     
  975.         bean.setWideFactor(3);     
  976.         bean.doQuietZone(true);     
  977.         ByteArrayOutputStream out = null;  
  978.         try {  
  979.             out = new ByteArrayOutputStream();  
  980.             BitmapCanvasProvider canvas = new BitmapCanvasProvider(dpi,BufferedImage.TYPE_BYTE_GRAY, true0);     
  981.             bean.generateBarcode(canvas, code);     
  982.             canvas.finish();     
  983.             BufferedImage barcodeImage = canvas.getBufferedImage();     
  984.             ImageIO.write(barcodeImage, "jpeg", out);    
  985.             out.flush();  
  986.             return out.toByteArray();  
  987.         } catch (Exception e) {  
  988.             e.printStackTrace();  
  989.         }finally{  
  990.             if(out != null){  
  991.                 try {  
  992.                     out.close();  
  993.                 } catch (Exception e) {  
  994.                     e.printStackTrace();  
  995.                 }  
  996.             }  
  997.         }  
  998.         return null;  
  999.     }  
  1000.       
  1001.     /** 
  1002.      * 功能描述:Object数据转换为String类型 
  1003.      * @param obj   
  1004.      * @param defaultStr 如果obj对象为空,则返回的值 
  1005.      * @return 
  1006.      * @author myclover 
  1007.      */  
  1008.     public static String converObjToStr(Object obj , String defaultStr){  
  1009.         if(obj != null){  
  1010.             return obj.toString();  
  1011.         }  
  1012.         return defaultStr;  
  1013.     }  
  1014.       
  1015. }  
分享到:   
评论
6 楼  iwwenbo 2015-07-10  
虽然看不懂,但觉得很牛叉,专门注册账号赞一个
5 楼  myclover 2013-03-18  
wilsonchen 写道
谢谢您的帮助^_^
填充完第一页继续填充第二页,或者使用文档合并功能也可以,这个要怎样实现呢?
如何将第一页的内容填充到第二页呢?合并文档?docx4j可以合并文档?有例子吗?谢谢。

填充完第一页数据之后,再调用我上面提到那个强行分页的方法,然后再次填充第一页填充的数据就行了啊
4 楼  wilsonchen 2013-03-17  
谢谢您的帮助^_^
填充完第一页继续填充第二页,或者使用文档合并功能也可以,这个要怎样实现呢?
如何将第一页的内容填充到第二页呢?合并文档?docx4j可以合并文档?有例子吗?谢谢。
3 楼  myclover 2013-03-16  
wilsonchen 写道
楼主,请教您一个问题:
如何利用docx4j copy第一页的数据,然后动态生成第二页,把第一页 的数据copy到第二页中呢?谢谢。

直接使用copy这个我还没用过,不知道api里面有没有提供直接copy的功能,但是你可以研究一下,我觉得要是第一页的内容你都知道了,不防直接循环生成两页,填充完第一页继续填充第二页,或者使用文档合并功能也可以。

强行分页功能:

Java代码   收藏代码
  1. /** 
  2.      * 功能描述:强行分页功能,调用了此方法会进行强行分页,后面添加的文档内容将会在新页中显示 
  3.      * @param wordPackage  文档处理包对象 
  4.      * @throws Exception    
  5.      * @author myclover 
  6.      */  
  7.     public static void newPage(WordprocessingMLPackage wordPackage)throws Exception{  
  8.         StringBuffer sb = new StringBuffer();  
  9.         sb.append("<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" >");  
  10.         sb.append("<w:pPr/><w:r><w:rPr/><w:br w:type=\"page\"/></w:r></w:p>");  
  11.         addObject(wordPackage, sb.toString(), true);  
  12.     }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值