赞
踩
@GetMapping(value = "/getPartyOrganCensus")public void getPartyOrganCensus(HttpServletRequest request, HttpServletResponse response) throwsIOException{
List list=commonPartyOrganMapperExt.getPartyOrganCensus();
PartyOrganCensusVO total=newPartyOrganCensusVO();
total.setRegionName("合计");
total.setTotalPartyMember(list.stream().collect(Collectors.summingInt(PartyOrganCensusVO::getTotalPartyMember)));
total.setTotalPartyOrgan(list.stream().collect(Collectors.summingInt(PartyOrganCensusVO::getTotalPartyOrgan)));
total.setPartyCommittee(list.stream().collect(Collectors.summingInt(PartyOrganCensusVO::getPartyCommittee)));
total.setPartyGeneralBranch(list.stream().collect(Collectors.summingInt(PartyOrganCensusVO::getPartyGeneralBranch)));
total.setPartyBranch(list.stream().collect(Collectors.summingInt(PartyOrganCensusVO::getPartyBranch)));
list.add(total);
String tempPath= "static/exportTemplates/党组织情况汇总表.docx";
String outputUrl= "static/exportTemplates/test.docx";
createWord(tempPath,outputUrl,list);
String fileName="党组织情况汇总表.doc";
fileName= URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-Disposition","attachment; filename=" +fileName);
response.setContentType("application/msword");//定义输出类型
response.setStatus(200);
OutputStream out=response.getOutputStream();byte[] buf = new byte[2 * 1024];intlen;
FileInputStream in= newFileInputStream(outputUrl);while ((len = in.read(buf)) != -1) {
out.write(buf,0, len);
}
in.close();
out.close();
}public void createWord(String inputUrl, String outputUrl,ListtableList) {try{
XWPFDocument document= newXWPFDocument(POIXMLDocument.openPackage(inputUrl));
List tables =document.getTables();for (int i = 0; i < tables.size(); i++) {//只处理行数大于等于2的表格,且不循环表头
XWPFTable table =tables.get(i);if(table.getRows().size()>1){
insertTable(table, tableList);
}
}
File file= newFile(outputUrl);
FileOutputStream stream= newFileOutputStream(file);
document.write(stream);
stream.close();
}catch(IOException e) {
e.printStackTrace();
}
}public static void insertTable(XWPFTable table, ListtableList){
XWPFTableRow oldrows= table.getRows().get(2);
List cellList =oldrows.getTableCells();
CTTrPr trpr=oldrows.getCtRow().getTrPr();//创建行,根据需要插入的数据添加新行,不处理表头
for(int i = 0; i < tableList.size(); i++){
XWPFTableRow newRow= table.insertNewTableRow(i+3);//复制行属性
newRow.getCtRow().setTrPr(trpr);if (null ==cellList) {return;
}//复制列及其属性和内容
for(XWPFTableCell sourceCell : cellList) {
XWPFTableCell newCell=newRow.createCell();//列属性
newCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr());
String text="";int j=cellList.indexOf(sourceCell);if(j == 0)
text=tableList.get(i).getRegionName();else if(j==2)
text=tableList.get(i).getTotalPartyMember().toString();else if(j==3)
text=tableList.get(i).getTotalPartyOrgan().toString();else if(j==4)
text=tableList.get(i).getPartyCommittee().toString();else if(j==5)
text=tableList.get(i).getPartyGeneralBranch().toString();else if(j==6)
text=tableList.get(i).getPartyBranch().toString();//段落属性
if(sourceCell.getParagraphs()!=null&&sourceCell.getParagraphs().size()>0){
newCell.getParagraphs().get(0).getCTP().setPPr(sourceCell.getParagraphs().get(0).getCTP().getPPr());
newCell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);//垂直居中
if(sourceCell.getParagraphs().get(0).getRuns()!=null&&sourceCell.getParagraphs().get(0).getRuns().size()>0){
XWPFRun cellR= newCell.getParagraphs().get(0).createRun();
cellR.setText(text);
cellR.setBold(sourceCell.getParagraphs().get(0).getRuns().get(0).isBold());
cellR.setSubscript(VerticalAlign.SUBSCRIPT);
}else{
newCell.setText(text);
}
}else{
newCell.setText(text);
}
}
}
table.removeRow(2);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。