241. * @param outputPath: Excel文件保存路径 242. */
243. private static void saveExcelFile(HSSFWorkbook workbook, String outputPath) { 244. try {
245. FileOutputStream fos = new FileOutputStream(outputPath);
246. workbook.write(fos); 247.
248. fos.flush(); 249. fos.close();
250. } catch (IOException e) { 251. e.printStackTrace(); 252. } 253. } 254. }
测试类POITest.java Java代码
1. package com.lightgjc1.test; 2.
3. import java.io.File;
4. import java.util.ArrayList; 5. import java.util.Date; 6. import java.util.List; 7. import java.util.Map; 8.
9. import junit.framework.TestCase; 10.
11.import com.lightgjc1.domain.Area; 12.import com.lightgjc1.domain.Employee; 13.import com.lightgjc1.domain.OrgType;
14.import com.lightgjc1.domain.Organization; 15.import com.lightgjc1.poi.MyPOI; 16.
17.public class POITest extends TestCase {
18. public void testImportAndExportExcel(){
19. String strKeys = \tHomeplace,setOrg,setContactTel\
20. List
21.
22. String[] keys = strKeys.split(\23. for(Map map : listMap) {
24. for(int i = 0; i < keys.length; i++) {
25. System.out.print(map.get(keys[i]) +\26. }
27. System.out.println();
28. System.out.println(\29. } 30.
31. String title = \员工表\
32. String strTitle = \员工代码,员工姓名,性别,出生日期,籍贯,所属机构,机构类型\
33. String strBody = \tHomeplace,setOrg,setContactTel\
34. String outputPath = \
35. MyPOI.exportExcelByMap(listMap, title, strTitle, strBody, outputPath); 36. } 37.
38. public void testExportExcel(){ 39. List objList = initData();
40. Class objClass = Employee.class; 41. String title = \员工表\
42. String strTitle = \员工代码,员工姓名,性别,出生日期,籍贯,所属机构,机构类型\
43. String strBody = \tHomeplace.getName,getOrg.getShortName,getOrg.getOrgType.getName\
44. String outputPath = \45.
46. MyPOI.exportExcelByObject(objList, objClass, title, strTitle, strBody, outputPath); 47. } 48.
49. private List initData(){
50. List empList = new ArrayList(); 51.
52. Employee emp = null;
53. for(int i = 0; i < 10; i++) { 54. emp = new Employee(); 55. emp.setCode(i +\号\56. emp.setName(i +\号\
57. emp.setSex(i % 2 == 0 ? \女\男\
58. emp.setBirthday(new Date()); 59. 60.
61. Area area = new Area(); 62. area.setName(i +\市\63. emp.setHomeplace(area); 64. 65.
66. OrgType orgType = new OrgType(); 67. orgType.setName(\机构类型\68.
69. Organization org = new Organization(); 70. org.setOrgType(orgType);
71. org.setShortName(\机构\72.
73. emp.setOrg(org); 74. 75.
76. empList.add(emp); 77. } 78.
79. return empList; 80. } 81.}
其他工具类(存放数据的实体类) Employee.java 用来存放员工数据 Java代码
1. package com.lightgjc1.domain; 2.
3. import java.util.Date; 4.
5. public class Employee { 6.
7. private String id; 8.
9. private String code; 10.
11. private String name; 12.
13. private String sex; 14.
15. private Date birthday; 16.
17. private String email; 18.
19. private String contactTel; 20.
21. private String zjm; 22.
23. private Organization org = new Organization(); 24.
25. private Area homeplace; 26.
27. public String getId() { 28. return id; 29. } 30.
31. public void setId(String id) { 32. this.id = id; 33. } 34.
35. public String getCode() { 36. return code; 37. } 38.
39. public void setCode(String code) { 40. this.code = code; 41. } 42.
43. public String getName() { 44. return name; 45. } 46.
47. public void setName(String name) { 48. this.name = name; 49. } 50.
51. public String getSex() { 52. return sex; 53. } 54.
55. public void setSex(String sex) { 56. this.sex = sex;
57. } 58.
59. public Date getBirthday() { 60. return birthday; 61. } 62.
63. public void setBirthday(Date birthday) { 64. this.birthday = birthday; 65. } 66.
67. public String getEmail() { 68. return email; 69. } 70.
71. public void setEmail(String email) { 72. this.email = email; 73. } 74.
75. public String getContactTel() { 76. return contactTel; 77. } 78.
79. public void setContactTel(String contactTel) { 80. this.contactTel = contactTel; 81. } 82.
83. public String getZjm() { 84. return zjm; 85. } 86.
87. public void setZjm(String zjm) { 88. this.zjm = zjm; 89. } 90.
91. public Organization getOrg() { 92. return org; 93. } 94.
95. public void setOrg(Organization org) { 96. this.org = org; 97. } 98.
99. public Area getHomeplace() { 100. return homeplace;