SSM项目导出Excel操作

来源:互联网 发布:如何组建办公网络 编辑:程序博客网 时间:2024/06/05 18:53
页面:
按钮:
 <shiro:hasPermission name="carFlow/exportAll.do">
        .navButtonAdd( _pager_id,{
        iconTitle : "&nbsp;&nbsp;导出表格&nbsp;&nbsp;",
            buttonicon : "ace-icon fa fa-arrow-circle-o-down orange",
            onClickButton : exportAll
        })
        </shiro:hasPermission>

//导出按条件查询出的数据
function exportAll(){
var $tdh = $("#tdh").val();
var $barCode = $("#barCode").val();
lui.confirm("确定要导出流向数据吗?",function(){
var data={//传递的数据
tdh : $tdh,
barCode : $barCode
         };
location="${base}/admin/carFlow/exportAll.do?tdh="+$tdh+"&barCode="+$barCode;
    },function(){
    return;
    });
}


controller:


@RequestMapping(value = "/exportAll", method = RequestMethod.GET)
    public String exportAll(HttpSession session,
    @RequestParam(value = "barCode",required = false)String barCode,
    @RequestParam(value = "tdh",required = false)String tdh,
    HttpServletRequest request,HttpServletResponse response) {
        OutputStream out=null; 
        String exportFileName = "流向信息";
        try {
            HSSFWorkbook hwb = exportAllHis(barCode,tdh);
            //response.setContentType("application/x-msdownload");
            response.setContentType("application/vnd.ms-excel"); 
          //根据浏览器类型处理文件名称
            String agent = request.getHeader("USER-AGENT").toLowerCase();
            if (agent.indexOf("firefox") > -1){//若是火狐
            exportFileName = new String(exportFileName.getBytes("UTF-8"), "ISO8859-1");
            } else {//其他浏览器
            exportFileName = java.net.URLEncoder.encode(exportFileName, "UTF-8");
            }
            response.setHeader("Content-Disposition", "attachment;filename="
                    + exportFileName + ".xls");
             out = response.getOutputStream();
            hwb.write(out);
            out.flush();
            out.close();  
        } catch (Exception e) {
        e.printStackTrace();
        } 
        return null;
    }

public HSSFWorkbook exportAllHis(String barCode,String tdh) {
String[] cellname={"序号","条码号","车牌号","流向号","司机姓名","联系方式","计划入港时间",
"计划提货重量","实际过磅重量","计划提货件数","实际埋货件数","备注"};
        HSSFWorkbook hwb = new HSSFWorkbook();
        HSSFSheet sheet = hwb.createSheet(new Date().getTime()+"report");
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell;
        HSSFCellStyle stycle = hwb.createCellStyle();
        stycle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        for (int i = 0; i < cellname.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(cellname[i]);
            cell.setCellStyle(stycle);
            sheet.setColumnWidth((short) i, cellname[i].getBytes().length * 250);
        }
        List<BbcCarOutplanBean> list = bbcCarOutplanService.findBySearch(barCode,tdh);
        SimpleDateFormat smp=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (list != null) { 
            for (int j = 0; j < list.size(); j++) {
                row = sheet.createRow(j + 1);
                row.createCell(0).setCellValue(j+1+"");
                BbcCarOutplanBean obj=list.get(j); 
                if(obj.getBarCode()!=null){ row.createCell(1).setCellValue(obj.getBarCode()+""); }
                if(obj.getCarno()!=null){ row.createCell(2).setCellValue(obj.getCarno()+""); } 
                if(obj.getTdh()!=null){ row.createCell(3).setCellValue(obj.getTdh()+""); }
                if(obj.getDriverName()!=null){ row.createCell(4).setCellValue(obj.getDriverName()+""); }
                if(obj.getDriverTel()!=null){ row.createCell(5).setCellValue(obj.getDriverTel()+""); } 
                if(obj.getCarPlanTime()!=null){ row.createCell(6).setCellValue(smp.format(obj.getCarPlanTime())); }
                if(obj.getCarPlanLoad()!=null){ row.createCell(7).setCellValue(obj.getCarPlanLoad()+""); }
                if(obj.getNetCarWeight()!=null){ row.createCell(8).setCellValue(obj.getNetCarWeight()+""); }
                if(obj.getCarPlanPieces()!=null){ row.createCell(9).setCellValue(obj.getCarPlanPieces()+""); }
                if(obj.getCarPlanPieces()!=null){ row.createCell(10).setCellValue(obj.getCarPlanPieces()+""); }
                if(obj.getRemark()!=null){ row.createCell(11).setCellValue(obj.getRemark()+""); }
            }
        }
        return hwb;
}

serviceimpl 查出要导出的数据;

@Override
public List<BbcCarOutplanBean> findBySearch(String barCode, String tdh) {
return bbcCarOutplanBeanMapper.findBySearch(barCode,tdh);
}

mapper:
<select id="findBySearch" resultMap="BaseResultMap">
  select 
  <include refid="Base_Column_List" />
  ,
  <include refid="Blob_Column_List" />
  from bbc_car_outplan
  <where>
  and CANCEL_FLAG = '0'
<if test="tdh != null and tdh != ''">
and tdh = #{tdh}
</if>
<if test="barCode != null and barCode != ''">
and bar_code LIKE CONCAT("%",#{barCode},"%")
</if>
</where>
  order by CAR_PLAN_TIME desc
  </select>
原创粉丝点击