您的位置:首页 > 其它

文件下载公共方法 以及调用

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: