java将后台数据库查询到的数据导出word文档当中
生活随笔
收集整理的這篇文章主要介紹了
java将后台数据库查询到的数据导出word文档当中
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
java將后臺數(shù)據(jù)庫查詢到的數(shù)據(jù)導(dǎo)出word文檔當(dāng)中
之前項目需求使用Java導(dǎo)出word文檔,一直沒有進(jìn)行整理,今天把它進(jìn)行整理出來,以便以后使用到;下面是導(dǎo)出的word文檔。
// 前端報告表格 <div id="divId_emerReport"><table class="table-report"><caption>應(yīng)急處置報告</caption><tr><td style="width: 100px; height: 40px;"><span class="span-title">報警地點(diǎn)</span></td><td style="width: 200px; height: 40px;"><span id="alarmAreaName" class="span-content"></span></td><td style="width: 100px; height: 40px;"><span class="span-title">報警時間</span></td><td style="width: 200px; height: 40px;"><span id="alarmTime" class="span-content"></span></td></tr><tr><td style="width: 100px; height: 40px;"><span class="span-title">接警人</span></td><td style="width: 200px; height: 40px;"><span id="receiveAlarmPolice" class="span-content"></span></td><td style="width: 100px; height: 40px;"><span class="span-title">接警時間</span></td><td style="width: 200px; height: 40px;"><span id="receiveAlarmTime" class="span-content"></span></td></tr><tr><td style="width: 100px; height: 40px;"><span class="span-title">預(yù)案名稱</span></td><td style="width: 200px; height: 40px;"><span id="preplanName" class="span-content">${emerHandleRecord.preplanName}</span></td><td style="width: 100px; height: 40px;"><span class="span-title">啟動時間</span></td><td style="width: 200px; height: 40px;"><span id="preplanStartTime" class="span-content"><fmt:formatDate value="${emerHandleRecord.startTime}" pattern="yyyy-MM-dd HH:mm:ss"/></span></td></tr><c:forEach var="handleStep" items="${emerHandleRecord.handleStepList}" varStatus="status1"><tr><td class="bgcolor-red" style="height: 40px;" colspan="4"><span id="stepName" class="span-title">${handleStep.stepName}</span></td></tr><c:forEach var="handleGroup" items="${handleStep.handleGroupList}" varStatus="status2"><tr><td><span id="groupName" class="span-title">${handleGroup.groupName}</span></td><td colspan="3" style="height: 40px;"><table class="table-report-nest"><c:forEach var="handleMember" items="${handleGroup.handleMemberList}" varStatus="status3"><tr style="height: 40px;"><td style="width: 200px;"><span id="memberName" class="span-content">${handleMember.memberName}</span></td><td style="width: 100px;"><span id="callNo" class="span-content">${handleMember.callNo}</span></td><td style="width: 200px;"><span id="callResult" class="span-content">${handleMember.callResult}</span></td></tr></c:forEach></table></td></tr></c:forEach><tr><td height="100"><span class="span-title">過程記錄</span></td><td colspan="3"><span id="handleContent" class="span-content">${handleStep.handleContent}</span></td></tr></c:forEach><tr><td height="120"><span class="span-title">經(jīng)驗(yàn)總結(jié)</span></td><td colspan="3"><span id="experience" class="span-content">${emerHandleRecord.experience}</span></td></tr></table><div style="height: 20px;"></div> </div> // js代碼 /*** 導(dǎo)出報告*/function exportReportDocument() {var selarrrow = $("#gridId_yjjl").grid("option", "selarrrow");window.location.href ="${ctx}/yjct/yjjl/exprotWord?recordId=" +selarrrow[0];} // controller控制層代碼/*** 預(yù)案報告導(dǎo)出word文檔* @param recordId* @param request* @param response* @return* @throws IOException* @throws Exception*/@RequestMapping(value = "exprotWord")@ResponseBodypublic void exprotWord(String recordId ,HttpServletRequest request,HttpServletResponse response)throws IOException , Exception {Map<String, Object> resultMap=new HashMap<>();try {System.out.println("recordId:"+recordId);Map<String, Object> map = yjjlService.queryYjyaReport(recordId);YjyabgEntity yjyabgEntity = (YjyabgEntity) map.get("YjyabgEntity");//設(shè)置相應(yīng)到客戶端的文件名編碼response.setCharacterEncoding("UTF-8");//定義下載的文件名字String fileName = "預(yù)案報告文檔.docx";response.setContentType("application/msword;charset=UTF-8");response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "UTF-8"));response.addHeader("Pargam", "no-cache");response.addHeader("Cache-Control", "no-cache");//獲取響應(yīng)報文輸出流對象ServletOutputStream out = response.getOutputStream();ExprotSTUtil exprotWord = new ExprotSTUtil();XWPFDocument document = exprotWord.exportSTToWord(yjyabgEntity);//String s = UUID.randomUUID().toString().replace("-","");//Write the Document in file system//out = new FileOutputStream(new File("D:\\預(yù)案報告文檔"+s+".docx"));document.write(out);document.close();out.close();}catch (Exception e){e.printStackTrace();}} } // 工具類package com.cesgroup.prison.yjct.util;import com.cesgroup.prison.alarm.record.entity.AlarmRecordEntity; import com.cesgroup.prison.emergency.handle.record.entity.EmerHandleRecord; import com.cesgroup.prison.emergency.handle.recordGroup.entity.EmerHandleRecordGroup; import com.cesgroup.prison.emergency.handle.recordMember.entity.EmerHandleRecordMember; import com.cesgroup.prison.emergency.handle.recordStep.entity.EmerHandleRecordStep; import com.cesgroup.prison.yjct.entity.GzzcyEntity; import com.cesgroup.prison.yjct.entity.YjyabgEntity; import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger; import java.text.SimpleDateFormat; import java.util.List;/*** @author 陳才島* @create 2019-08-15 15:00*/ public class ExprotSTUtil {public XWPFDocument exportSTToWord(YjyabgEntity yjyabgEntity) throws IOException{//創(chuàng)建document對象XWPFDocument document = new XWPFDocument();//添加標(biāo)題XWPFParagraph titleParagraph = document.createParagraph();//設(shè)置段落居中titleParagraph.setAlignment(ParagraphAlignment.CENTER);XWPFRun titleParagraphRun = titleParagraph.createRun();titleParagraphRun.setText("預(yù)案報告");titleParagraphRun.setColor("000000");titleParagraphRun.setBold(true); //設(shè)置加粗titleParagraphRun.setFontSize(20);List<List<GzzcyEntity>> workgroup = yjyabgEntity.getWorkgroup();//動態(tài)生成表格的行數(shù)int x=0;int m=yjyabgEntity.getWorkgroup().size();for(int i=0;i<workgroup.size();i++){List<GzzcyEntity> gzzcyEntities = workgroup.get(i);if(gzzcyEntities.size()==0){m--;}int size = gzzcyEntities.size();x+=size;}//創(chuàng)建表格int row = m+x+6;System.out.println("表格的行數(shù):"+row);//int column = 4;XWPFTable table = document.createTable();//列寬自動CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW();infoTableWidth.setType(STTblWidth.DXA);infoTableWidth.setW(BigInteger.valueOf(9072));//創(chuàng)建一個段落XWPFParagraph p = document.createParagraph();//第一行XWPFTableRow row1 = table.getRow(0);XWPFTableCell cell1 = row1.getCell(0);XWPFTableCell cell2 = row1.addNewTableCell();XWPFTableCell cell3 = row1.addNewTableCell();XWPFTableCell cell4 = row1.addNewTableCell();//跨列合并 合并第一行 第1列到第4列mergeCellsHorizontal(table,0,0,3);p = cell1.addParagraph();XWPFRun cell1Run= p.createRun();cell1Run.setText("基本信息:");cell1Run.setFontSize(12);cell1Run.setBold(true);cell1Run.setColor("000000");//表格第二行XWPFTableRow row2 = table.createRow();XWPFTableCell row2Cell1 = row2.getCell(0);XWPFTableCell row2Cell2 = row2.getCell(1);XWPFTableCell row2Cell3 = row2.getCell(2);XWPFTableCell row2Cell4 = row2.getCell(3);XWPFRun row2Run1 = row2Cell1.addParagraph().createRun();row2Run1.setText("報警地點(diǎn):");row2Run1.setFontSize(12);row2Run1.setColor("000000");XWPFRun row2Run2 = row2Cell2.addParagraph().createRun();row2Run2.setText(yjyabgEntity.getAlertArea());XWPFRun row2Run3 = row2Cell3.addParagraph().createRun();row2Run3.setText("報警時間:");row2Run3.setFontSize(12);row2Run3.setColor("000000");XWPFRun row2Run4 = row2Cell4.addParagraph().createRun();row2Run4.setText(yjyabgEntity.getAlertTime());//表格第三行XWPFTableRow row3 = table.createRow();XWPFTableCell row3cell1 = row3.getCell(0);XWPFTableCell row3cell2 = row3.getCell(1);XWPFTableCell row3cell3 = row3.getCell(2);XWPFTableCell row3cell4 = row3.getCell(3);XWPFRun row3cell1Run = row3cell1.addParagraph().createRun();row3cell1Run.setText("預(yù)案詳情:");row3cell1Run.setFontSize(12);row3cell1Run.setBold(true);row3cell1Run.setColor("000000");//跨列合并 合并第三行 第1列到第4列mergeCellsHorizontal(table,2,0,3);//表格第四行XWPFTableRow row4 = table.createRow();XWPFTableCell row4Cell1 = row4.getCell(0);XWPFTableCell row4Cell2 = row4.getCell(1);XWPFTableCell row4Cell3 = row4.getCell(2);XWPFTableCell row4Cell4 = row4.getCell(3);XWPFRun row4Cell1Run = row4Cell1.addParagraph().createRun();row4Cell1Run.setText("預(yù)案名稱:");row4Cell1Run.setFontSize(12);row4Cell1Run.setColor("000000");XWPFRun row4Cell2Run = row4Cell2.addParagraph().createRun();row4Cell2Run.setText(yjyabgEntity.getPlanName());XWPFRun row4Cell3Run = row4Cell3.addParagraph().createRun();row4Cell3Run.setText("啟動時間:");row4Cell3Run.setFontSize(12);row4Cell3Run.setColor("000000");XWPFRun row4Cell4Run = row4Cell4.addParagraph().createRun();row4Cell4Run.setText(yjyabgEntity.getCreateTime());//表格第五行XWPFTableRow row5 = table.createRow();XWPFTableCell row5Cell1 = row5.getCell(0);XWPFTableCell row5Cell2 = row5.getCell(1);XWPFTableCell row5Cell3 = row5.getCell(2);XWPFTableCell row5Cell4 = row5.getCell(3);XWPFRun row5Cell1run = row5Cell1.addParagraph().createRun();row5Cell1run.setText("接警人:");row5Cell1run.setFontSize(12);row5Cell1run.setColor("000000");XWPFRun row5Cell2run = row5Cell2.addParagraph().createRun();row5Cell2run.setText(yjyabgEntity.getReceiveAlarmPolice());XWPFRun row5Cell3run = row5Cell3.addParagraph().createRun();row5Cell3run.setText("接警時間:");row5Cell3run.setFontSize(12);row5Cell3run.setColor("000000");XWPFRun row5Cell4run = row5Cell4.addParagraph().createRun();row5Cell4run.setText(yjyabgEntity.getReceiveTime());//動態(tài)生成行int count=0;int n=0;for(int i=0;i<workgroup.size();i++){List<GzzcyEntity> gzzcyEntities = workgroup.get(i);if(gzzcyEntities.size()>0){XWPFTableRow gzz = table.createRow();XWPFTableCell RowiCell1 = gzz.getCell(0);XWPFTableCell RowiCell2 = gzz.getCell(1);XWPFTableCell RowiCell3 = gzz.getCell(2);XWPFTableCell RowiCell4 = gzz.getCell(3);XWPFRun RowiCell1run1 = RowiCell1.addParagraph().createRun();RowiCell1run1.setText("工作組"+(i+1)+":");RowiCell1run1.setFontSize(12);RowiCell1run1.setBold(true);RowiCell1run1.setColor("000000");n++;int size = gzzcyEntities.size();if(n==1){ //第一次進(jìn)入//跨列合并 合并第6行 第2列到第4列mergeCellsHorizontal(table,5,1,3);count+=size;}if(n>1){//第二次進(jìn)入//跨列合并 合并第6行 第2列到第4列mergeCellsHorizontal(table,5+count+1,1,3);count=count+size+1;}for(int j=0;j<size;j++){XWPFTableRow gzzcy = table.createRow();XWPFTableCell RowjCell1 = gzzcy.getCell(0);XWPFTableCell RowjCell2 = gzzcy.getCell(1);XWPFTableCell RowjCell3 = gzzcy.getCell(2);XWPFTableCell RowjCell4 = gzzcy.getCell(3);XWPFRun RowjCell1run = RowjCell1.addParagraph().createRun();RowjCell1run.setText("姓名:");RowjCell1run.setFontSize(12);RowjCell1run.setColor("000000");XWPFRun RowjCell2run = RowjCell2.addParagraph().createRun();RowjCell2run.setText(gzzcyEntities.get(j).getWgmPoliceName());XWPFRun RowjCell3run = RowjCell3.addParagraph().createRun();RowjCell3run.setText("聯(lián)系方式:");RowjCell3run.setFontSize(12);RowjCell3run.setColor("000000");XWPFRun RowjCell4run = RowjCell4.addParagraph().createRun();RowjCell4run.setText(gzzcyEntities.get(j).getPbdPhone());}}}//最后一行XWPFTableRow lastRow = table.createRow();XWPFTableCell lastRowcell1 = lastRow.getCell(0);XWPFTableCell lastRowcell2 = lastRow.getCell(1);XWPFTableCell lastRowcell3 = lastRow.getCell(2);XWPFTableCell lastRowcell4 = lastRow.getCell(3);XWPFRun lastRowcell1run = lastRowcell1.addParagraph().createRun();lastRowcell1run.setText("過程記錄:");lastRowcell1run.setBold(true);lastRowcell1run.setFontSize(12);lastRowcell1run.setColor("000000");XWPFRun lastRowcell2run = lastRowcell2.addParagraph().createRun();lastRowcell2run.setText(yjyabgEntity.getProcessRecord());//跨列合并 合并最后一行 第2列到第4列int a=row-1;mergeCellsHorizontal(table,a,1,3);CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);//添加頁眉CTP ctpHeader = CTP.Factory.newInstance();CTR ctrHeader = ctpHeader.addNewR();CTText ctHeader = ctrHeader.addNewT();XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);//設(shè)置為右對齊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();XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);headerParagraph.setAlignment(ParagraphAlignment.CENTER);XWPFParagraph[] parsFooter = new XWPFParagraph[1];parsFooter[0] = footerParagraph;policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);FileOutputStream out=null;return document;}/**** 合并行* @param table* @param col 所合并的列(哪一列需要合并行)* @param fromRow 起始行* @param toRow 終止行*/private 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 valuecell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);} else {// Cells which join (merge) the first one, are set with CONTINUEcell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);}}}/**** 合并列* @param table* @param row 所合并的行(哪一行需要合并列)* @param fromCell 起始列* @param toCell 終止列*/private 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 valuecell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);} else {// Cells which join (merge) the first one, are set with CONTINUEcell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);}}}/**** 導(dǎo)出word 設(shè)置行寬* @param table* @param width*/private void setTableWidth(XWPFTable table,String width){CTTbl ttbl = table.getCTTbl();CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();CTJc cTJc=tblPr.addNewJc();cTJc.setVal(STJc.Enum.forString("center"));tblWidth.setW(new BigInteger(width));tblWidth.setType(STTblWidth.DXA);}/*** liyanzhe** */public XWPFDocument exportSTToWord1(EmerHandleRecord emerHandleRecord, AlarmRecordEntity alarmRecordEntity) throws IOException{List<EmerHandleRecordStep> emerHandleRecordSteps = emerHandleRecord.getHandleStepList();//動態(tài)生成表格的行數(shù)int x=0;//有多少人收到的行數(shù)int m=emerHandleRecord.getHandleStepList().size()*2;//有幾步和過程記錄的行數(shù)for (EmerHandleRecordStep emerHandleRecordStep : emerHandleRecordSteps) {List<EmerHandleRecordGroup> emerHandleRecordGroups = emerHandleRecordStep.getHandleGroupList();if (emerHandleRecordGroups!=null) {for (EmerHandleRecordGroup emerHandleRecordGroup : emerHandleRecordGroups) {if(emerHandleRecordGroup.getHandleMemberList()!=null){x+= emerHandleRecordGroup.getHandleMemberList().size();}else{x++;}}}else{x++;}}//表格總行數(shù)int row = m+x+4;System.out.println("表格的行數(shù):"+row);//date轉(zhuǎn)stringString pattern2 = "yyyy-MM-dd HH:mm:ss";SimpleDateFormat sdf2 = new SimpleDateFormat(pattern2);//創(chuàng)建document對象XWPFDocument document = new XWPFDocument();//添加標(biāo)題XWPFParagraph titleParagraph = document.createParagraph();//設(shè)置段落居中titleParagraph.setAlignment(ParagraphAlignment.CENTER);XWPFRun titleParagraphRun = titleParagraph.createRun();titleParagraphRun.setText("應(yīng)急處置報告");titleParagraphRun.setColor("000000");titleParagraphRun.setBold(true); //設(shè)置加粗titleParagraphRun.setFontSize(20);// 創(chuàng)建表格XWPFTable table = document.createTable(row, 4);//列寬自動CTTblWidth infoTableWidth = table.getCTTbl().addNewTblPr().addNewTblW();infoTableWidth.setType(STTblWidth.DXA);infoTableWidth.setW(BigInteger.valueOf(9072));//表格樣式tableBorderStyle(table);// 第一行List<XWPFTableCell> tableCells1 = table.getRow(0).getTableCells();tableTextStyle(tableCells1,0,"報警地點(diǎn)",true,false);tableTextStyle(tableCells1,1,emerHandleRecord.getAlarmAreaName(),false,false);tableTextStyle(tableCells1,2,"報警時間",true,false);tableTextStyle(tableCells1,3,sdf2.format(alarmRecordEntity.getArdAlertTime()),false,false);// 第二行List<XWPFTableCell> tableCells2 = table.getRow(1).getTableCells();tableTextStyle(tableCells2,0,"接警人",true,false);tableTextStyle(tableCells2,1,alarmRecordEntity.getArdReceiveAlarmPolice(),false,false);tableTextStyle(tableCells2,2,"接警時間",true,false);tableTextStyle(tableCells2,3,sdf2.format(alarmRecordEntity.getArdReceiveTime()),false,false);//alarmRecordEntity.getArdReceiveTime()這個里面沒值,bug// 第三行List<XWPFTableCell> tableCells3 = table.getRow(2).getTableCells();tableTextStyle(tableCells3,0,"預(yù)案名稱",true,false);tableTextStyle(tableCells3,1,emerHandleRecord.getPreplanName(),false,false);tableTextStyle(tableCells3,2,"啟動時間",true,false);tableTextStyle(tableCells3,3,sdf2.format(emerHandleRecord.getStartTime()),false,false);int num = 0;//計算行數(shù)int firstnum = 0;//小隊名稱開始行for(int i=0;i<emerHandleRecordSteps.size();i++){EmerHandleRecordStep emerHandleRecordStep = emerHandleRecordSteps.get(i);//第幾步那行的合并mergeCellsHorizontal(table,3+num,0,3);String stepName = emerHandleRecordStep.getStepName();//第幾步//第幾步那行賦值List<XWPFTableCell> tableCellsi = table.getRow(3+num).getTableCells();tableTextStyle(tableCellsi,0,stepName,true,true);String handleContent = emerHandleRecordStep.getHandleContent();//過程記錄List<EmerHandleRecordGroup> emerHandleRecordGroups = emerHandleRecordStep.getHandleGroupList();if (emerHandleRecordGroups!=null) {for (EmerHandleRecordGroup emerHandleRecordGroup : emerHandleRecordGroups) {String groupName = emerHandleRecordGroup.getGroupName();//小隊名稱if(emerHandleRecordGroup.getHandleMemberList()!=null){List<EmerHandleRecordMember> emerHandleRecordMembers= emerHandleRecordGroup.getHandleMemberList();for(int l=0;l< emerHandleRecordMembers.size();l++){EmerHandleRecordMember emerHandleRecordMember = emerHandleRecordMembers.get(l);String meberName = emerHandleRecordMember.getMemberName();//方式那列名稱String callNo = emerHandleRecordMember.getCallNo();//呼叫號String callresult = emerHandleRecordMember.getCallResult();//呼叫內(nèi)容已發(fā)送List<XWPFTableCell> tableCellsy = table.getRow(4+num).getTableCells();if(l==0){//第一次進(jìn)入,給小隊名稱賦值tableTextStyle(tableCellsy,0,groupName,true,false);//在有多個成員的時候給小隊名稱賦一個值//記錄第一次的行數(shù),為多個成員時,合并小隊名稱有初始行firstnum = 4+num;}//方式,呼叫號,呼叫內(nèi)容已發(fā)送tableTextStyle(tableCellsy,1,meberName,false,false);tableTextStyle(tableCellsy,2,callNo,false,false);tableTextStyle(tableCellsy,3,callresult,false,false);if(l==emerHandleRecordMembers.size()-1){//多個成員時,合并小隊名稱mergeCellsVertically(table, 0, firstnum, 4+num);}num++;}//過程記錄List<XWPFTableCell> tableCellsz = table.getRow(4+num).getTableCells();tableTextStyle(tableCellsz,0,"過程記錄",true,false);mergeCellsHorizontal(table,4+num,1,3);tableTextStyle(tableCellsz,1,handleContent,false,false);}else{//沒有成員的時候,顯示的小隊名稱List<XWPFTableCell> tableCellsy = table.getRow(4+num).getTableCells();tableTextStyle(tableCellsy,0,groupName,true,false);//過程記錄List<XWPFTableCell> tableCellsz = table.getRow(5+num).getTableCells();tableTextStyle(tableCellsz,0,"過程記錄",true,false);mergeCellsHorizontal(table,5+num,1,3);tableTextStyle(tableCellsz,1,handleContent,false,false);num++;}num++;}}else{num++;}num++;}//最后一行String experience = emerHandleRecord.getExperience();//經(jīng)驗(yàn)總結(jié)List<XWPFTableCell> tableCellsl = table.getRow(row-1).getTableCells();tableTextStyle(tableCellsl,0,"經(jīng)驗(yàn)總結(jié)",true,false);mergeCellsHorizontal(table,row-1,1,3);tableTextStyle(tableCellsl,1,experience,false,false);return document;}private static void tableBorderStyle(XWPFTable table){//表格屬性CTTblPr tablePr = table.getCTTbl().addNewTblPr();//表格寬度CTTblWidth width = tablePr.addNewTblW();width.setW(BigInteger.valueOf(8000));//表格顏色CTTblBorders borders=table.getCTTbl().getTblPr().addNewTblBorders();//表格內(nèi)部橫向表格顏色CTBorder hBorder=borders.addNewInsideH();hBorder.setVal(STBorder.Enum.forString("single"));hBorder.setSz(new BigInteger("1"));hBorder.setColor("dddddd");//表格內(nèi)部縱向表格顏色CTBorder vBorder=borders.addNewInsideV();vBorder.setVal(STBorder.Enum.forString("single"));vBorder.setSz(new BigInteger("1"));vBorder.setColor("dddddd");//表格最左邊一條線的樣式CTBorder lBorder=borders.addNewLeft();lBorder.setVal(STBorder.Enum.forString("single"));lBorder.setSz(new BigInteger("1"));lBorder.setColor("dddddd");//表格最左邊一條線的樣式CTBorder rBorder=borders.addNewRight();rBorder.setVal(STBorder.Enum.forString("single"));rBorder.setSz(new BigInteger("1"));rBorder.setColor("dddddd");//表格最上邊一條線(頂部)的樣式CTBorder tBorder=borders.addNewTop();tBorder.setVal(STBorder.Enum.forString("single"));tBorder.setSz(new BigInteger("1"));tBorder.setColor("dddddd");//表格最下邊一條線(底部)的樣式CTBorder bBorder=borders.addNewBottom();bBorder.setVal(STBorder.Enum.forString("single"));bBorder.setSz(new BigInteger("1"));bBorder.setColor("dddddd");}private static void tableTextStyle(List<XWPFTableCell> tableCells1,int index,String text,boolean bold,boolean bjColor){tableTextStyle(tableCells1.get(index),text,bold,bjColor);}private static void tableTextStyle(XWPFTableCell tableCell,String text,boolean bold,boolean bjColor){if(bjColor){tableCell.setColor("DBDBDB");//設(shè)置表格背景顏色}tableCell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);//垂直居中XWPFParagraph p0 = tableCell.addParagraph();tableCell.setParagraph(p0);XWPFRun r0 = p0.createRun();// 設(shè)置字體是否加粗r0.setBold(bold);r0.setFontSize(8);// 設(shè)置使用何種字體r0.setFontFamily("微軟雅黑");// 設(shè)置上下兩行之間的間距//r0.setTextPosition(8);r0.setColor("333333");r0.setText(text);} }
總結(jié)
以上是生活随笔為你收集整理的java将后台数据库查询到的数据导出word文档当中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 远程无线视频监控知识
- 下一篇: 金融组织做规模化敏捷怎么划小队,一文讲清