导出excel

部分导出和全部导出:

<a class="btn btn-default" href="javascript:void(0)" οnclick="exportExcel(0)"><span><span style="font-size: 14px;">导出</span></span></a>
<a class="btn btn-default" href="javascript:void(0)" οnclick="exportExcel(1)"><span><span style="font-size: 14px;">全部导出</span></span></a>

<input type="checkbox" id="checkAll" οnclick="setAllCheckedStatus('id',this.checked)">全选  
<input type="checkbox" name="id" value="<s:property value="id" />" οnclick="setChkAllStatus('id',this,'checkAll');">

//导出Excel
function exportExcel(num){
	if(num==0){
		var ids = getcbxIds('id');
		if(ids == ""){
			alert("请选择要导出的信息");
			return false;
		}
		$("#specifiedWeiboList_form :input[name='ids']").val(ids);
	}

	var action = getBaseURL()+'/client/SpecifiedWeibo!exportExcel.action?num='+num;
	$("#specifiedWeiboList_form").prop("action",action);
	$("#specifiedWeiboList_form").submit();
}
//获取一组复选框的值
function getcbxIds(id) {
	var values = "";
	var els = $('input[name='+id+']');
	for (var i = 0; i < els.length; i++) {
		if (els[i].name && els[i].checked) {
			if (values.length > 0) {
				values += ",";
			}
			values += els[i].value;
		}
	}
	return values;
}

public String exportExcel() {
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(os);
            //创建单元薄,设置Excel 单元薄名称
            WritableSheet sheet2 = workbook.createSheet("特定微博", 0);
            Label label2 = null;

            label2 = new jxl.write.Label(0, 0, "微博标题");
            sheet2.setColumnView(0, 20);
            sheet2.addCell(label2);

            label2 = new jxl.write.Label(1, 0, "微博来源");
            sheet2.setColumnView(2, 20);
            sheet2.addCell(label2);
            
            label2 = new jxl.write.Label(2, 0, "微博分类");
            sheet2.setColumnView(1, 20);
            sheet2.addCell(label2);
            
            label2 = new jxl.write.Label(3, 0, "发布日期");
            sheet2.setColumnView(1, 20);
            sheet2.addCell(label2);

            label2 = new jxl.write.Label(4, 0, "链接地址");
            sheet2.setColumnView(3, 20);
            sheet2.addCell(label2);

            label2 = new jxl.write.Label(5, 0, "微博内容");
            sheet2.setColumnView(4, 20);
            sheet2.addCell(label2);

            int i = 1;
            if (num == 0) {
                String[] idsArr = ids.split(",");
                List idsList = Arrays.asList(idsArr);
                List<SpecifiedWeibo> specifiedWeiboListOther = specifiedWeiboService.findByIds(idsList);
                specifiedWeiboList = specifiedWeiboListOther;
            } else {
                if(specifiedWeibo ==null){
                    specifiedWeibo = new SpecifiedWeibo();
                }
                specifiedWeibo.setOrganId(getOrganId());
                specifiedWeibo.setOther("orderBy","createdate desc");
                specifiedWeiboList = specifiedWeiboService.list(specifiedWeibo);
            }
            for (SpecifiedWeibo s : specifiedWeiboList) {
                specifiedWeibo = specifiedWeiboService.findById(s.getId());
                label2 = new Label(0, i, specifiedWeibo.getTitle());
                sheet2.addCell(label2);

                label2 = new Label(1, i, specifiedWeibo.getSource());
                sheet2.addCell(label2);
                
                Integer weiboType = specifiedWeibo.getWeiboType();
                String TypeName = "";
                if (weiboType == 1) {
                    TypeName = "有害信息类";
                } else if (weiboType == 2) {
                    TypeName = "广告类";
                } else if (weiboType == 3) {
                    TypeName = "未知";
                }
                label2 = new Label(2, i, TypeName);
                sheet2.addCell(label2);

                label2 = new Label(3, i, DateFunction.getString(specifiedWeibo.getCreateDate(), "yyyy-MM-dd HH:mm:ss"));
                sheet2.addCell(label2);
                
                label2 = new Label(4, i, specifiedWeibo.getLocation());
                sheet2.addCell(label2);

                label2 = new Label(5, i, HTMLSpirit.delHTMLTag(specifiedWeibo.getDescription()));
                sheet2.addCell(label2);
                i++;
            }
            workbook.write();
            workbook.close();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String date = format.format(new Date());
            fileName = date + "-weibo.xls";
            inputStream = new ByteArrayInputStream(os.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "excel";
    }

<!-- 特定微博 -->
		<action name="SpecifiedWeibo!*" method="{1}" class="com.founder.fwpt.action.client.specifiedWeibo.SpecifiedWeiboAction">
		    <result name="success">/WEB-INF/fwpt/client/specifiedWeibo/specifiedWeibo.jsp</result>
			<result name="list">/WEB-INF/fwpt/part/client/specifiedWeibo/specifiedWeibo-list.jsp</result>
			<result name="analysisPage">/WEB-INF/fwpt/part/client/specifiedWeibo/analysisPage.jsp</result>
			<result name="excel" type="stream">  
               <param name="contentType">application/octet-stream</param>  
               <param name="contentDisposition">attachment;fileName="${fileName}"</param>  
               <param name="inputName">inputStream</param>  
               <param name="bufferSize">4096</param>  
             </result>
		</action>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值