web项目java后台将html表格导出为word表格文件

web环境;spring+jQuery+hibernate 

前端jsp文件核心代码:

 <div class="col-sm-10 col-xs-10 col-md-8 col-md-offset-2 text-right" style="padding: 10px 0;">
     <input type="hidden" name="attid" id="attid" value="${attid }" />
     <div id="tip4"></div>
     <a id="dwywjs" class="andown worddd">下载word版</a>
</div>
<script>
$("#dwywjs").click(function(){
       var attid="${映射表文件名.attid}";//对应数据库里该表名里的某一行具体的数据
       var userid="${userSession.user.id}";//当前登录用户的id
  var param = {
               'userid': userid,
           'attid' : attid,
       'subtype' :'dwcxqd'   //后台选用不同样式下载的标志
           };             
       $.ajax({
               url:'<c:url value="/embed/ajax/updatePass.htm"/>',
               data : param,
  type : 'get',
  offline : false,
               success:function(data){                         
                  if(data.code==2){
                       $("#tip4").show().html("<font color=\"green\" size=\"3\">  Word 文件下载 成功!已下载至桌面</font>");
                       $("#tip4").delay(2000).hide(0);        
                     }else{
                       $("#tip4").show().html("<font color=\"red\" size=\"3\">Word 文件下载 失败!</font>");
                       }                                     
                  }
               });
       });
</script>

前后端交互配置文件:

nsl-servlet.xml里配置网页和后台交互的一一对应:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">


<!-- - Application context -->
<beans>
  <!-- 国际化 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/classes/messages" />
<property name="defaultEncoding" value="UTF-8"></property>
<property name="fallbackToSystemLocale" value="false"/>
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
</bean>
   <!-- 定义本地化变更拦截器 -->
<bean id="localeChangeInterceptor"
  class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />


        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
          <property name="interceptors" >
<list>
                    <ref local="localeChangeInterceptor"/>
        </list>
   </property>
          <property name="mappings">
            <props>
             <prop key="/embed/ajax/updatePass.htm">
       updatepassController
             </prop>
            </props>
         </property>
        </bean>




        <bean id="updatepassController"
class="cn.nsl.web.embed.ajax.UpdatepassController">
<property name="dao" ref="commonDAOProxy" />
</bean>

</beans>


需要的jar包:ooxml-schemas-1.1.jar,poi-ooxml-3.15.jar,poi-3.15.jar,poi-examples-3.15.jar,poi-excelant-3.15.jar,poi-scratchpad-3.15.jar

后台ajax下载文件:

package cn.nsl.web.embed.ajax;


import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGridCol;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHeightRule;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;


import cn.nsl.domain.glob.*;
import cn.nsl.web.util.Md5Security;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jbetter.spring.web.JSONObjectAbstractController;


public class UpdatepassController  extends JSONObjectAbstractController {
@Override
protected String handleAjaxRequestInternal(
HttpServletRequest request,
HttpServletResponse response) throws Exception {
String userid = request.getParameter("userid");
String json ="{}";
        Gson gson = new GsonBuilder().create();  //创建json对象,目的将最后想要的数据传递到前台
        String subtype = request.getParameter("subtype");   //下载类型
        HashMap<String,String> data = new HashMap<String,String>();
        if("dwcxqd".equals(subtype)){
        String attid = request.getParameter("attid");
        XWPFDocument document= new XWPFDocument();
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File com=fsv.getHomeDirectory();  //获取桌面路径
        List<NovKjcx> nnlist=dao.find(" from NovKjcx obj where obj.attid='"+attid+"' and obj.userid="+userid);   //NovKjcx为通过hibernate写的与数据库表对应的java文件,包含表的字段以及字段类型及其get()和set()方法
                FileOutputStream outs = new FileOutputStream(new File(com+"/cxdd.doc"));   //在本地桌面生成word文件
        
            XWPFParagraph titleParagraph = document.createParagraph();      //添加标题  
            titleParagraph.setAlignment(ParagraphAlignment.CENTER);                //设置段落居中
      
            XWPFRun titleParagraphRun = titleParagraph.createRun();   //声明一个段落
            titleParagraphRun.setText("XXXX清单");    //设置段落内容,也可以从数据库读数据写入
            titleParagraphRun.setColor("000000");    //设置这一行字体颜色
            titleParagraphRun.setFontSize(20);  //设置这一行字体大小
      
      
            //两个表格之间加个换行  
            XWPFParagraph paragraph = document.createParagraph();  
            XWPFRun paragraphRun = paragraph.createRun();  
            paragraphRun.setText("\r");
            
            /***方式二***/
            XWPFTable ComTable = document.createTable(24,9);   //声明一个24行9列的表格
            
            setTableWidthAndHAlign(ComTable, "9024", STJc.CENTER);  //设置表的总体宽度
            setTableCellMargin(ComTable, 0, 20, 0, 20);  //这里相当于css里设置margin的值(上,左,下,右)
            //设置列宽
            int[] colWidths = new int[] { 4000, 2638, 525, 3692 ,2169,2169,2169,2169,2169};
            setTableGridCol(ComTable, colWidths);
            //读取数据库相关数据
            List<NovKjcx> list=dao.find(" from NovKjcx obj where obj.attid='"+attid+"' and obj.userid="+userid);
            Long ddid=null;
            String cce=null;
            if(list!=null&&list.size()>0){
            NovKjcx tep=list.get(0);
            ddid=tep.getId();
            cce=tep.getKjcxwt_code();


            UserLxr Lxr=tep.getUser_lxr();


            ComTable.getRow(0).getCell(0).setText("XXXX委托日期 *  "+tep.getKjcxwt_wtrq()); //(0.0)表格的坐标从(0,0)开始
            if(tep.getKjcxwt_yqrq()==null){
            ComTable.getRow(0).getCell(5).setText("XXXX期望完成日期 * "+tep.getKjcxwt_finishdate()); //(0.5)
              }else{
            ComTable.getRow(0).getCell(5).setText("XXXX期望完成日期 * "+tep.getKjcxwt_yqrq()); //(0.5) 
              }
            ComTable.getRow(1).getCell(0).setText("委托须知 *");  //(1.0)表格的第一行第0列
            ComTable.getRow(1).getCell(1).setText("1.委托人必须按要求认真填写并对所提供资料的真实性及可靠性负责。" +
            "查新委托内容经确认并被受理后,则不能随意更改。若委托人要求更改查新内容或增加查新点," +
            "则需重新办理查新委托,并按新项目收费,或双方协商酌情增加收费。 " +
            "2.项目查新实行先付费制度,委托人与查新机构确认查新内容并按收费标准办理缴款手续后,查新委托方被受理。" +
            "3.查新受理日以缴款或汇款凭证传真至本查新机构的日期为准。" +
            "4.若有其他情况,请电话咨询。"); //(1.1)
            ComTable.getRow(3).getCell(0).setText("项目名称 *"); //(3.0)
            ComTable.getRow(3).getCell(1).setText("中文     "+tep.getKjcxwt_cname());  //(3.1)
            ComTable.getRow(4).getCell(1).setText("英文   "+tep.getKjcxwt_ename()); //(4.1)
            ComTable.getRow(5).getCell(0).setText("委托人");  //(5.0)
            ComTable.getRow(5).getCell(1).setText("机构名称 *");  //(5.1)
            ComTable.getRow(5).getCell(2).setText(Lxr.getOrg_cname());  //(5.2)
            ComTable.getRow(6).getCell(1).setText("邮政编码 *");  //(6.1)
            ComTable.getRow(6).getCell(2).setText(Lxr.getOrg_postcode());  //(6.2)
            ComTable.getRow(6).getCell(3).setText("通信地址 *");  //(6.3)
            ComTable.getRow(6).getCell(4).setText(Lxr.getOrg_address());  //(6.4)
            ComTable.getRow(7).getCell(1).setText("项目负责人 *");  //(7.1)
            ComTable.getRow(7).getCell(2).setText(Lxr.getFzr_cname());  //(7.2)
            ComTable.getRow(7).getCell(3).setText("电话 *");  //(7.3)
            ComTable.getRow(7).getCell(4).setText(Lxr.getFzr_tel()); //(7.4)
            ComTable.getRow(7).getCell(5).setText("手机 *");  //(7.5)
            ComTable.getRow(7).getCell(6).setText(Lxr.getFzr_mobile());  //(7.6)
            ComTable.getRow(7).getCell(7).setText("EMAIL *");  //(7.7)
            ComTable.getRow(7).getCell(8).setText(Lxr.getFzr_email());  //(7.8)
            


            
            ComTable.getRow(8).getCell(1).setText("联系人 *");  //(8.1)
            ComTable.getRow(8).getCell(2).setText(Lxr.getLxr_cname());  //(8.2)
            ComTable.getRow(8).getCell(3).setText("电话 *");  //(8.3)
            ComTable.getRow(8).getCell(4).setText(Lxr.getLxr_phone());  //(8.4)
            ComTable.getRow(8).getCell(5).setText("手机 *");  //(8.5)
            ComTable.getRow(8).getCell(6).setText(Lxr.getLxr_mobile()); //(8.6) 
            ComTable.getRow(8).getCell(7).setText("EMAIL *");  //(8.7)
            ComTable.getRow(8).getCell(8).setText(Lxr.getLxr_email());  //(8.8)
            
            ComTable.getRow(9).getCell(0).setText("XX目的 *");  //(9.0)
            ComTable.getRow(9).getCell(1).setText(" XXXX:");  //(9.1)
            ComTable.getRow(10).getCell(1).setText("XXXX:");  //(10.1) 
            ComTable.getRow(11).getCell(1).setText("其他:");  //(11.1)
            List<NovKjcxLX> lxlist=dao.find(" from NovKjcxLX obj where obj.type=0 order by obj.id ");
            List<NovKjcxPurpose> plist=dao.find(" from NovKjcxPurpose obj where obj.novkjcx.id="+ddid);
            for(NovKjcxLX lxx:lxlist){
            String mdty=lxx.getCxmd_type();
            if("XXXX".equals(mdty)){
               for(NovKjcxPurpose pupose:plist){
              String pup=pupose.getPurpose();
              Long lid=lxx.getId();
              if((Long.valueOf(pup)).longValue()==lid.longValue()){
              ComTable.getRow(9).getCell(2).setText(lxx.getCxmd_cname());  //(9.2)
                }
                  }
                    }
           
            if("XXXX".equals(mdty)){
             for(NovKjcxPurpose pupose:plist){
            String pup=pupose.getPurpose();
            Long lid=lxx.getId();
            if((Long.valueOf(pup)).longValue()==lid.longValue()){
            ComTable.getRow(10).getCell(2).setText(lxx.getCxmd_cname());  //(10.2)
              }
                }
                  }
            if("其他".equals(mdty)){
                 for(NovKjcxPurpose pupose:plist){
                String pup=pupose.getPurpose();
                Long lid=lxx.getId();
                if((Long.valueOf(pup)).longValue()==lid.longValue()){
                ComTable.getRow(11).getCell(2).setText(lxx.getCxmd_cname());  //(11.2)
                  }
                    }
                      }
            }
            ComTable.getRow(12).getCell(0).setText("XX范围 *");  //(12.0)
            ComTable.getRow(12).getCell(1).setText(tep.getKjcxwt_cxfw());  //(12.1)
            ComTable.getRow(13).getCell(0).setText("XXXX的科学技术要点");  //(13.0)
            ComTable.getRow(13).getCell(1).setText(tep.getKjcxwt_cxyd());  //(13.1)
            ComTable.getRow(14).getCell(0).setText("XX点");  //(14.0)
            List<NovKjcxCXD> cxdlist=dao.find(" from NovKjcxCXD obj where obj.novkjcx.id="+ddid);
            if(cxdlist!=null&&cxdlist.size()>0){
            String cxds="";
            for(NovKjcxCXD CXD:cxdlist){
            cxds +=CXD.getKjyd();
            }
            ComTable.getRow(14).getCell(1).setText(cxds);  //(14.1)
            }
            ComTable.getRow(15).getCell(0).setText("参考");  //(15.0)
            List<NovKjcxKeyword> keywordlist=dao.find(" from NovKjcxKeyword obj where obj.novkjcx.id="+ddid);
            if(cxdlist!=null&&cxdlist.size()>0){
            String jscs="";
            for(NovKjcxKeyword JSC:keywordlist){
            jscs +=" 中文:"+JSC.getCkeyword();
            jscs +=" 英文:"+JSC.getEkeyword();
            }
            ComTable.getRow(15).getCell(1).setText(jscs);  //(15.1)
            }
            
            ComTable.getRow(16).getCell(0).setText("XX分类 *");  //(16.0)
            List<Subject> subList=dao.find(" from Subject obj where obj.subject_type='XX分类' and obj.del=0");
            if(subList!=null&&subList.size()>0){
            for(Subject ssb:subList){
            if((ssb.getId()).longValue()==(tep.getKjcx_subject_id().getId()).longValue()){
            ComTable.getRow(16).getCell(1).setText(ssb.getSubject_cname());  //(16.1)
            }
            }
            }
            
            ComTable.getRow(17).getCell(0).setText("XX分类 *");  //(17.0)
            List<Subject> industryList=dao.find(" from Subject obj where obj.subject_type='XX分类' and obj.del=0");
            if(industryList!=null&&industryList.size()>0){
            for(Subject ius:industryList){
            Long ii=ius.getId();
            Long tp=tep.getKjcx_chanye_id().getId();
            if(ii.longValue()==tp.longValue()){
            ComTable.getRow(17).getCell(1).setText(ius.getSubject_cname());  //(17.1)
            }
            }
            }
            ComTable.getRow(18).getCell(0).setText("希望完成日期");  //(18.0)
            if(tep.getKjcxwt_yqrq()==null){
            ComTable.getRow(18).getCell(1).setText(tep.getKjcxwt_finishdate()); //(18.1)
              }else{
             ComTable.getRow(18).getCell(1).setText(tep.getKjcxwt_yqrq()); //(18.1) 
              }
            ComTable.getRow(19).getCell(0).setText("需要红章份数");  //(19.0)
            String rednm =tep.getRednums().toString();
            ComTable.getRow(19).getCell(1).setText(rednm);  //(19.1)
            ComTable.getRow(20).getCell(0).setText("提交方式");  //(20.0)
            String sbtype=tep.getSubmit_type().toString();
            if("1".equals(sbtype)){
            ComTable.getRow(20).getCell(1).setText("EMS快递预付");  //(20.1)
            }else if("0".equals(sbtype)){
            ComTable.getRow(20).getCell(1).setText("自取");  //(20.1)
            }else if("2".equals(sbtype)){
            ComTable.getRow(20).getCell(1).setText("顺丰快递到付");  //(20.1)
            }
            ComTable.getRow(21).getCell(0).setText("指定查新员");  //(21.0)
            List<User> userList=dao.find(" from User obj where obj.type=2");
            if(userList!=null&&userList.size()>0){
            for(User ue:userList){
            if((tep.getUser_id_cxyuan().getId()).longValue()==(ue.getId()).longValue()){
            ComTable.getRow(21).getCell(1).setText(ue.getCname());  //(21.1)
            }
            }
            }
            ComTable.getRow(22).getCell(0).setText("备注");  //(22.0)
            ComTable.getRow(22).getCell(1).setText(tep.getKjcxwt_memo());  //(22.1)
            ComTable.getRow(23).getCell(0).setText("添加附件");  //(23.0)
            List<ReportInfo> rlist=dao.find(" from ReportInfo obj where obj.attach_code='"+cce+"' and obj.del=0");
            if(rlist!=null&&rlist.size()>0){
            ComTable.getRow(23).getCell(1).setText(rlist.get(0).getLocalurl());  //(23.1)
              }
            
            }
            ComTable.getRow(0).getCell(0).setColor("E1E6F6");  //(0.0)
            ComTable.getRow(0).getCell(5).setColor("E1E6F6");  //(0.5)
            
            
            //横向合并单元格
            mergeCellsHorizontal(ComTable,0,0,4);   // 合并第0行的从第0列开始到底4列为一格
            mergeCellsHorizontal(ComTable,0,5,8);   // 合并第0行的从第5列开始到底8列为一格
            mergeCellsHorizontal(ComTable,1,1,8);
            mergeCellsHorizontal(ComTable,2,1,8);
            mergeCellsHorizontal(ComTable,3,1,8);
            mergeCellsHorizontal(ComTable,4,1,8);
            mergeCellsHorizontal(ComTable,5,2,8);
            mergeCellsHorizontal(ComTable,6,4,8);
            mergeCellsHorizontal(ComTable,9,2,8);
            mergeCellsHorizontal(ComTable,10,2,8);
            mergeCellsHorizontal(ComTable,11,2,8);
            mergeCellsHorizontal(ComTable,12,1,8);
            mergeCellsHorizontal(ComTable,13,1,8);
            mergeCellsHorizontal(ComTable,14,1,8);
            mergeCellsHorizontal(ComTable,15,1,8);
            mergeCellsHorizontal(ComTable,16,1,8);
            mergeCellsHorizontal(ComTable,17,1,8);
            mergeCellsHorizontal(ComTable,18,1,8);
            mergeCellsHorizontal(ComTable,19,1,8);
            mergeCellsHorizontal(ComTable,20,1,8);
            mergeCellsHorizontal(ComTable,21,1,8);
            mergeCellsHorizontal(ComTable,22,1,8);
            mergeCellsHorizontal(ComTable,23,1,8);
            //纵向合并单元格
            for(int k=0;k<9;k++){//垂直
            mergeCellsVertically(ComTable,k,1,2);   //合并第k列的第一行和第二行
            }
            for(int k=0;k<1;k++){//垂直
            mergeCellsVertically(ComTable,k,3,4);
            }
            for(int k=0;k<1;k++){//垂直
            mergeCellsVertically(ComTable,k,5,8);
            }
            for(int k=0;k<1;k++){//垂直
            mergeCellsVertically(ComTable,k,9,11);
            }
            
            CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
            /*CTPageSz pgsz = sectPr.isSetPgSz() ? sectPr.getPgSz() : sectPr.addNewPgSz();  
            //设置为A3纸张
            pgsz.setW(BigInteger.valueOf(15840));
            pgsz.setH(BigInteger.valueOf(12240));
            pgsz.setOrient(STPageOrientation.LANDSCAPE);*/
            
            
            XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);  
      
            //添加页眉  
            CTP ctpHeader = CTP.Factory.newInstance();  
            CTR ctrHeader = ctpHeader.addNewR();  
            CTText ctHeader = ctrHeader.addNewT();  
            String headerText = "XXXXXXXXX";  
            ctHeader.setStringValue(headerText);  
            XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);  
            //设置为右对齐  
            headerParagraph.setAlignment(ParagraphAlignment.RIGHT);  
            XWPFParagraph[] parsHeader = new XWPFParagraph[1];  
            parsHeader[0] = headerParagraph;  
            policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);  
      
      
            //添加页脚  
            CTP ctpFooter = CTP.Factory.newInstance();  
            CTR ctrFooter = ctpFooter.addNewR();  
            CTText ctFooter = ctrFooter.addNewT();  
            String footerText = "XXXXXXXX";  
            ctFooter.setStringValue(footerText);  
            XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);  
            headerParagraph.setAlignment(ParagraphAlignment.CENTER);  
            XWPFParagraph[] parsFooter = new XWPFParagraph[1];  
            parsFooter[0] = footerParagraph;  
            policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);  
      
      
            document.write(outs);  
            outs.close();  
            System.out.println("create XXXX订单详情单  success.");
            data.put("code", "2");
        }
        if(data!=null)
    json=gson.toJson(data);  //传递一个参数到json里返回前台,告知是否成功并给予提示信息
        return json;
    }
// word跨列合并单元格  
    public static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {    
        for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {    
            XWPFTableCell cell = table.getRow(row).getCell(cellIndex);    
            if ( cellIndex == fromCell ) {    
                // The first merged cell is set with RESTART merge value    
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);    
            } else {    
                // Cells which join (merge) the first one, are set with CONTINUE    
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);    
            }    
        }    
    }    
    // word跨行并单元格  
    public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {    
        for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {    
            XWPFTableCell cell = table.getRow(rowIndex).getCell(col);    
            if ( rowIndex == fromRow ) {    
                // The first merged cell is set with RESTART merge value    
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);    
            } else {    
                // Cells which join (merge) the first one, are set with CONTINUE    
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);    
            }    
        }    
    }
    /** 
     * @Description: 设置表格总宽度与水平对齐方式 
     */  
    public static void setTableWidthAndHAlign(XWPFTable table, String width,  
            STJc.Enum enumValue) {  
        CTTblPr tblPr = getTableCTTblPr(table);  
        CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr  
                .addNewTblW();  
        if (enumValue != null) {  
            CTJc cTJc = tblPr.addNewJc();  
            cTJc.setVal(enumValue);  
        }  
        tblWidth.setW(new BigInteger(width));  
        tblWidth.setType(STTblWidth.DXA);  
    }
    public static CTTblPr getTableCTTblPr(XWPFTable table) {  
        CTTbl ttbl = table.getCTTbl();  
        CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl  
                .getTblPr();  
        return tblPr;  
    }
    /** 
     * @Description: 设置单元格Margin 
     */  
    public static void setTableCellMargin(XWPFTable table, int top, int left,  
            int bottom, int right) {  
        table.setCellMargins(top, left, bottom, right);  
    }
    /** 
     * @Description: 设置表格列宽 
     */  
    public static void setTableGridCol(XWPFTable table, int[] colWidths) {  
        CTTbl ttbl = table.getCTTbl();  
        CTTblGrid tblGrid = ttbl.getTblGrid() != null ? ttbl.getTblGrid()  
                : ttbl.addNewTblGrid();  
        for (int j = 0, len = colWidths.length; j < len; j++) {  
            CTTblGridCol gridCol = tblGrid.addNewGridCol();  
            gridCol.setW(new BigInteger(String.valueOf(colWidths[j])));  
        }  
    }
    /** 
     * @Description: 设置行高 
     */  
    public static void setRowHeight(XWPFTableRow row, String hight,  
            STHeightRule.Enum heigthEnum) {  
        CTTrPr trPr = getRowCTTrPr(row);  
        CTHeight trHeight;  
        if (trPr.getTrHeightList() != null && trPr.getTrHeightList().size() > 0) {  
            trHeight = trPr.getTrHeightList().get(0);  
        } else {  
            trHeight = trPr.addNewTrHeight();  
        }  
        trHeight.setVal(new BigInteger(hight));  
        if (heigthEnum != null) {  
            trHeight.setHRule(heigthEnum);  
        }  
    }
    public static CTTrPr getRowCTTrPr(XWPFTableRow row) {  
        CTRow ctRow = row.getCtRow();  
        CTTrPr trPr = ctRow.isSetTrPr() ? ctRow.getTrPr() : ctRow.addNewTrPr();  
        return trPr;  
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值