将数据库内容导出为xls文件并传送到web---实现方式显导出为xls,再传送到web,若有直接传送到web,请不吝赐教!

在这个功能实现前,首先要对一些基础知识需要了解

http消息头,请查看这个http://blog.csdn.net/fanyuna/article/details/5568089

实现导出xls需要用到开源的POI,这个相信大家能自己去找到。我再这也附上一个相对较好的教程,不过这个教程只针对写xls,若需要读的话,还需要自己去百度了:

http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html

写好了这些后需要发送文件了,参考例子:

http://blog.csdn.net/xtj332/article/details/25701411


经过上面几个链接的学习,我估计也差不多了,废话不多说了,直接上代码

<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 
<span style="white-space:pre">	</span> * 
<span style="white-space:pre">	</span> * @author <a href="mailto:3313971898@qq.com">Jason</a>
<span style="white-space:pre">	</span> * @date 2016年1月21日
<span style="white-space:pre">	</span> * @param sheetName 表名,excel里的sheet表<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span> * @param headers 表头
<span style="white-space:pre">	</span> * @param cols 需要导出的列,对应headers
<span style="white-space:pre">	</span> * @param data 数据集
<span style="white-space:pre">	</span> * @param response
<span style="white-space:pre">	</span> * @throws IOException 
<span style="white-space:pre">	</span> */
</pre><pre name="code" class="java">@SuppressWarnings("rawtypes")
	public void write(String outPath,String sheetName,String[] headers,String[] cols,List<Map> data,HttpServletResponse response) throws IOException  {
		// 创建工作文档对象
		Workbook wb = new HSSFWorkbook();
		// 创建sheet对象
		Sheet sheet = wb.createSheet(sheetName);
		// 循环写入行数据
		Row row = sheet.createRow(0);
        for (short i = 0; i < headers.length; i++) {
            Cell cell = row.createCell(i);
            cell.setCellValue(headers[i]);
        }
       Iterator<Map> it = data.iterator();
       int index = 0;
       OutputStream stream = null;
       BufferedInputStream in = null;    
       BufferedOutputStream out = null;
       File f=null;
       try {
	       while (it.hasNext()) {
	           index++;
	           row = sheet.createRow(index);
	           Map map = (Map) it.next();           
	           Object value=null;
	           for (short i = 0; i < cols.length; i++) {
	        	   if(map.containsKey(cols[i])){
	        		   value=map.get(cols[i]);
		               String textValue = null;
<span style="white-space:pre">				</span>//排除空指针异常,都转化成String转到excel
		               if(value==null){
		            	   textValue="";
		               }else if (value instanceof Timestamp) {
		                   Timestamp date = (Timestamp) value;
		                   SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		                   textValue = sdf1.format(date);
		               }else if (value instanceof Date) {
		                   Date date = (Date) value;
		                   SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		                   textValue = sdf1.format(date);
		               }else {
		            	   textValue=value.toString();
		               }
		               Cell cell = row.createCell(i); 
		               cell.setCellValue(textValue);
	        	   }else{
	        		   value=null;
	        	   }	           
	           }	
	       }
	       stream = new FileOutputStream(outPath);
			wb.write(stream);
		   f= new File(outPath);  
		   long fLength=0;
		   if (f.exists() && f.isFile()){  
		        fLength=f.length();  
		   }
		   String fileName=outPath.substring(outPath.lastIndexOf("/")+1);
		   response.setContentType("application/vnd.ms-excel");    
		   response.setCharacterEncoding("UTF-8");    
		   response.setHeader("Content-Disposition", "attachment; filename="+fileName);    
		   response.setHeader("Content-Length",String.valueOf(fLength));  
		   in = new BufferedInputStream(new FileInputStream(f));    
		   out = new BufferedOutputStream(response.getOutputStream());    
		   byte[] fileData = new byte[1024];    
		   int len = 0;    
		   while (-1 != (len=in.read(fileData, 0, fileData.length))) {    
		       out.write(fileData, 0, len);    
		   }
		   out.flush();
	        
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(stream!=null){
				stream.close();
			}
			if(in!=null){
				in.close();
			}
			if(out!=null){
				out.close();
			}
			
			System.out.println(f.delete());
			
		}  

          
	}

在这里我用了参数List<map>来封装对象,相对来说灵活一点,当然也可用JavaBean来封装对象。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值