`
comeon-liming
  • 浏览: 47374 次
  • 性别: Icon_minigender_1
  • 来自: 大连
最近访客 更多访客>>
社区版块
存档分类
最新评论

java导出Excel 玩玩吧!

    博客分类:
  • java
阅读更多
java 代码
  1. /*  
  2.  * Generated by MyEclipse Struts  
  3.  * Template path: templates/java/JavaClass.vtl  
  4.  */  
  5. package com.axon.fable.sams.view.action;   
  6.   
  7. import java.io.IOException;   
  8. import java.io.OutputStream;   
  9. import java.util.List;   
  10.   
  11. import javax.servlet.http.HttpServletRequest;   
  12. import javax.servlet.http.HttpServletResponse;   
  13.   
  14. import jxl.Workbook;   
  15. import jxl.write.WriteException;   
  16. import jxl.write.biff.RowsExceededException;   
  17.   
  18. import org.apache.struts.action.ActionForm;   
  19. import org.apache.struts.action.ActionForward;   
  20. import org.apache.struts.action.ActionMapping;   
  21. import org.hibernate.HibernateException;   
  22. import org.hibernate.Query;   
  23. import org.hibernate.Session;   
  24. import org.hibernate.Transaction;   
  25.   
  26. import com.axon.fable.empolderpackage.out.OutJavaScript;   
  27. import com.axon.fable.empolderpackage.page.Pager;   
  28. import com.axon.fable.empolderpackage.string.MyPublic;   
  29. import com.axon.fable.sams.common.BaseAction;   
  30. import com.axon.fable.sams.exception.AppBusinessException;   
  31. import com.axon.fable.sams.exception.AppSystemException;   
  32.   
  33. /**   
  34.  * MyEclipse Struts  
  35.  * Creation date: 06-28-2007  
  36.  *   
  37.  * XDoclet definition:  
  38.  * @struts.action path="/axon" name="axonForm" input="/samspage/zm/axon.jsp" parameter="method" scope="request" validate="true"  
  39.  * @struts.action-forward name="success" path="/samspage/zm/content.jsp"  
  40.  */  
  41. public class StshipoperationAction extends BaseAction {   
  42.     /*  
  43.      * Generated Methods  
  44.      */  
  45.     private static  Session session=null;   
  46.     private static  Transaction ts=null;   
  47.     private static  Query queryC=null;   
  48.     private static  Query queryR=null;   
  49.     private static  Query query=null;   
  50.        
  51.     private static  List list=null;   
  52.     private static  Integer startRow;   
  53.     private static  Integer ncurrentPage;   
  54.     private static  Integer cell;   
  55.        
  56.     private static  String  property;   
  57.     private static  String  sql;   
  58.        
  59.        
  60.     private static  String type;    
  61.        
  62.     private static  String condition ;//是否导出当前页   
  63.   
  64.     private static  String currentPage;     
  65.     private static  String from ;   
  66.        
  67.        
  68.        
  69.        
  70.     private static  String pactdata;   
  71.     private static  String voyagename;   
  72.     private static  String voyageno;   
  73.     private static  String dwt ;   
  74.     private static  String hirefrom  ;   
  75.        
  76.     private static  String deliveryposion ;   
  77.     private static  String redeliveryposion     ;   
  78.     private static  String sheepowner ;   
  79.     private static  String addr;   
  80.     private static  String addcomm;   
  81.        
  82.     private static  String rent;   
  83.   
  84.     private static  String fileName ;   
  85.     private static  OutputStream os;   
  86.     @Override  
  87.     public ActionForward findAll(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {   
  88.         // TODO Auto-generated method stub   
  89.         return null;   
  90.     }   
  91.   
  92.     @Override  
  93.     public ActionForward findById(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {   
  94.         // TODO Auto-generated method stub   
  95.         return null;   
  96.     }   
  97.   
  98.     @Override  
  99.     public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)  {   
  100.         // TODO Auto-generated method stub   
  101.   
  102.         return null;   
  103.     }   
  104.     public static String strNull(Object nullStr,String newStr,Integer cell){       
  105.            
  106.            
  107.         if(nullStr==null||nullStr.equals("")){return newStr;}else{cell+=1;return nullStr+"";}      
  108.     }   
  109.     public static String getStr(String str,Integer cell){   
  110.         if(str==null||str.trim().equals("")){return "";}else{cell+=1;return ","+str;}      
  111.     }   
  112.     public static String getExcelTile(String title){   
  113.         if(title==null)   
  114.            
  115.         return "";     
  116.             if(title.equals("modela.stsid"))   
  117.                 return "编号";   
  118.             if(title.equals("modelc.pactdata"))   
  119.                 return "合同日期";   
  120.             if(title.equals("modela.voyagename"))   
  121.                 return "航名";   
  122.             if(title.equals("modela.voyageno"))   
  123.                 return "航次";   
  124.             if(title.equals("modelc.dwt"))   
  125.                 return "DWT";   
  126.                
  127.             if(title.equals("modelc.hirefrom"))   
  128.                 return "受载期";              
  129.             if(title.equals("modela.deliveryposion"))   
  130.                 return "交船地点";   
  131.             if(title.equals("modela.redeliveryposion"))   
  132.                 return "还船地点";   
  133.             if(title.equals("modelc.sheepowner"))   
  134.                 return "联系人";   
  135.             if(title.equals("modelc.addr"))   
  136.                 return "经纪人拥金";   
  137.                
  138.             if(title.equals("modelc.addcomm"))   
  139.                 return "ADD COMM";     
  140.             if(title.equals("modelc.rent"))   
  141.                 return "租金";   
  142.             return "";   
  143.            
  144.     }   
  145.     public ActionForward exporVoyagesInfoToExcel(ActionMapping mapping, ActionForm form,   
  146.             HttpServletRequest request, HttpServletResponse response) {   
  147.            
  148.          list=null;   
  149.          startRow=0;   
  150.          ncurrentPage=1;   
  151.          cell=0;   
  152.            
  153.          type                        =request.getParameter("type");    
  154.            
  155.          condition                   =request.getParameter("condition");//是否导出当前页   
  156.        
  157.          currentPage                 =request.getParameter("currentPage");     
  158.          from                        =request.getParameter("from");   
  159.            
  160.            
  161.            
  162.            
  163.          pactdata                    = request.getParameter("modelc.pactdata");   
  164.          voyagename                  = request.getParameter("modela.voyagename");   
  165.          voyageno                    = request.getParameter("modela.voyageno");   
  166.          dwt                         = request.getParameter("modelc.dwt");   
  167.          hirefrom                    = request.getParameter("modelc.hirefrom");   
  168.            
  169.          deliveryposion              = request.getParameter("modela.deliveryposion");   
  170.          redeliveryposion            = request.getParameter("modela.redeliveryposion");   
  171.          sheepowner                  = request.getParameter("modelc.sheepowner");   
  172.          addr                        = request.getParameter("modelc.addr");   
  173.          addcomm                     = request.getParameter("modelc.addcomm");   
  174.            
  175.          rent                        = request.getParameter("modelc.rent");   
  176.            
  177.           
  178.         if(type!=null&&type.trim().equals("1")){   
  179.             type                ="已还船舶--费用未结清";   
  180.         }else{   
  181.             type                ="已还船舶--费用已结清";   
  182.         }   
  183.            
  184.            
  185.         property                =getStr(pactdata,cell)+getStr(voyagename,cell)+getStr(voyageno,cell)+getStr(dwt,cell)+getStr(hirefrom,cell)   
  186.                                 +getStr(deliveryposion,cell)+getStr(redeliveryposion,cell)+getStr(sheepowner,cell)+getStr(addr,cell)+getStr(addcomm,cell)   
  187.                                 +getStr(rent,cell);   
  188.         property                = property.substring(1);   
  189.         String split[]          = property.split(",");   
  190. //        System.out.println("-----------------------------property:"+property);   
  191.         if(currentPage!=null&&!currentPage.trim().equals("")){   
  192.                 ncurrentPage    =Integer.parseInt(currentPage);   
  193.         }else{   
  194.                 OutJavaScript.outString(response, "Sorry! Failed to get information of pager.");   
  195.                 return null;   
  196.        }   
  197.                
  198.                
  199.                
  200.         try {   
  201.                            
  202.             session             =getServiceLocator().getBaseHibernateDAO().getSession();   
  203.             sql                 ="select count(*) "+from;   
  204.   
  205.             query               =session.createQuery(sql);         
  206.             list                = query.list();            
  207.                   
  208.                for (int i = 0; i < list.size(); i++) {             
  209.                totalSize        =(Integer)list.get(i);             
  210.                  if(totalSize!=0){   
  211.                     pager       =new Pager(ncurrentPage,totalSize);     
  212.                  }                 
  213.             }   
  214.                                                
  215.                query            =getServiceLocator().getBaseHibernateDAO().getSession().createQuery("select " +property+from);   
  216.                 if(condition!=null&&condition.trim().equals("1")){//分页数据   
  217.                     startRow    = (ncurrentPage - 1)*pager.getPageSize();   
  218.                     query.setFirstResult(startRow);   
  219.                     query.setMaxResults(pager.getPageSize());              
  220. //                  System.out.println("---------------------------------------------------query:"+query);   
  221.                 }          
  222.              list               = query.list();       
  223.            
  224.         fileName                = "shipInfo";   
  225.        
  226.         os                      = response.getOutputStream();   
  227.         response.reset();           
  228.         response.setHeader("Content-disposition",   
  229.                 "attachment; filename=" +fileName + ".xls");            
  230.         response.setContentType("application/msexcel");   
  231.                    
  232.         jxl.write.WritableWorkbook wbook = Workbook.createWorkbook(os);   
  233.         jxl.write.WritableSheet wsheet = wbook.createSheet("the first sheet"0);    
  234.   
  235.         for (int i = 0; i < split.length; i++) {   
  236.             jxl.write.Label wlabel0;   
  237.             wlabel0 = new jxl.write.Label(i, 0, getExcelTile(split[i]));   
  238.             wsheet.addCell(wlabel0);   
  239.         }          
  240.         jxl.write.Label wlabel1;   
  241.           for(int i=0;i<list.size();i++) {         
  242.               if(split.length==1){   
  243.                   Object strval = (Object) list.get(i);                  
  244.                   String javaScript=""+MyPublic.toHtmlStr(strval==null?"":strval.toString().trim())+"";      
  245.                 wlabel1 = new jxl.write.Label(0, i+1,strval==null?"":strval.toString().trim() );   
  246.                 wsheet.addCell(wlabel1);   
  247.               }else{   
  248.                   Object[] strval = (Object[]) list.get(i);             
  249.                   for(int j=0;j<strval.length;j++) {                 
  250.                       String javaScript=""+MyPublic.toHtmlStr(strval[j]==null?"":strval[j].toString().trim())+"";             
  251.                       //System.out.println("===================script:"+javaScript);                     
  252.                     wlabel1 = new jxl.write.Label(j, i+1,strval[j]==null?"":strval[j].toString().trim() );   
  253.                     wsheet.addCell(wlabel1);   
  254.                  }                   
  255.               }   
  256.                  
  257.         
  258.           }              
  259.            
  260.         wbook.write();    
  261.         response.flushBuffer();   
  262.         wbook.close();         
  263.         os.close();           
  264.         } catch (IOException e) {   
  265.             // TODO Auto-generated catch block   
  266.             OutJavaScript.outString(response, "Sorry! Export Excel exception.");   
  267.             e.printStackTrace();   
  268.         } catch (HibernateException e1) {   
  269.             // TODO Auto-generated catch block   
  270.             OutJavaScript.outString(response, "Sorry! Database exception.");   
  271.             e1.printStackTrace();   
  272.         } catch (AppSystemException e1) {   
  273.             // TODO Auto-generated catch block   
  274.             OutJavaScript.outString(response, "Sorry! System exception.");   
  275.             e1.printStackTrace();   
  276.         } catch (AppBusinessException e1) {   
  277.             // TODO Auto-generated catch block   
  278.             OutJavaScript.outString(response, "Sorry! Database exception.");   
  279.             e1.printStackTrace();   
  280.         } catch (RowsExceededException e) {   
  281.             // TODO Auto-generated catch block   
  282.             OutJavaScript.outString(response, "Sorry! Export Excel exception.");   
  283.             e.printStackTrace();   
  284.         } catch (WriteException e) {   
  285.             // TODO Auto-generated catch block   
  286.             OutJavaScript.outString(response, "Sorry! Export Excel exception.");   
  287.             e.printStackTrace();   
  288.         }   
  289.            
  290.         return null;   
  291.     }   
  292.   
  293.     @Override  
  294.     public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {   
  295.         // TODO Auto-generated method stub   
  296.         return null;   
  297.     }   
  298. }  
上面是我程序中用到的一个示例.有什么疑问大家再讨论吧!
  • jxl.jar (499.4 KB)
  • 描述: 需用到的Excel包
  • 下载次数: 492
分享到:
评论
5 楼 wildeurope 2009-08-18  
楼主代码 看起来很乱 不过还是有实用性的  借鉴下
4 楼 xin163 2008-12-17  
不是一般人谁能看懂?
3 楼 lerous 2008-10-10  
包提供的还不错````至于代码`?````
2 楼 jiyuchonger 2008-10-04  
很垃圾。。。
1 楼 ralfsumahe 2008-06-02  
头晕

相关推荐

Global site tag (gtag.js) - Google Analytics