天天看點

Java實作從網頁表格導出CSV檔案的例子

鍓嶅彴缃戦〉浠g爜 聽

<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>绗竴鍏?lt;/th>

聽 聽 聽 聽 聽 聽 聽 聽 <th>鏃堕棿</th>

聽 聽 聽 聽 聽 聽 聽 聽 <th>绗簩鍏?lt;/th>

聽 聽 聽 聽 聽 聽 聽 聽 <th>鏃堕棿</th>

聽 聽 聽 聽 聽 聽 聽 聽 <th>绗笁鍏?lt;/th>

聽 聽 聽 聽 聽 聽 聽 聽 <th>鏃堕棿</th>

聽 聽 聽 聽 聽 聽 聽 聽 </tr>

聽 聽 聽 聽 聽 聽 </thead>

聽 聽 聽 聽 聽 聽 <tbody>

聽 聽 聽 聽 聽 聽 聽聽

聽 聽 聽 聽 聽 聽 </tbody>

聽 聽 聽 聽 聽 </table>

聽 聽 聽 聽 </div>

聽 聽 聽 </div>

聽 聽 </div>

聽 </div>

鍓嶅彴瀵瑰簲js浠g爜

$("#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");

});

鍚庡彴涓氬姟浠g爜

@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;

聽 聽

聽 聽 @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);

}

}

}

}