SSM框架使用POI技术导出Excel_第1页
SSM框架使用POI技术导出Excel_第2页
SSM框架使用POI技术导出Excel_第3页
SSM框架使用POI技术导出Excel_第4页
SSM框架使用POI技术导出Excel_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、POI框架是Apache开源的可以导出导入Excel表的,本博客介绍在SSM(Spring+SpringMVC+Mybatis)项目里,如何使用POI框架,导出Excel表这里我们先要去Apache官网下载jar然后,就可以先编程了先提供一个封装的httpservlet请求和添加数据的类java view plain copy 1. public class PageData extends HashMap implements Map  2.    &#

2、160;  3.     private static final long serialVersionUID = 1L;  4.       5.     Map map = null;  6.     HttpServletRequest request;

3、  7.       8.     public PageData(HttpServletRequest request)  9.         this.request = request;  10.         Map 

4、properties = request.getParameterMap();  11.         Map returnMap = new HashMap();   12.         Iterator entries = properties.entrySet().iterator();

5、   13.         Map.Entry entry;   14.         String name = ""    15.         String value 

6、;= ""    16.         while (entries.hasNext()   17.             entry = (Map.Entry) entries.next();   18.  

7、60;          name = (String) entry.getKey();   19.             Object valueObj = entry.getValue();   20.     &

8、#160;       if(null = valueObj)   21.                 value = ""   22.          

9、   else if(valueObj instanceof String)   23.                 String values = (String)valueObj;  24.         &#

10、160;       for(int i=0;i<values.length;i+)   25.                      value = valuesi + ","  26.

11、                  27.                 value = value.substring(0, value.length()-1);   28.  

12、0;          else  29.                 value = valueObj.toString();   30.           &#

13、160;   31.             returnMap.put(name, value);   32.           33.         map = returnMap; 

14、0;34.       35.       36.     public PageData()   37.         map = new HashMap();  38.       39.  &

15、#160;    40.     Override  41.     public Object get(Object key)   42.         Object obj = null;  43.      &#

16、160;  if(map.get(key) instanceof Object)   44.             Object arr = (Object)map.get(key);  45.             obj 

17、= request = null ? arr:(request.getParameter(String)key) = null ? arr:arr0);  46.          else   47.             obj =&

18、#160;map.get(key);  48.           49.         return obj;  50.       51.       52.     public String

19、 getString(Object key)   53.         return (String)get(key);  54.       55.       56.     SuppressWarnings("unchecked") 

20、0;57.     Override  58.     public Object put(Object key, Object value)   59.         return map.put(key, value);  60.      &

21、#160;61.       62.     Override  63.     public Object remove(Object key)   64.         return map.remove(key);  65.    

22、   66.   67.     public void clear()   68.         map.clear();  69.       70.   71.     public boolean contai

23、nsKey(Object key)   72.         / TODO Auto-generated method stub  73.         return map.containsKey(key);  74.       75. &#

24、160; 76.     public boolean containsValue(Object value)   77.         / TODO Auto-generated method stub  78.         return map.co

25、ntainsValue(value);  79.       80.   81.     public Set entrySet()   82.         / TODO Auto-generated method stub  83.   

26、60;     return map.entrySet();  84.       85.   86.     public boolean isEmpty()   87.         / TODO Auto-generated met

27、hod stub  88.         return map.isEmpty();  89.       90.   91.     public Set keySet()   92.         /&

28、#160;TODO Auto-generated method stub  93.         return map.keySet();  94.       95.   96.     SuppressWarnings("unchecked")  97.  

29、60;  public void putAll(Map t)   98.         / TODO Auto-generated method stub  99.         map.putAll(t);  100.     

30、60; 101.   102.     public int size()   103.         / TODO Auto-generated method stub  104.         return map.size(); 

31、 105.       106.   107.     public Collection values()   108.         / TODO Auto-generated method stub  109.      

32、   return map.values();  110.       111.       112.   写个实体类:会员类java view plain copy 1. public class Member   2.       3.  

33、60;  /* 4.      * 会员账号 5.      */  6.     private String memberID;  7.       8.     /* 9.      

34、* 会员密码 10.      */  11.     private String password;  12.       13.     /* 14.      * 会员级别 15.      

35、*/  16.     private String rank;  17.       18.     /* 19.      * 会员积分 20.      */  21.     private 

36、int credit;  22.       23.     /* 24.      * 会员手机号 25.      */  26.     private String phone;  27.    

37、60;  28.     /* 29.      * 会员皮肤 30.      */  31.     private String imgPath;  32.       33.     private&#

38、160;List<GroupPost> postes;  34.   35.     public List<GroupPost> getPostes()   36.         return postes;  37.       38.   

39、;39.     public void setPostes(List<GroupPost> postes)   40.         this.postes = postes;  41.       42.   43.     public&#

40、160;String getMemberID()   44.         return memberID;  45.       46.   47.     public void setMemberID(String memberID)   48.  &

41、#160;      this.memberID = memberID;  49.       50.   51.     public String getPassword()   52.         return password;

42、  53.       54.   55.     public void setPassword(String password)   56.         this.password = password;  57.     

43、0; 58.   59.     public String getRank()   60.         return rank;  61.       62.   63.     public void setRank(St

44、ring rank)   64.         this.rank = rank;  65.       66.   67.     public int getCredit()   68.       &#

45、160; return credit;  69.       70.   71.     public void setCredit(int credit)   72.         this.credit = credit;  73.  

46、0;    74.   75.     public String getPhone()   76.         return phone;  77.       78.   79.     public 

47、void setPhone(String phone)   80.         this.phone = phone;  81.       82.   83.     public String getImgPath()   84.  

48、0;      return imgPath;  85.       86.   87.     public void setImgPath(String imgPath)   88.         this.imgPath =

49、0;imgPath;  89.       90.       91.   DAO.javajava view plain copy 1. package com.appweb.core.dao;  2.   3. public interface DAO   4.    

50、60;  5.     /* 6.      * 保存对象 7.      * param str 8.      * param obj 9.      * return 10.     

51、 * throws Exception 11.      */  12.     public Object save(String str, Object obj) throws Exception;  13.       14.     /* 1

52、5.      * 修改对象 16.      * param str 17.      * param obj 18.      * return 19.      * throws Exception 20. 

53、60;    */  21.     public Object update(String str, Object obj) throws Exception;  22.       23.     /* 24.      * 删除对象&

54、#160; 25.      * param str 26.      * param obj 27.      * return 28.      * throws Exception 29.      */ 

55、0;30.     public Object delete(String str, Object obj) throws Exception;  31.   32.     /* 33.      * 查找对象 34.      * param str

56、60;35.      * param obj 36.      * return 37.      * throws Exception 38.      */  39.     public Object findForObject(S

57、tring str, Object obj) throws Exception;  40.   41.     /* 42.      * 查找对象 43.      * param str 44.      * param obj

58、0;45.      * return 46.      * throws Exception 47.      */  48.     public Object findForList(String str, Object obj) throws Exception

59、;  49.       50.     /* 51.      * 查找对象封装成Map 52.      * param s 53.      * param obj 54.      

60、* return 55.      * throws Exception 56.      */  57.     public Object findForMap(String sql, Object obj, String key , String value) throws

61、 Exception;  58.       59.   DAOSupport类:java view plain copy 1. package com.appweb.core.dao;  2.   3. import java.util.List;  4.   5. import javax.annotation.Resource; &

62、#160;6.   7. import org.apache.ibatis.session.ExecutorType;  8. import org.apache.ibatis.session.SqlSession;  9. import org.apache.ibatis.session.SqlSessionFactory;  10. import org.mybatis.spring.SqlSessionTemplate;  11. import

63、60;org.springframework.stereotype.Repository;  12.   13. Repository("daoSupport")  14. public class DaoSupport implements DAO   15.   16.     Resource(name = "sqlSessionTemplate&qu

64、ot;)  17.     private SqlSessionTemplate sqlSessionTemplate;  18.       19.     /* 20.      * 保存对象 21.      * param str 

65、22.      * param obj 23.      * return 24.      * throws Exception 25.      */  26.     public Object save(String s

66、tr, Object obj) throws Exception   27.         return sqlSessionTemplate.insert(str, obj);  28.       29.       30.     /*

67、60;31.      * 批量更新 32.      * param str 33.      * param obj 34.      * return 35.      * throws Exception 36.

68、      */  37.     public Object batchSave(String str, List objs )throws Exception  38.         return sqlSessionTemplate.insert(str, objs);  

69、;39.       40.       41.     /* 42.      * 修改对象 43.      * param str 44.      * param obj 45.  

70、    * return 46.      * throws Exception 47.      */  48.     public Object update(String str, Object obj) throws Exception   

71、;49.         return sqlSessionTemplate.update(str, obj);  50.       51.   52.     /* 53.      * 批量更新 54.      *

72、0;param str 55.      * param obj 56.      * return 57.      * throws Exception 58.      */  59.     public void

73、0;batchUpdate(String str, List objs )throws Exception  60.         SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();  61.         

74、;/批量执行器  62.         SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);  63.         try  64.         

75、60;   if(objs!=null)  65.                 for(int i=0,size=objs.size();i<size;i+)  66.                

76、     sqlSession.update(str, objs.get(i);  67.                   68.                 sqlSes

77、sion.flushStatements();  69.                 sqlSmit();  70.                 sqlSession.clearCache();  71. 

78、60;             72.         finally  73.             sqlSession.close();  74.       &

79、#160;   75.       76.       77.     /* 78.      * 批量更新 79.      * param str 80.      * param

80、60;obj 81.      * return 82.      * throws Exception 83.      */  84.     public Object batchDelete(String str, List objs )throws 

81、Exception  85.         return sqlSessionTemplate.delete(str, objs);  86.       87.       88.     /* 89.      * 删除对象

82、  90.      * param str 91.      * param obj 92.      * return 93.      * throws Exception 94.      */ 

83、60;95.     public Object delete(String str, Object obj) throws Exception   96.         return sqlSessionTemplate.delete(str, obj);  97.      

84、0;98.        99.     /* 100.      * 查找对象 101.      * param str 102.      * param obj 103.      *

85、0;return 104.      * throws Exception 105.      */  106.     public Object findForObject(String str, Object obj) throws Exception   107.   &

86、#160;     return sqlSessionTemplate.selectOne(str, obj);  108.       109.   110.     /* 111.      * 查找对象 112.      * param

87、60;str 113.      * param obj 114.      * return 115.      * throws Exception 116.      */  117.     public Object f

88、indForList(String str, Object obj) throws Exception   118.         return sqlSessionTemplate.selectList(str, obj);  119.       120.       12

89、1.     public Object findForMap(String str, Object obj, String key, String value) throws Exception   122.         return sqlSessionTemplate.selectMap(str, obj,&

90、#160;key);  123.       124.       125.   写个Service类:java view plain copy 1. /* 2.      * 会员信息列表 3.      * param pd 4.  &

91、#160;   * return 5.      * throws Exception 6.      */  7.     public List<PageData> listM(PageData pd)throws Exception  8.    &#

92、160;    return (List<PageData>)dao.findForList("MemberMapper.memberList", pd);  9.       ObjectExcelView.java:java view plain copy 1. package com.appweb.core.view;  2.   3. i

93、mport java.util.Date;  4. import java.util.List;  5. import java.util.Map;  6.   7. import javax.servlet.http.HttpServletRequest;  8. import javax.servlet.http.HttpServletResponse;  9.   10. import or

94、g.apache.poi.hssf.usermodel.HSSFCell;  11. import org.apache.poi.hssf.usermodel.HSSFCellStyle;  12. import org.apache.poi.hssf.usermodel.HSSFFont;  13. import org.apache.poi.hssf.usermodel.HSSFSheet;  14. import org.apache.poi.hssf.usermode

95、l.HSSFWorkbook;  15. import org.springframework.web.servlet.view.document.AbstractExcelView;  16.   17. import com.appweb.core.entity.PageData;  18. import com.appweb.core.utils.Tools;  19.   20. /* 21. * 导入到EXCEL&#

96、160;22. * 类名称:ObjectExcelView.java 23. * 类描述:  24.  */  25. public class ObjectExcelView extends AbstractExcelView  26.   27.     Override  28.     protected void

97、60;buildExcelDocument(Map<String, Object> model,  29.             HSSFWorkbook workbook, HttpServletRequest request,  30.            &

98、#160;HttpServletResponse response) throws Exception   31.         / TODO Auto-generated method stub  32.         Date date = new Date();

99、0; 33.         String filename = Tools.date2Str(date, "yyyyMMddHHmmss");  34.         HSSFSheet sheet;  35.         HSSF

100、Cell cell;  36.         response.setContentType("application/octet-stream");  37.         response.setHeader("Content-Disposition", "attachment;filename="+filen

101、ame+".xls");  38.         sheet = workbook.createSheet("sheet1");  39.           40.         List<String> tit

102、les = (List<String>) model.get("titles");  41.         int len = titles.size();  42.         HSSFCellStyle headerStyle = workbook.createCel

103、lStyle(); /标题样式  43.         headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  44.         headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  45.    &

104、#160;    HSSFFont headerFont = workbook.createFont();    /标题字体  46.         headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  47.         head

105、erFont.setFontHeightInPoints(short)11);  48.         headerStyle.setFont(headerFont);  49.         short width = 20,height=25*20;  50.       

106、0; sheet.setDefaultColumnWidth(width);  51.         for(int i=0; i<len; i+) /设置标题  52.             String title = titles.get(i); 

107、60;53.             cell = getCell(sheet, 0, i);  54.             cell.setCellStyle(headerStyle);  55.       &#

108、160;     setText(cell,title);  56.           57.         sheet.getRow(0).setHeight(height);  58.           59. 

109、0;       HSSFCellStyle contentStyle = workbook.createCellStyle(); /内容样式  60.         contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  61.       

110、0; List<PageData> varList = (List<PageData>) model.get("varList");  62.         int varCount = varList.size();  63.         for(int i

111、=0; i<varCount; i+)  64.             PageData vpd = varList.get(i);  65.             for(int j=0;j<len;j+)  66. &#

112、160;               String varstr = vpd.getString("var"+(j+1) != null ? vpd.getString("var"+(j+1) : ""  67.      

113、60;          cell = getCell(sheet, i+1, j);  68.                 cell.setCellStyle(contentStyle);  69.     

114、60;           setText(cell,varstr);  70.               71.               72.    

115、       73.           74.       75.   76.   控制类导出Excel表:java view plain copy 1. /* 2.      * 导出Excel 3.      * return 4.    &#

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论