源碼下載
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);
}
}
}