超简单的java web 项目下载CSV文件的功能实现

web 开发 专栏收录该内容
4 篇文章 0 订阅

首先,JSP页面长这样:

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@ include file="common.jsp"%>
<link rel="stylesheet" type="text/css"
	href="<%=basePath %>js/h-ui/css/H-ui.min.css" />
<script type="text/javascript"
	src="<%=basePath %>js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="<%=basePath %>js/easydate.js"></script>
<script type="text/javascript"
	src="<%=basePath %>js/My97DatePicker/WdatePicker.js"></script>
</head>
<body>
	<div class="text-c" style="padding: 9px; height: auto;" align="left">
		<label>日期范围:</label> <input type="text" class="input-text Wdate"
			id="start" style="height: 25px; width: 120px;"
			οnfοcus="initMy97(this);"> - <input type="text"
			class="input-text Wdate" id="end"
			style="height: 25px;; width: 120px;" οnfοcus="initMy97(this);">

		<label>数据类型:</label> <select id="dataType" name="DataType"
			style="width: 100px;">
			<option value='channel'>频道</option>
			<option value='category'>类型</option>
			<option value='tag'>标签</option>
		</select> <a id="search" οnclick="getData()" class="btn btn-primary">查询</a> <a
			id="download"
			href="/download/downloadCSV?start=2017-06-01&end=2017-06-30&dataType="
			class="btn btn-primary">下载</a>
	</div>
	<table id="myTable" style="width: 98%;"
		class="table table-border table-bordered table-bg table-hover table-sort table-striped table-responsive">
		<thead>
			<tr class="text-c">
				<th>日期</th>
				<th>数据类型</th>
				<th>类型值</th>
			</tr>
		</thead>
	</table>
</body>
<script>


$(function(){
	//初始化时间
    var edate = easydate();
	var edate2 = easydate().calc('day', -4);
	var edate3 = easydate().calc('day', -1);
	var startDate = edate2.format('yyyy-MM-dd');
	var endDate = edate3.format('yyyy-MM-dd');
	document.getElementById("end").value=endDate;
	document.getElementById("start").value=startDate;
	var start=$("#start").val();
	var end=$("#end").val();
	var dataType=$("#dataType option:selected").val();
	var durl = "/download/downloadCSV?start="+start+"&end="+end+"&dataType="+dataType;
	$("#download").attr("href",durl);

});

</script>
</html>

然后Controller长这样:


import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iqiyi.nadou.vangogh.commons.Constants;
import com.iqiyi.nadou.vangogh.entity.HotPointCTR;
import com.iqiyi.nadou.vangogh.service.HotPointCTRService;
import com.iqiyi.nadou.vangogh.utils.PageEntity;
import com.iqiyi.nadou.vangogh.utils.PagingResult;
import com.iqiyi.nadou.vangogh.utils.ResponseMessage;

/**
 * 
 * @author zhangjiamin_sx
 *
 */
@RequestMapping("download")
@Controller
public class DownloadController {
	@RequestMapping("download")
	public String index() {
		return "download";
	}

	@RequestMapping("downloadCSV")
	@ResponseBody
	public void download(HttpServletRequest request, HttpServletResponse response) {
		String start = request.getParameter("start");
		String end = request.getParameter("end");
		String dataType = request.getParameter("dataType");
		int sum = 10;
		PrintWriter pw = null;
		try {
			pw = setDownload(request, response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		pw.println("日期,数据类型,类型值");
		for (int i = 0; i < sum; i++) {
			pw.println("日期" + i + "," + "数据类型" + i + "," + "类型值" + i);
		}
	}

	public PrintWriter setDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 设置csv文件下载头信息
		response.setContentType("text/csv");
		response.setHeader("Content-Disposition", "attachment; filename=\"userDirectory.csv\"");
		response.setCharacterEncoding("utf-8");

		// ************设置csv文件下载头信息**************
		String fileName = "download.csv";
		response.setCharacterEncoding("gbk");
		response.setContentType(request.getServletContext().getMimeType(fileName));
		// **********设置以附件打开***********
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
		// 2、导出csv文件
		PrintWriter pw = response.getWriter();

		return pw;
	}

}

放到你的代码中,就可以看到正常的下载功能啦


                
  • 0
    点赞
  • 0
    评论
  • 3
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

张糖糖

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值