Java实现从网页表格导出CSV文件的例子
2014-12-12 11:04
155 查看
前台网页代码
<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);
}
}
}
}
<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);
}
}
}
}
相关文章推荐
- java实现CSV文件的导入导出操作
- Java编程之CSV文件导入与导出的实现
- 网页端中将表格数据和JSON数据读取(导出)到本地的excel和csv文件中
- 利用vc实现数据表格导出到CSV文件
- java实现xls、csv文件导出或下载
- java 实现csv文件导入导出
- java代码实现CSV文件读取、将数据拆分成多个CSV文件及数据导出到CSV文件
- java实现无组件导出CSV文件
- Java 导出CSV文件及实现web下载CSV
- 用CSV框架导出csv文件实现下载(jsp)
- java实现简单XMPP发送消息和文件的简单例子
- java从数据库导出.csv文件及中文乱码问题
- 如何用java实现在网页中保存html编辑器 编辑好的 html 文件?!(如blog里发文章的时候把要发表的内容以html文件的形式保存到服务器上?)
- 动态创建表格js文件,javascript,Ajax,DHTML动态实现表格的创建,动态读取XML中的文件,读取dom节点的例子。
- 一个用servlet实现导出csv文件的实例
- java 导出成.csv文件的乱码问题
- YAML的Java实现——JYAML基本原理与示例(1)导出数据为YAML格式文件
- 本文实现了一个基于servlet技术的简单的csv文件导出的程序实例。
- PHP 实现导出CSV格式文件
- JAVASCRIPT将网页表格导出EXCEL或WORD文件