首页 > 代码库 > java 操作新建Excel 并插入值
java 操作新建Excel 并插入值
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;
/**
* 导出的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 = http://www.mamicode.com/new ArrayList
java 操作新建Excel 并插入值
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。