1、首先需要准备一个jar包 javacsv.jar
2、编写一个将数据写入csv的方法
/**
* 写入csv
* @param request
* @param rootPath
* @param title
* @param headers
* @param dataset
* @param out
*/
public void exportCSV(HttpServletRequest request,String rootPath, String title, String[] headers,List<LinkedHashMap<String, Object>> dataset, OutputStream out) {
try {
System.out.println(rootPath);
CsvWriter csvWriter = new CsvWriter(rootPath,',', Charset.forName("UTF-8"));
//写入表头信息
csvWriter.writeRecord(headers);
//写入内容信息
for(int k=0;k<dataset.size();k++){
curcount++;
LinkedHashMap<String, Object> infos=dataset.get(k);
String agent_id=infos.get("agent_id").toString();
String extension=infos.get("extension").toString();
String starttime=infos.get("starttime").toString();
String endtime=infos.get("endtime").toString();
String info=infos.get("info").toString();
csvWriter.write(agent_id);
csvWriter.write(extension);
csvWriter.write(starttime);
csvWriter.write(endtime);
csvWriter.write(info);
csvWriter.endRecord();
//导出的进度条信息
double dPercent=(double)curcount/totalCount; //将计算出来的数转换成double
int percent=(int)(dPercent*100); //再乘上100取整
request.getSession().setAttribute("curCount", curcount);
request.getSession().setAttribute("percent", percent); //比如这里是50
request.getSession().setAttribute("percentText",percent+"%");//这里是50%
}
//关闭写入的流
csvWriter.close();
File fileLoad = new File(rootPath);
FileInputStream in = new java.io.FileInputStream(fileLoad);
//每次写入10240个字节
byte[] b = new byte[10240];
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n); //每次写入out1024字节
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3、设置导出的文件的格式和其他的必要信息
/***
* 导出成csv文件
* @param request
* @param response
* @param title
* @param headers
* @param allList
* @return
*/
public synchronized String mapOutToCSV(HttpServletRequest request,
HttpServletResponse response, String title, String[] headers,
List<LinkedHashMap<String, Object>> allList){
totalCount=allList.size();
int len = headers.length;
trace.info("@header length=" + len + " title:" + title);
OutputStream out = null;// 创建一个输出流对象
try {
/* 截取掉表名中多余的部分 */
title = title.replaceAll(":", "-");
title = title.replaceAll("(", "(");
title = title.replaceAll(")", ")");
String subTitle = title;
String filename = new String((subTitle).getBytes("UTF-8"),"iso8859-1");
trace.info("@filename:=" + filename);
out = response.getOutputStream();//
response.setHeader("Content-disposition", "attachment; filename="+ filename + ".csv");// filename是下载的xls的名,建议最好用英文
response.setContentType("application/csv;charset=UTF-8");// 设置类型
response.setHeader("Pragma", "No-cache");// 设置头
response.setHeader("Cache-Control", "no-cache");// 设置头
response.setDateHeader("Expires", 0);// 设置日期头
String rootPath = request.getSession().getServletContext().getRealPath("/")+ filename + ".csv";
exportCSV(request,rootPath, title, headers, allList, out);
out.flush();
} catch (IOException e) {
trace.error("OutToCSV OutputStream " + e.getMessage());
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
trace.error("OutToCSV OutputStream close " + e.getMessage());
}
}
return null;
}
4、后台调用该方法即可实现导出成csv文件
@RequestMapping(value="/excelData.html",method=RequestMethod.GET,params={"fromTime","toTime"})
public Object excelDataRate(HttpServletRequest request,HttpServletResponse response,String fromTime,String toTime){
request.getSession().removeAttribute("totalCount");
request.getSession().removeAttribute("curCount");
request.getSession().removeAttribute("percent");
request.getSession().removeAttribute("percentText");
ArrayList<LinkedHashMap<String, Object>> DataListALL=trans.selectDataList(fromTime, toTime);
String[] headers = {" 员工编号 ", " 分机号 ", " 开始时间 ", "结束时间", "对话信息", };
ExportExcelUtil ex = new ExportExcelUtil();
//return ex.mapOutToExcel(request, response, "语音转文本的信息数据", headers,DataListALL);
return ex.mapOutToCSV(request, response,"语音转文本的信息数据"+fromTime+"至"+toTime, headers, DataListALL);
}
5、前台调用,只要两行代码集合实现
在函数中写入下面两句代码
var url="${pageContext.request.contextPath}/excelData.html?fromTime="+encodeURI(starttime)+"&toTime="+encodeURI(endtime);
window.location.href=url;
该类方法中还存在进度条信息,导出时存在进度条