java导出word表格

List<ActivityMetting> list = activityMettingMapper.getWorkTotalDir(startDate,endDate,partyOrgName,activityTypeList,hostOrg);
		 //创建Document对象
        Document doc = new Document();
        Section sec = doc.addSection();
        
        Section section = doc.getSections().get(0);
        //设置页边距
        section.getPageSetup().getMargins().setTop(30f);
        section.getPageSetup().getMargins().setBottom(30f);
        section.getPageSetup().getMargins().setLeft(25f);
        section.getPageSetup().getMargins().setRight(30f);
        
        //添加段落
        Paragraph paragraph = sec.addParagraph();
        paragraph.appendText("工作记录总目录");
        //将第一段作为标题,设置标题格式
        ParagraphStyle style1 = new ParagraphStyle(doc);
        style1.setName("titleStyle");
        style1.getCharacterFormat().setBold(true);
        style1.getCharacterFormat().setTextColor(Color.BLACK);
        style1.getCharacterFormat().setFontName("宋体");
        style1.getCharacterFormat().setFontSize(17f);
        doc.getStyles().add(style1);
        paragraph.applyStyle("titleStyle");
        //设置第一个段落的对齐方式
        paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //声明数组内容
        String[] header = {"月份","日期","主题", "会议类型", "页码","备注"};

        String[][] oldData = new String[list.size()][6]; 
        Map<String,Integer> countMap = new HashMap<>();
        for(int i=0;i<list.size();i++) {
        	oldData[i]= new String[] {list.get(i).getMonth(),list.get(i).getActivityTime(),list.get(i).getMettingTitle(),list.get(i).getActivityType(),"",""};
        	Integer count = countMap.get(list.get(i).getMonth());
        	if(count == null) {
        		countMap.put(list.get(i).getMonth(), 1);
        	}else {
        		countMap.put(list.get(i).getMonth(),++count );
        	}
        }
        int newArrSize = 0;
        Set<String> set = countMap.keySet();
        for(String key:set) {
        	Integer count = countMap.get(key);
        	if(count<4) {
        		newArrSize+=(3-count);
        	}
        }
        int offSet = 0;
        String[][] data = new String[list.size()+newArrSize][6]; 
        Map<String,Integer> newMap = new LinkedHashMap<>();
        for(int i=0;i<list.size();) {
        	Integer count = countMap.get(list.get(i).getMonth());
//        	if(count < 3) {
//        		count = 3;
//        	}
        	newMap.put(list.get(i).getMonth(), count<3?3:count);
        	for(int j =0;j<(count<3?3:count);j++) {
        		if(count <= 3) {
        			 if(count == 1) {
 	        			if(j == 0) {
 	        				data[offSet]= new String[] {list.get(i+j).getMonth(),list.get(i+j).getActivityTime(),list.get(i+j).getMettingTitle(),list.get(i+j).getActivityType(),"",""};
 	            		}else {
 	            			data[offSet]= new String[] {"","","","","",""};
 	            		}
        			 }else if(count ==2) {
  	        			if(j == 0) {
 	        				data[offSet]= new String[] {list.get(i+j).getMonth(),list.get(i+j).getActivityTime(),list.get(i+j).getMettingTitle(),list.get(i+j).getActivityType(),"",""};
 	            		}else if(j==1) {
 	            			data[offSet]= new String[] {"",list.get(i+j).getActivityTime(),list.get(i+j).getMettingTitle(),list.get(i+j).getActivityType(),"",""};
 	            		}else {
 	            			data[offSet]=  new String[] {"","","","","",""};
 	            		}
        			 }else {
   	        			if(j == 0) {
  	        				data[offSet]= new String[] {list.get(i+j).getMonth(),list.get(i+j).getActivityTime(),list.get(i+j).getMettingTitle(),list.get(i+j).getActivityType(),"",""};
  	            		}else {
  	            			data[offSet]= new String[] {"",list.get(i+j).getActivityTime(),list.get(i+j).getMettingTitle(),list.get(i+j).getActivityType(),"",""};
  	            		}
        			 }
        		}else {
        			if(j == 0) {
        				data[offSet]= new String[] {list.get(i+j).getMonth(),list.get(i+j).getActivityTime(),list.get(i+j).getMettingTitle(),list.get(i+j).getActivityType(),"",""};
        			}else {
        				data[offSet]= new String[] {"",list.get(i+j).getActivityTime(),list.get(i+j).getMettingTitle(),list.get(i+j).getActivityType(),"",""};
        			}
        		}
    			offSet++;
    		}

        		i += count;

        }
        /**
        for(int i=0;i<data.length;i++) {
        	Integer count = countMap.get(data[i][0]);
        	if(count == null) {
        		countMap.put(data[i][0], 1);
        	}else {
        		countMap.put(data[i][0],++count );
        	}
        }*/
        //添加表格
        Table table = sec.addTable(true);
        table.resetCells(data.length + 1, header.length);

        //设置表格第一行作为表头,写入表头数组内容,并格式化表头数据
        TableRow row = table.getRows().get(0);
        row.isHeader(true);
        row.setHeight(20);
        row.setHeightType(TableRowHeightType.Exactly);
        row.getRowFormat().setBackColor(Color.WHITE);
        for (int i = 0; i < header.length; i++) {
            row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
            Paragraph p = row.getCells().get(i).addParagraph();
            p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            TextRange range1 = p.appendText(header[i]);
            range1.getCharacterFormat().setFontName("Arial");
            range1.getCharacterFormat().setFontSize(12f);
            range1.getCharacterFormat().setBold(true);
            range1.getCharacterFormat().setTextColor(Color.BLACK);
        }

        //写入剩余组内容到表格,并格式化数据
        for (int r = 0; r < data.length; r++) {
            TableRow dataRow = table.getRows().get(r + 1);
            dataRow.setHeight(25);
            dataRow.setHeightType(TableRowHeightType.Exactly);
            dataRow.getRowFormat().setBackColor(Color.white);
            for (int c = 0; c < data[r].length; c++) {
                dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
                range2.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
                range2.getCharacterFormat().setFontName("Arial");
                range2.getCharacterFormat().setFontSize(10f);
            }
        }

        int offSet1= 0;
        Set<String> row1 = newMap.keySet();
        for(String row2 :row1) {
        	Integer integer = newMap.get(row2);
        	int end = integer+offSet1;
        	table.applyVerticalMerge(0,offSet1+1,end);
        	offSet1 =end;
        }
        //纵向合并指定单元格
//        table.applyVerticalMerge(0,1,2);
        //table.applyVerticalMerge(0,4,5);

        //插入图片到指定单元格
//        DocPicture dp = table.getRows().get(1).getCells().get(0).addParagraph().appendPicture("1.png");
//        dp.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

//        //设置单元格背景颜色
//        for (int j = 1; j < table.getRows().getCount(); j++) {
//            if (j % 2 == 0) {
//                TableRow row2 = table.getRows().get(j);
//                for (int f = 1; f < row2.getCells().getCount(); f++) {
//                    row2.getCells().get(f).getCellFormat().setBackColor(new Color(144,238,144));
//                }
//            }
//        }

        //设置表格边框样式
        table.getTableFormat().getBorders().setBorderType(BorderStyle.Single);

        //保存文档
//        doc.saveToFile("F:\\CreateTable.docx", FileFormat.Docx_2013);
        response.setHeader("Content-type", "application/msword");
        response.setHeader("Content-disposition","attachment; filename="+ URLEncoder.encode("工作记录总目录.docx","UTF-8"));
        doc.saveToStream(response.getOutputStream(),  FileFormat.Docx_2013);

导出如下
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值