把数据导入到.CSV文件,用的是opencsv-1.8.jar框架
public static void main(String[] args)
{
try
{
CSVWriter c = new CSVWriter(new FileWriter("d://cc.csv"));
//报表名称
String [] title = {"以下是摘要,表头与内容信息"};
c.writeNext(title);
//得到摘要信息
List<String> sum = summary();
String []ss = new String[sum.size()];
for(int i =0;i<sum.size();i++)
{
String [] temp = {sum.get(i)};
c.writeNext(temp);
}
//摘要与表头之间要有空行
String [] temp = {""};
c.writeNext(temp);
//得到表头信息
Map<String,String> map = head();
String []sm = new String[map.size()];
for(int i =0;i<map.size();i++)
{
sm[i] = map.get(String.valueOf(i));
}
c.writeNext(sm);
//得到数据内容
List<Map<String,Object>> data = initRowdata();
for(int i = 0; i< data.size();i++){
Map li = data.get(i); //得到每一行
String [] tmp = new String[li.size()];
for(int j =0;j<li.size();j++){
tmp[j] = String.valueOf(li.get(String.valueOf(j)));
}
c.writeNext(tmp);
}
c.flush();
c.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private static List<Map<String,Object>> initRowdata(){
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("0", "one");
map.put("1", "two");
map.put("2", "three");
map.put("3", "four");
map.put("4", "five");
map.put("5", "six");
list.add(0, map);
Map<String,Object> map2 = new HashMap<String,Object>();
map2.put("0", "jack");
map2.put("1", "rose");
map2.put("2", "judy");
map2.put("3", "vivi");
map2.put("4", "vick");
map2.put("5", "angel");
list.add(1, map2);
return list;
}
private static List<String> summary(){
List<String> list = new ArrayList<String>();
String a = "时间:2011-3-16";
String b = "条数:156";
String c = "MOLEVEL:BSC";
String d = "TIMELEVEL:DAILY";
String e = "用户:jack";
list.add(0, a);
list.add(1, b);
list.add(2, c);
list.add(3, d);
list.add(4, e);
return list;
}
private static Map<String,String> head(){
Map<String,String> map = new HashMap<String,String>();
map.put("0", "name");
map.put("1", "sex");
map.put("2", "heiht");
map.put("3", "width");
map.put("4", "tele");
map.put("5", "hobi");
return map;
}