/**
* 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. 关于Cocos2d-x头文件的引用

    cocos2d-x 3.10的G:\cocoshome\Cocos2d-x\cocos2d-x-3.10\extensions\GUI\CCControlExtension\CCScale9Sprit ...

  2. am335x LCD参数更改

    /******************************************************************** * am335x LCD参数更改 * * 本文记录am335 ...

  3. atitit.javascript调用java in swt attilax 总结

    atitit.javascript调用java in swt attilax 总结 1. BrowserFunction 简单介绍1 1.1. BrowserFunction 能够分为三类: 1 1. ...

  4. QTableWidget的表头颜色设置

    设置水平和垂直表头的颜色ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{b ...

  5. CentOS运行级别和开机过程

    linux运行级别: 1)0:关机 2)1:单用户 3)2:多用户状态没有网络服务 4)3:多用户状态有网络服务 5)4:系统未使用保留给用户 6)5:图形界面 7)6:系统重启 注:常用运行级别是3 ...

  6. jquery-创建元素和添加子元素

    一.创建新元素 1.使用$函数创建新元素 var $newElement=$('<div><p>段落</p></div>');//创建元素,返回jQue ...

  7. Oracle 添加主键和索引

    数据的主键和索引一般情况下都是必须的,特别是表有大量数据的时候,索引和主键更是必不可少,这样可以提供数据的查询效率: 一.创建表的同时创建主键约束 (1)无命名 create table studen ...

  8. MathType公式波浪线怎么编辑

    数学公式中有很多符号与数学样式,在用手写时是没有问题的,但是很多论文或者期刊中也是需要用到这些符号或者样式的,比如公式波浪线,那么MathType公式波浪线怎么编辑出来呢? 具体操作步骤如下: 1.打 ...

  9. [Scikit-learn] Dynamic Bayesian Network - Kalman Filter

    看上去不错的网站:http://iacs-courses.seas.harvard.edu/courses/am207/blog/lecture-18.html SciPy Cookbook:http ...

  10. POJ 3093 Margaritas(Kind of wine) on the River Walk (背包方案统计)

    题目 Description One of the more popular activities in San Antonio is to enjoy margaritas in the park ...