Java实现从网页表格导出CSV文件的例子

来源:互联网 发布:计算机java就业前景 编辑:程序博客网 时间:2024/05/16 14:44

前台网页代码  

<div class="row-fluid">

    <div class="span12">
      <div class="widget-box">
        <div class="widget-title">
          <h5>中奖信息列表</h5>
          <div class="controls controls-row" style="padding-top:5px;">
          <select id="export" name="export" class="span2 m-wrap" style="width:120px">
<option value="-1">导出文件</option>
   <option value="XLS">导出Xls</option>
</select>
          </div>
        </div>
        <div class="widget-content nopadding">
          <table class="table table-bordered data-table table-striped with-check">
            <thead>
              <tr>
                <th style="width:30px;">序号</th>
                <th>用户ID</th>
                <th>姓名</th>
                <th>联系方式</th>
                <th>第一关</th>
                <th>时间</th>
                <th>第二关</th>
                <th>时间</th>
                <th>第三关</th>
                <th>时间</th>
                </tr>
            </thead>
            <tbody>
              
            </tbody>
          </table>
        </div>
      </div>
    </div>

  </div>

前台对应js代码

$("#export").change(function() {
var v = $(this).val();
if(v == "XLS") {
window.open($ctx + '/prize/exportAsXls?firststep=' + $('#firststep').val()+"&secondstep="+$('#secondstep').val()+"&thirdstep="+$('#thirdstep').val());
}
$(this).val("-1");
});

后台业务代码


@Controller
@RequestMapping(value="/prize")
public class PrizeManagerController extends BaseController{
    private static final Logger logger = (Logger) LoggerFactory.getLogger(PrizeController.class);
    @Autowired
    private ManagerLogService managerLogService;
    
    @Autowired
    private PrizeManagerService prizeManagerService;

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportAsXls", method = RequestMethod.GET)
    public void exportAsXls(PrizeSearchVo search, HttpServletRequest request, HttpServletResponse response) {
    StringBuffer buf = new StringBuffer();
    buf.append("姓名").append(",");
    buf.append("联系方式").append(",");
    buf.append("交易账号").append(",");
    buf.append("会员单位").append(",");
    buf.append("注册时间").append(",");
    buf.append("第一关").append(",");
    buf.append("时间").append(",");
    buf.append("第二关").append(",");
    buf.append("时间").append(",");
    buf.append("第三关").append(",");
    buf.append("时间").append(",");
   
    List<PrizeConfigVo> dataList = this.prizeManagerService.queryAllPrizeConfigVoData(search);
    if (dataList != null && !dataList.isEmpty()) {
    for (PrizeConfigVo p : dataList) {
    buf.append("\r\n");
    if (StringUtils.isNotBlank(p.getName())) {
    buf.append(p.getName()).append(",");
    } else {
    buf.append(",");
    }
    if (StringUtils.isNotBlank(p.getMobileNo())) {
    buf.append("\t").append(p.getMobileNo()).append(",");
    } else {
    buf.append(",");
    }
    if (StringUtils.isNotBlank(p.getCustomerno())) {
       buf.append("\t").append(p.getCustomerno()).append(",");
    } else {
                    buf.append(",");
                }
    if (StringUtils.isNotBlank(p.getMemberno())) {
       buf.append("\t").append(p.getMemberno()).append(",");
    } else {
                    buf.append(",");
                }
    if (p.getRegistertime() != null) {
       String date = DateUtils.format(p.getRegistertime(), DateUtils.S_DEFAULT);
       buf.append("\t").append(date).append(",");
    }
    else {
       buf.append(",");
    }
    if (p.getFirststep() != null) {
    String str = p.getPrizeF() != null ? p.getPrizeF() : "";
    buf.append("2元彩票(").append(str).append("),");
    } else {
    buf.append(",");
    }
    if (p.getFirstdate() != null) {
    String date = DateUtils.format(p.getFirstdate(), DateUtils.S_DEFAULT);
    buf.append("\t").append(date).append(",");
    } else {
    buf.append(",");
    }
    if (p.getSecondstep() != null) {
    String str = p.getPrizeS() != null ? p.getPrizeS() : "";
    buf.append("10元彩票(").append(str).append("),");
    } else {
    buf.append(",");
    }
    if (p.getSeconddate() != null) {
    String date = DateUtils.format(p.getSeconddate(), DateUtils.S_DEFAULT);
    buf.append("\t").append(date).append(",");
    } else {
    buf.append(",");
    }
    if (p.getIsthird() != null) {
    if (p.getThirdstep() == -1) {
    buf.append("银条").append(",");
    } else if (p.getThirdstep() != null && !p.getThirdstep().equals("")) {
    String str = p.getPrizeT() != null ? p.getPrizeT() : "";
        buf.append("10元彩票(").append(str).append("),");
    }
    } else {
    buf.append(",");
    }
    if (p.getThirddate() != null) {
    String date = DateUtils.format(p.getThirddate(), DateUtils.S_DEFAULT);
    buf.append("\t").append(date).append(",");
    } else {
    buf.append(",");
    }
    }
    }
   
    try {
       IManagerVo mv = (IManagerVo) getSessionVal(request, Constants.SESSION_MANAGER);
            ManagerLog log = new ManagerLog();
            log.setUsercode(mv.getCode());
            log.setSourceip(getRealIp(request));
            log.setLogtype(ManagerLogService.LOG_DOWN_PRIZE);
            log.setRemark("管理员执行下载中奖信息");
            log.setUsertype(mv.getType().toString());
            this.managerLogService.insert(log);
        } catch (Exception ex) {
            logger.error("插入日志LOG_DOWN_PRIZE失败:{}", ex.getMessage());
        }
   
try {
response.addHeader("Content-Disposition", "attachment;filename=prizeReport.csv");
response.setContentType("application/octet-stream;charset=GBK");
response.setCharacterEncoding("GBK");
response.getWriter().write(buf.toString());
} catch (Exception ex) {
logger.error("导出异常,请求参数:" + JSON.toJSONString(search), ex);
} finally {
try {
           response.getWriter().flush();
response.getWriter().close();
} catch (IOException e) {
logger.error("HttpServletResponse Writer关闭异常", e);
}
}
}
}

0 0
原创粉丝点击