导出Csv文件思路:1.首先把数据遍历,在服务器端生成Csv文件
2.将服务器端生成的Csv文件提供给客户端下载
注意:如果导出数据中有科学计数法表示:请在导出数据后加上“\t”。
package com.neusoft.util.excel;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.neusoft.common.exception.BaseException;
import com.neusoft.dataCenter.residentManage.model.ResidentBean;
public class ExportCsv
{
private static final Logger logger = Logger.getLogger(ExportCsv.class);
/**
*
* 作用:获得Csv文件
*
* @param residentList
* 需要遍历的结合
* @param dirpath
* 临时文件路径
* @param Prefix
* 临时文件前缀
* @return
* @throws BaseException
*/
public File getCsvFile(List<ResidentBean> residentList, String dirpath, String Prefix) throws BaseException
{
BufferedWriter out = null;
FileOutputStream writerStream = null;
int random = (int) (Math.random() * 1000 + 1);
File excelFile = null;
File dir = new File(dirpath);
if (!dir.exists())
{
dir.mkdirs();
}
try
{
File[] filelist = dir.listFiles();
Date date = new Date();
for (int i = 0; i < filelist.length; i++)
{
long modifytime = filelist[i].lastModified();
long nowdate = date.getTime();
if ((nowdate - modifytime) / 1000 >= 3600)
{
filelist[i].delete();
}
}
} catch (Exception e)
{
logger.info("删除导出居民信息产生的临时文件异常", e);
}
try
{
excelFile = File.createTempFile(Prefix + random, ".csv", dir);
int i = 1;
writerStream = new FileOutputStream(excelFile);
out = new BufferedWriter(new OutputStreamWriter(writerStream, "gb2312"));
out.write("序号,姓名,手机号码,街道名称,社区名称,小区名称,年龄,性别,民族,出生日期,身份证号码,政治面貌,文化程度,地址");// 换成你需要的表头
out.newLine();
Iterator<ResidentBean> resultIterator = residentList.iterator();
while (resultIterator.hasNext())
{
ResidentBean e = resultIterator.next();
StringBuffer tempBuffer = new StringBuffer();
tempBuffer.append(i);
tempBuffer.append(",");
tempBuffer.append(e.getName() == null ? "" : e.getName().trim());
tempBuffer.append(",");
tempBuffer.append(e.getMobile() == null ? "" : e.getMobile().trim() + "\t");
tempBuffer.append(",");
tempBuffer.append(e.getStreetName() == null ? "" : e.getStreetName().trim());
tempBuffer.append(",");
tempBuffer.append(e.getCommunityName() == null ? "" : e.getCommunityName().trim().replace("\r\n", ""));
tempBuffer.append(",");
tempBuffer.append(e.getVillageName() == null ? "" : e.getVillageName().trim());
tempBuffer.append(",");
tempBuffer.append(e.getAge() == null ? "" : e.getAge().trim());
tempBuffer.append(",");
tempBuffer.append(e.getSex() == null ? "" : e.getSex().trim());
tempBuffer.append(",");
tempBuffer.append(e.getNation() == null ? "" : e.getNation().trim());
tempBuffer.append(",");
tempBuffer.append(e.getBirthday() == null ? "" : e.getBirthday().trim() + "\t");
tempBuffer.append(",");
tempBuffer.append(e.getCardNo() == null ? "" : e.getCardNo().trim() + "\t");
tempBuffer.append(",");
tempBuffer.append(e.getNationality() == null ? "" : e.getNationality().trim());
tempBuffer.append(",");
tempBuffer.append(e.getEdu() == null ? "" : e.getEdu().trim());
tempBuffer.append(",");
tempBuffer.append(e.getAddress() == null ? "" : e.getAddress().trim());
out.write(tempBuffer.toString());
out.newLine();
tempBuffer = null;
i++;
}
out.flush();
} catch (IOException e)
{
logger.error("生成居民信息CSV临时文件异常", e);
} finally
{
if (out != null)
{
try
{
out.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
if (writerStream != null)
{
try
{
writerStream.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
return excelFile;
}
/**
* 将服务器端生成的Excel文件提供给客户端下载
*
* @param response
* @param tempFile
*/
public void download(HttpServletResponse response, File tempFile)
{
String filenamedownload = tempFile.toString();
String filenamedisplay = "居民信息.csv";
OutputStream output = null;
FileInputStream fis = null;
try
{
filenamedisplay = URLEncoder.encode(filenamedisplay, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + filenamedisplay);
output = response.getOutputStream();
fis = new FileInputStream(filenamedownload);
byte[] b = new byte[1024];
int i = 0;
while ((i = fis.read(b)) > 0)
{
output.write(b, 0, i);
}
output.flush();
} catch (Exception e)
{
// logger.error("导出居民信息CSV文件异常", e);
} finally
{
if (fis != null)
{
try
{
fis.close();
} catch (IOException e)
{
e.printStackTrace();
}
fis = null;
}
if (output != null)
{
try
{
output.close();
} catch (IOException e)
{
e.printStackTrace();
}
output = null;
}
}
}
}
调用:
//residentList 需要导出的集合,一般是从数据库获得的数据
//exportDirPath 导出到服务器的Csv临时文件路径,一般是spring注入进来
//jumin 导出Csv临时文件前缀
ExportCsv xx = new ExportCsv();
File tempfile = xx.getCsvFile(residentList, exportDirPath, "jumin");
xx.download(response, tempfile);
相关推荐
java导出数据时,如果对数据格式没有特殊要求,为了提高效率可以导出CSV格式的文件,导出CSV文件不支持多个sheet页。
fastadmin 导出csv文件
1.浏览器点击导出按钮,直接下载到本地,服务器不生成临时文件; 2.生成csv,中文无乱码,数据不会科学计数法; 3.支持大数据下载; 4.后台服务java语言实现,前端不限制; 5.前端代码: [removed]void(0)" class=...
1-Gbase导出csv文件教程
使用io流写如文件导出csv文件,使用jsp实现。简单的方法可以推广!
java实现后端数据导出成csv文件并上传到指定的sftp服务器,主要是使用univocity-parsers来做的csv文件导出以及采用jsch来完成csv文件的sftp上传。
1.将数据导成.csv格式,并压缩成.zip格式后,显示下载提示(含保存文件路径) 2.前后台代码都有哦
包含数据库资源 测试需要手动修改TP数据库配置
一段简单的导出CSV文件格式的代码 希望对大家有用
这是一个php导出csv文件无乱码示例,如果你的数据源编码不是gb2312的,可能需要服务器支持iconv函数转换编码.
csv.js导出csv格式文件 CSV.js 是一个简单,超快的用于 CSV 解析和编码的 JavaScript 工具库,兼容浏览器,AMD 和 NodeJS 环境。
sqlserver数据库用,指定查询语句执行后导出指定路径下生成csv文件
详细说明、注释、源码、超级经典 详细说明、注释、源码、超级经典
在ruby中用fastercsv导出csv文件。 给出了主要文件,如有疑问请查google。
大量数据库导出csv,一般的通过PLSQL导出csv文件,但对于数据量较大时,则需要通过数据库内部去调用存储过程去导出,这样效率会高出很多
flex导出csv文件。可以实现flex表格里面的内容导成csv文件
亲测OK,可直接导出excel文件,csv文件;不用指定下载路劲,直接下载到本地,服务器不用生成临时文件。
通过条件查询hbase数据导出csv,文本,html等文件,实现方式:将hbase关联hive,然后将hive数据导入真实表,在将真实表数据导入sql数据库
java使用jdbc将数据库数据导出到csv文件
TIA博途wincc如何将变量归档记录导出为.CSV文件进行查看?