struts2配置
<span style="white-space:pre">	<!-- 用户组统计下载 -->
<span style="white-space:pre">		</span><action name="groupCountDownLoad" class="com.dbs.action.QueryAction"
<span style="white-space:pre">			</span>method="groupCountDownLoad">
<span style="white-space:pre">			</span><result name="success" type="stream">
<span style="white-space:pre">				</span><param name="contentType">text/plain</param>
<span style="white-space:pre">				</span><param name="contentDisposition">attachment;filename="${fileFileName}"</param>
<span style="white-space:pre">				</span><param name="inputName">downloadFile</param>
<span style="white-space:pre">				</span><param name="bufferSize">4096</param>
<span style="white-space:pre">			</span></result>
<span style="white-space:pre">		</span></action></span>
action中
<span style="white-space:pre">		try {
<span style="white-space:pre">			</span>fileFileName = new String("用户组统计.doc".getBytes(), "ISO8859-1");
<span style="white-space:pre">		</span>} catch (UnsupportedEncodingException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>downloadFile = qService.printGroupWord(date, dbsGroups, dbsDbtypes,status);</span>


@Override
	public InputStream printGroupWord(String date, List<DbsGroup> dbsGroups, List<DbsDbtype> dbsDbtypes,Long status) {
	XWPFDocument doc = new XWPFDocument();  
        XWPFParagraph p1 = doc.createParagraph();
        XWPFTable table = doc.createTable(dbsGroups.size()+1, dbsDbtypes.size()+2);
        // 设置上下左右四个方向的距离,可以将表格撑大
        //表格属性
        CTTblPr tablePr = table.getCTTbl().addNewTblPr();
        //表格宽度
        CTTblWidth width = tablePr.addNewTblW();
        width.setW(BigInteger.valueOf(8000));
    	ByteArrayOutputStream baos = new ByteArrayOutputStream();
        List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
        System.out.println("tableCells="+tableCells.size());
        tableCells.get(0).setText("用户组");
        for(int i = 0;i<dbsDbtypes.size();i++){
        	tableCells.get(i+1).setText(dbsDbtypes.get(i).getType());
        }
        tableCells.get(dbsDbtypes.size()+1).setText("合计");
        for(int i= 0 ;i<dbsGroups.size();i++){
        	tableCells = table.getRow(i+1).getTableCells();
        	tableCells.get(0).setText(dbsGroups.get(i).getGroupName());
        	System.out.println("i="+i);
        	for(int j = 0;j<dbsGroups.get(i).getDbTypes().size();j++){
        		tableCells.get(j+1).setText(dbsGroups.get(i).getDbTypes().get(j).getCount()+"");
        	}
        	tableCells.get(dbsGroups.get(i).getDbTypes().size()+1).setText(dbsGroups.get(i).getSum()+"");
        }
        // 设置字体对齐方式
        p1.setAlignment(ParagraphAlignment.CENTER);
        p1.setVerticalAlignment(TextAlignment.TOP);
        // 第一页要使用p1所定义的属性
        XWPFRun r1 = p1.createRun();
        // 设置字体是否加粗
        r1.setBold(true);
        r1.setFontSize(20);
        // 设置使用何种字体
        r1.setFontFamily("Courier");
        // 设置上下两行之间的间距
        r1.setTextPosition(20);
        if(status==1){
        	r1.setText(date+"发布事项汇总");
        }else if(status==2){
        	r1.setText(date+"销号事项汇总");
        }else{
        	r1.setText(date+"在办事项汇总");
        }
        try {
			doc.write(baos);
			  baos.flush();
				byte[] aa = baos.toByteArray();
				baos.close();
				return new ByteArrayInputStream(aa, 0, aa.length);
		} catch (IOException e) {
			e.printStackTrace();
		}
	return null;
	}

更多推荐

poi生成word表格文档