文件下载公共方法 以及调用
2015-12-03 17:15
423 查看
/*
* List<String[]> 传入
* 公共方法
*/
public static boolean exportCsv(File file, List dataList) {
boolean isSuccess = false;
if(file == null || !file.getName().toUpperCase().endsWith(格式)) {
isSuccess = false;
} else {
FileOutputStream out = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
int i = 1;
try {
bw = new BufferedWriter(new FileWriter(file));
for (int j = 0; j < dataList.size(); j++) {
//第一行是标题
String[] data = (String[])dataList.get(j);
String dataStr = "";
for (int k = 0; k < data.length; k++) {
if(k == data.length - 1) {
dataStr = dataStr + data[k];
} else {
dataStr = dataStr + data[k] + ",";
}
}
bw.write(dataStr);
bw.newLine();
}
bw.flush();
isSuccess = true;
} catch(IOException e) {
e.printStackTrace();
isSuccess = false;
} finally {
if(out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return isSuccess;
}
调用
File csvFile = null;
try {
//生成临时文件
csvFile = File.createTempFile("模板名称", "模板格式");
} catch (IOException e) {
e.printStackTrace();
}
List<String[]> dataList = new ArrayList<String[]>();
String[] data = new String[]{"姓名","年龄","编号"}; //文件列名称
dataList.add(data);
CsvUtil.exportCsv(csvFile, dataList);
* List<String[]> 传入
* 公共方法
*/
public static boolean exportCsv(File file, List dataList) {
boolean isSuccess = false;
if(file == null || !file.getName().toUpperCase().endsWith(格式)) {
isSuccess = false;
} else {
FileOutputStream out = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
int i = 1;
try {
bw = new BufferedWriter(new FileWriter(file));
for (int j = 0; j < dataList.size(); j++) {
//第一行是标题
String[] data = (String[])dataList.get(j);
String dataStr = "";
for (int k = 0; k < data.length; k++) {
if(k == data.length - 1) {
dataStr = dataStr + data[k];
} else {
dataStr = dataStr + data[k] + ",";
}
}
bw.write(dataStr);
bw.newLine();
}
bw.flush();
isSuccess = true;
} catch(IOException e) {
e.printStackTrace();
isSuccess = false;
} finally {
if(out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return isSuccess;
}
调用
File csvFile = null;
try {
//生成临时文件
csvFile = File.createTempFile("模板名称", "模板格式");
} catch (IOException e) {
e.printStackTrace();
}
List<String[]> dataList = new ArrayList<String[]>();
String[] data = new String[]{"姓名","年龄","编号"}; //文件列名称
dataList.add(data);
CsvUtil.exportCsv(csvFile, dataList);
相关文章推荐
- 2015杭电新生赛1001搬砖【dfs】
- iOS UITabBarController 典型应用
- QT 相关问题
- 关于神一样存在的csdn-一个bug的修复时间是以年记?而且目前还没解决!!!
- nagios配置,Linux下Nagios的工作原理简介
- HDU3496 Watch The Movie(二维费用背包)
- 剑指offer系列之四:重建二叉树
- C语言跳转到指定地址执行
- IOS--UILable
- HDOJ1495(倒水BFS)
- 实验三 进程调度实验
- hdu 1018 big number
- PHP Fatal error: Cannot use object of type stdClass as array in错误
- THINKPHP限制今天提交的数据不超过几条
- 代码块创建Label
- php提供service总结---wsdl篇
- Struts2-动态方法调用(DMI)
- 使用PHP生成二维码的两种方法(带logo图像)
- 项目适配iOS9 遇到的坑坑娃娃
- class_create