码迷,mamicode.com
首页 > 编程语言 > 详细

java 操作新建Excel 并插入值

时间:2014-09-12 19:10:44      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:cvs   excel   poi   

public class test {
/**
* 导出的CVS文件
* @param exportData   要导入Excel  的数据集合
* @param rowMapper     Excel 表头部分
* @param outputPath    Excel 文件的保存位置
* @return              Excel文件
*/
@SuppressWarnings("rawtypes")
    public static File createCSVFile(List exportData,LinkedHashMap rowMapper,String outputPath){
File csvFile = null;
BufferedWriter csvFileoutputStream = null;
try {
String temp="数据动态更新情况对比分析统计表_";
csvFile = File.createTempFile(temp, ".csv",new File(outputPath));
//GB2312使正确读取分隔符“,”
csvFileoutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile),"GB2312"),1024);
//写入文件头部
for(Iterator propertyIterator =rowMapper.entrySet().iterator();propertyIterator.hasNext();){
java.util.Map.Entry propertyEntery = (java.util.Map.Entry)propertyIterator.next();
csvFileoutputStream.write(propertyEntery.getValue().toString());
if(propertyIterator.hasNext()){
csvFileoutputStream.write(",");
}
}
//换行
csvFileoutputStream.newLine();
//写入文件内容
for(Iterator iterator = exportData.iterator();iterator.hasNext();){
Object row = iterator.next();
for (Iterator propertyIterator = rowMapper.entrySet().iterator();propertyIterator.hasNext();) {
java.util.Map.Entry propertyEntry= (java.util.Map.Entry)propertyIterator.next();
try {
csvFileoutputStream.write(BeanUtils.getProperty(row, String.valueOf(propertyEntry.getKey())));
if(propertyIterator.hasNext()){
csvFileoutputStream.write(",");
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
if(iterator.hasNext()){
csvFileoutputStream.newLine();
}
}
csvFileoutputStream.flush();

} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
csvFileoutputStream.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return csvFile;

}



/**
* 测试类
* @param args
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
 //Excel 表头 map 是Excel的表头部分
LinkedHashMap map = new LinkedHashMap();
map.put("1", "序号");
map.put("2", "法院名称");
map.put("3", "管辖法院数");
map.put("4", "有数据的法院数");
map.put("5","自动汇聚案件量");
map.put("6","成功入库案件量");
map.put("7","入库失败案件量");
map.put("8","当日新收");
map.put("9", "当日已结");
map.put("10","法院数");
map.put("11","当日新收");
map.put("12","当日已结");
map.put("13", "有数据的法院数");  
 
/**
* exportData Excel数据集合
* 注意row的单元格cell的个数和表头的长度一致(但不强制要求)
*/
List<Map> exportData = new ArrayList<Map>();   //Excel数据集合

Map row1 = new LinkedHashMap<String,String>();  //每一条数据,要保持和表头统一(循环出来,依次添加到exporData中)
row1.put("1", "10001"); //row("cell单元格的索引,从0开始","单元格中存放的值");
row1.put("2", "北京高院");
row1.put("3", "2000");
row1.put("4", "200");
row1.put("5", "451");
row1.put("6", "45612");
row1.put("7", "59525");
row1.put("8", "8518");
row1.put("9", "1569");
row1.put("10", "235");
row1.put("11", "125");
row1.put("12", "125");
row1.put("13", "125");
exportData.add(row1);
test.createCSVFile(exportData, map, "F:/");

}
}


java 操作新建Excel 并插入值

标签:cvs   excel   poi   

原文地址:http://blog.csdn.net/u011598529/article/details/39233557

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!