java动态生成.csv文件,不好的清指出

iso_wangjian 2011-04-21 10:39:54

package com.huawei.csv;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CreaterCSV {

public static void CreateCsvfiles(String targetPath, String filename,
List<ValueBean> fieldList, List<? extends Object> list){
File file = null;
OutputStream out = null;
PrintWriter pw = null;
try {

file = new File(targetPath + filename + ".csv");
out = new FileOutputStream(file);
pw = new PrintWriter(out);

int fieldListLength = fieldList.size();
int length = list.size();
StringBuilder sb = new StringBuilder();
String[] fields = new String[fieldListLength];
for(int i = 0 ;i < fieldListLength;i++){
ValueBean vb = fieldList.get(i);
sb.append(vb.getValue())
.append(",");
fields[i] = vb.getKey();
}
sb.append("\n");
Map<String,Method> methodMap = new HashMap<String, Method>();

for (int i = 0; i < length; i++) {
Object obj = list.get(i);
for(int j = 0 ;j < fieldListLength; j++){
String fieldName = fields[j];
Method method = methodMap.get(fieldName);
if(method == null){
method = obj.getClass().getMethod(fieldName, new Class[] {});
methodMap.put(fieldName, method);
}

sb.append(method.invoke(obj, new Object[]{}))
.append(",");
}
sb.append("\n");
}
pw.append(sb.toString());
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
System.out.println(targetPath + filename + ".csv生成成功");
try {
pw.flush();
pw.close();
out.flush();
out.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}

public static void CreateCsvfiles(String targetPath, String filename,
String title[], List<JobBean> list) throws Exception {
File file = null;
OutputStream out = null;
PrintWriter pw = null;
try {
file = new File(targetPath + filename + ".csv");
out = new FileOutputStream(file);
pw = new PrintWriter(out);
for (int j = 0; j < title.length; j++) {
pw.append(title[j] + ",");
}
for (int i = 0; i < list.size(); i++) {
String operationname = (list.get(i).getPaerationname());
String operationresult = (list.get(i).getOperationresult());
String username = (list.get(i).getUsername());
String userip = (list.get(i).getUereip());
String operationtime = (list.get(i).getOperationtime());
String operationdetail = (list.get(i).getOperationdetil());
pw.append("\n");
pw.append(operationname + ",");
pw.append(operationresult + ",");
pw.append(username + ",");
pw.append(userip + ",");
pw.append(operationtime + ",");
pw.append(operationdetail + ",");
}
} catch (Exception e) {
// TODO: handle exception
} finally {
System.out.println(targetPath + filename + ".csv生成成功");
pw.flush();
pw.close();
out.flush();
out.close();
}
}

public static void main(String[] args) throws IOException {
try {
String path = "e://789/";
Date df = new Date();
SimpleDateFormat sm = new SimpleDateFormat("yyyyMMddHHmmss");
String filename = "job_dump_" + sm.format(df);
System.out.println(filename);
List<JobBean> list = new ArrayList<JobBean>();
for (int i = 0; i < 5; i++) {
JobBean job = new JobBean();
job.setPaerationname("备份静态数据");
job.setOperationresult("成功");
job.setUsername("admin");
job.setOperationtime("20101-11-11 11:11:11");
job.setOperationdetil("备份单板");
job.setUereip("10.71.138.167");
list.add(job);
}
//CreaterCSV.CreateCsvfiles(path, filename, title, list);
CreaterCSV.CreateCsvfiles(path, filename, JobBean.getList(), list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}



package com.huawei.csv;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class JobBean {
private String paerationname;
private String username;
private String operationtime;
private String uereip;
private String operationdetil;
private String operationresult;

private Map<String,String> map;

public static List<ValueBean> getList(){
List<ValueBean> list = new ArrayList<ValueBean>();
list.add(new ValueBean("getPaerationname", "操作名称 "));
list.add(new ValueBean("getOperationresult", "操作结果"));
list.add(new ValueBean("getUsername", "用户名称"));
list.add(new ValueBean("getOperationtime", "操作时间"));
list.add(new ValueBean("getUereip", "用户IP"));
list.add(new ValueBean("getOperationdetil", "操作详情"));

return list;
}
public String getPaerationname() {
return paerationname;
}
public void setPaerationname(String paerationname) {
this.paerationname = paerationname;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getOperationtime() {
return operationtime;
}
public void setOperationtime(String operationtime) {
this.operationtime = operationtime;
}
public String getUereip() {
return uereip;
}
public void setUereip(String uereip) {
this.uereip = uereip;
}
public String getOperationdetil() {
return operationdetil;
}
public void setOperationdetil(String operationdetil) {
this.operationdetil = operationdetil;
}
public String getOperationresult() {
return operationresult;
}
public void setOperationresult(String operationresult) {
this.operationresult = operationresult;
}
}

package com.huawei.csv;

public class ValueBean {

String key;
String value;

public ValueBean(){}
public ValueBean(String key,String value){
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}

}
...全文
86 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
liang8305 2011-04-22
  • 打赏
  • 举报
回复
逻辑及其混乱
过程及其多此一举
还有很多蛋疼的赋值操作.....

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧