源码下载
https://download.csdn.net/download/qq_39706570/12524634
分析
1、本项目只集成了导入导出Excel功能,不需要配置相关文件;
2、工具类高内聚封装,没有其他多余组件的集成;
3、适用导出List<Map<String,Object>>和List<Bean>格式的数据;
4、导入导出只需要几行代码。
调用导出List<Map>案例
/**
* @author SargerasWang
*/
package com.sargeraswang.util.ExcelUtil;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
/**
* 以List<Map<String,Object>>作为导出格式
*
* @author SargerasWang
* Created at 2014年9月21日 下午4:38:42
*/
public class TestExportMap {
@Test
public void exportXls() throws IOException {
List<Map<String,Object>> list = new ArrayList<>();
Map<String,Object> map =new LinkedHashMap<>();
map.put("name", "");
map.put("age", "");
map.put("createTime","");
map.put("className","");
Map<String,Object> map2 =new LinkedHashMap<String, Object>();
map2.put("name", "测试是否是中文长度不能自动宽度.测试是否是中文长度不能自动宽度.");
map2.put("age", null);
map2.put("className", null);
map.put("createTime",null);
Map<String,Object> map3 =new LinkedHashMap<String, Object>();
map3.put("name", "张三");
map3.put("age", 12);
map3.put("className", "1班");
map3.put("createTime",new Date());
list.add(map);
list.add(map2);
list.add(map3);
Map<String,String> map1 = new LinkedHashMap<>();
map1.put("name","姓名");
map1.put("age","年龄");
map1.put("createTime","出生日期");
map1.put("className","班级");
String currentTimeMillis = System.currentTimeMillis()+"";
File f= new File("C:\\Users\\Administrator\\Desktop\\"+currentTimeMillis+".xls");
OutputStream out = new FileOutputStream(f);
try {
ExcelUtil.exportExcel(map1,list, out );
} finally {
out.close();
}
}
}
调用导出List<Bean>案例
package com.sargeraswang.util.ExcelUtil;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
public class TestExportBean {
@Test
public void exportXls() throws IOException {
//用排序的Map且Map的键应与ExcelCell注解的index对应
Map<String,String> map = new LinkedHashMap<>();
//这里注意与实体类的@ExcelCell中的index顺序对应。
map.put("name","姓名");
map.put("age","年龄");
map.put("className","班级");
map.put("createTime","出生日期");
Collection<Object> dataset=new ArrayList<Object>();
dataset.add(new Student("", "", "",null));
dataset.add(new Student(null, null, null,null));
dataset.add(new Student("王五", "34", "1班",new Date()));
String currentTimeMillis = System.currentTimeMillis()+"";
File f= new File("C:\\Users\\Administrator\\Desktop\\"+currentTimeMillis+".xls");
OutputStream out=new FileOutputStream(f);
try {
ExcelUtil.exportExcel(map, dataset, out);
} finally {
out.close();
}
}
}
调用导入案例
/**
* @author SargerasWang
*/
package com.sargeraswang.util.ExcelUtil;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;
/**
* 测试导入Excel 97/2003
*/
public class TestImportExcel {
@Test
public void importXls() throws FileNotFoundException {
File f=new File("C:\\Users\\Administrator\\Desktop\\a.xlsx");
InputStream inputStream= new FileInputStream(f);
ExcelLogs logs =new ExcelLogs();
Collection<Map> importExcel ;
try {
importExcel = ExcelUtil.importExcel(Map.class, inputStream, "yyyy/MM/dd HH:mm:ss", logs , 0);
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
for(Map m : importExcel){
System.out.println(m);
}
}
}