/**
* Desc : 生成为CVS文件
* User : RICK
* @param data 源数据List
* @param map csv文件的列表头map
* @param outPutPath 文件路径
* @param fileName 文件名称
* Time : 2017/10/20 13:50
*/
@SuppressWarnings("rawtypes")
public static File createCVSFile(List data, LinkedHashMap map, String outPutPath, String fileName){
File csvFile = null;
BufferedWriter bufferedWriter = null;
try {
File file = new File(outPutPath);
if (!file.exists()){
file.mkdir();
}
//定义文件名格式并创建
csvFile = File.createTempFile(fileName, ".cvs", new File(outPutPath));
System.out.println("csvFile:" + csvFile);
// UTF-8使正确读取分隔符","
bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8"), 1024);
// 写入文件头部
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
bufferedWriter.write((String) propertyEntry.getValue() != null ? new String(
((String) propertyEntry.getValue()).getBytes("UTF-8"), "UTF-8") : "");
if (propertyIterator.hasNext()) {
bufferedWriter.write(",");
}
System.out.println(new String(((String) propertyEntry.getValue()).getBytes("UTF-8"),
"UTF-8"));
}
bufferedWriter.write("\r\n");
// 写入文件内容
for (Iterator iterator = data.iterator(); iterator.hasNext();) {
Object row = (Object) iterator.next();
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
bufferedWriter.write((String) BeanUtils.getProperty(row,
((String) propertyEntry.getKey()) != null? (String) propertyEntry.getKey()
: ""));
if (propertyIterator.hasNext()) {
bufferedWriter.write(",");
}
}
if (iterator.hasNext()) {
bufferedWriter.write("\r\n");
}
}
bufferedWriter.flush();
} catch(Exception e){
e.printStackTrace();
} finally {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
} /**
* Desc : 下载文件
* User : RICK
* @param response
* @param csvFilePath 文件路径
* @param fileName 文件名称
* Time : 2017/10/20 13:53
*/
public static void exportFile(HttpServletResponse response, String csvFilePath, String fileName)
throws IOException {
response.setContentType("application/csv;charset=GBK");
response.setHeader("Content-Disposition",
"attachment; filename=" + new String(fileName.getBytes("GBK"), "ISO8859-1"));
//URLEncoder.encode(fileName, "UTF-8")
InputStream in = null;
try {
in = new FileInputStream(csvFilePath);
int len = 0;
byte[] buffer = new byte[1024];
response.setCharacterEncoding("GBK");
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer)) > 0) {
//out.write(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF });
out.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
System.out.println(e);
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
} /**
* Desc : 删除该目录filePath下的所有文件
* User : RICK
* @param filePath 文件目录路径
* Time : 2017/10/20 13:53
*/
public static void deleteFiles(String filePath) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
files[i].delete();
}
}
}
} /**
* Desc : 删除单个文件
* User : RICK
* @param filePath 文件目录路径
* @param fileName 文件名称
* Time : 2017/10/20 13:54
*/ public static void deleteFile(String filePath, String fileName) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
if (files[i].getName().equals(fileName)) {
files[i].delete();
return;
}
}
}
}
}public static void main(String[] args) {
List exportData = new ArrayList<Map>();
Map row1 = new LinkedHashMap<String, String>();
row1.put("1", "rick");
row1.put("2", "男");
row1.put("3", "20");
row1.put("4", "14000000000");
exportData.add(row1);
row1 = new LinkedHashMap<String, String>();
row1.put("1", "anna");
row1.put("2", "女");
row1.put("3", "23");
row1.put("4", "180123456789");
exportData.add(row1);
LinkedHashMap map = new LinkedHashMap();
map.put("1", "姓名");
map.put("2", "性别");
map.put("3", "年龄");
map.put("4", "手机号"); String path = "D:/export/";
String fileName = DateUtil.getYearMonthDay() + Constant.CharacterType.underline;
File file = CvsUtil.createCVSFile(exportData, map, path, fileName);
String fileName2 = file.getName();
System.out.println("文件最终名称:" + fileName2); }

170808、生成为CVS文件的更多相关文章

  1. BAT 快速删除CVS文件和拷贝最近修改文件的目录结构

    相信大家在操作大量文件的的时候,经常会遇到一些手动很难操作的情况 比如有CVS版本控制下每个文件夹下都有一个CVS文件夹,一个个手工删除肯定很费劲,我们都是懒人,还是用工具解决吧.不用重新写程序,直接 ...

  2. CVS 文件自动移 tag 的 Python 脚本

    CVS 文件自动移 tag 的 Python 脚本 背景 工作中使用的版本管理工具是 CVS,在两次发布中,如果修改的文件比较少,会选择用移 Tag 的方式来生成一个新 Tag 发布.文件比较少的情况 ...

  3. matlab读取cvs文件的几种方法

    matlab读取CVS文件的几种方法: 1,实用csvread()函数   csvread()函数有三种使用方法: 1.M = csvread('filename')2.M = csvread('fi ...

  4. wps直接打开CVS文件会把长串数字订单号最后4位变为0

    WPS打开CVS文件,发现里面的长串数字订单号后4位全变成0了,而且是以科学计数法来显示了 上网查了下,不能直接打开,得先在WPS(OFFICE中也一样)中新建一个空白表格,然后用菜单里的数据导入功能 ...

  5. access 如何导出 cvs 文件?

    三部曲 1 access 数据表导出 excel 表格 2 excel 另存为 *.cvs 格式文件 3 数据库导入 *.cvs 文件

  6. QCustomplot使用分享(八) 绘制图表-加载cvs文件

    目录 一.概述 二.效果图 三.源码讲解 1.源码结构 2.头文件 3.移动游标 4.设置坐标轴矩形个数 5.添加图表数据 6.设置折线图类型 6.其他函数 四.测试方式 1.测试工程 2.测试文件 ...

  7. 使用notepad++/excle快速将cvs文件转换为insert语句技巧以及注意点

    使用notepad++/excle快速将cvs文件转换为insert语句技巧以及注意点 业务场景 最近nc项目经理从第三方弄来了一个300w行的csv文件,让导入数据库做处理,出现了下列问题: csv ...

  8. python py生成为pyc文件

    生成单个pyc文件 python就是个好东西,它提供了内置的类库来实现把py文件编译为pyc文件,这个模块就是 py_compile 模块. 使用方法非常简单,如下所示,直接在idle中,就可以把一个 ...

  9. cvs 文件无法上传debug

    当时文件始终上传不成功时(一般先update后commit): cvs update filename report:move away filename ,it is in the way cvs ...

随机推荐

  1. spark学习系列

    转自: http://www.cnblogs.com/magj2006/p/4316264.html spark 系列文章汇总 源码导读 spark 源码导读1 从spark启动脚本开始 spark ...

  2. JavaScript 中,num = num || 1 这种写法有哪些优缺点?

    语法糖,等价于 if(!!num === false) num=1; 类似于java的 num=null ? 1 : num 以下是来自某位知友的解答: 就是个简写法而已.好处就是短一点,但是其实坏处 ...

  3. e1084. 捕获错误和异常

    All errors and exceptions extend from Throwable. By catching Throwable, it is possible to handle all ...

  4. 对phpexcel的若干补充

    导出excel属性设置 //Include class require_once('Classes/PHPExcel.php'); require_once('Classes/PHPExcel/Wri ...

  5. Loadrunner的Socket脚本关联小技巧

    Socket脚本关联小技巧 我们在socket脚本调试的时候经常会遇到很多问题,比如:socket包中繁杂的二进制编码,socket数据如何进行截取,如何对socket数据包进行参数化等等,以下几点内 ...

  6. struts2 + spring3 + mybatis3 环境搭建

    struts2 + spring3 + mybatis3 1. 框架下载 struts2: http://struts.apache.org/ 下载 struts-2.3.14-all.zip spr ...

  7. delphi中设置listview行高的方法

    第一步.在form中放置一个ImageList: 第二步.将ListView的SmallImages设置为第一步中放置的ImageList: 第三部.将imageList的height设置成自己需要的 ...

  8. asp.net基于windows服务实现定时发送邮件的方法

    本文实例讲述了asp.net基于windows服务实现定时发送邮件的方法.分享给大家供大家参考,具体如下: //定义组件 private System.Timers.Timer time; publi ...

  9. Visual Studio 2013 离线版msdn下载和安装

    Visual Studio 2013出来后,并没有自带msdn安装包,而变成了在线安装msdn,好处是msdn可以随时进行更新,坏处是难道以后每次重新安装系统,都需要重新下载吗,如何解决这个问题呢?本 ...

  10. DeDeCMS织梦的采集教程

    http://www.tuicool.com/articles/VziaEz    dede 第一步.我们打开织梦后台点击采集——采集节点管理——增加新节点 第二步.新增节点-配置网址索引 填写要采集 ...