微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flex4 导入Excel文件示例

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
			import mx.rpc.remoting.mxml.RemoteObject;
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			private var fileRefer:FileReference = new FileReference()
			//浏览按钮,选择文件
			private function importClick(event:MouseEvent):void {
				//过滤文件
				var fileType:FileFilter = new FileFilter("Excel","*.xls");
				var fileTypes:Array = new  Array(fileType);
				//导入
				fileRefer.addEventListener(Event.SELECT,parseExcel);
				fileRefer.browse(fileTypes);
			}
			
			private function parseExcel(evt:Event):void {
				//调用Servlet
				var url:URLRequest=new  URLRequest("/web/ExcelUp?loadType=parseExcel");
			    fileRefer.upload(url);
			    //定时器  获取session
			    setTimeout(function():void{
						readfromflex();
					},3000);
			}
			//Remote对象
			private var ro:RemoteObject=new RemoteObject("operdisp_display_excelImportService");
			//获取session
		    private function readfromflex():void {
				ro.showBusyCursor = true;
 				ro.getExcelSession();
				ro.getExcelSession.addEventListener(ResultEvent.RESULT,getExcelResult);
				ro.getExcelSession.addEventListener(FaultEvent.FAULT,setFault); 
   			}
   			//回调
   			private function getExcelResult(e:ResultEvent):void{
   				if (e.result != null) {
   					var arr:ArrayCollection = e.result as ArrayCollection;
   					//打印导入的excel中的数据
	   				log.debug(arr);
		      		//销毁session
					distorySession();
	      		} else {
	      			readfromflex();
	      		}
	    	}
	    	//销毁session
	    	private function distorySession():void {
	    		ro.showBusyCursor = true;
				ro.distorySession();
				ro.distorySession.addEventListener(ResultEvent.RESULT,distorySuccess);
				ro.distorySession.addEventListener(FaultEvent.FAULT,setFault);
	    	}
	    	
	    	private function distorySuccess(e:ResultEvent):void {
	    		
	    	}
			private function setFault(e:FaultEvent):void{
				Alert.yesLabel="确认";
	      	}
		]]>
	</mx:Script>
	<mx:Button label="导入" click="importClick(event)">
	</mx:Button>
</mx:Application>

package demo

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

import javax.servlet.servletexception;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.diskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import demo.invoke.demoServiceInvoke;
import epm.core.model.SysUserInfo;
import epm.sp.priv.user.webapp.util.UserUtil;
@SuppressWarnings("unchecked")
public class ExcelServlet extends HttpServlet {
	
	private static final long serialVersionUID = 678209845731804056L;


	@Override
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws servletexception,IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request,IOException {
        request.setCharacterEncoding("UTF-8");
        String loadType = request.getParameter("loadType");
        try {
            if ("parseExcel".endsWith(loadType)) {
            	parseExcel(request,response);
            }
        }
        catch (Exception e) {
            e.printstacktrace();
        }
    }
    
    //导入excel
	public void parseExcel(HttpServletRequest request,HttpServletResponse response) throws Exception {
        diskFileItemFactory factory = new diskFileItemFactory();
        ServletFileUpload fileUp = new ServletFileUpload(factory);
        fileUp.setSizeMax(1024 * 1025 * 1024);
        List items = fileUp.parseRequest(request);
        Iterator iter = items.iterator();
	    while (iter.hasNext()) {
	         FileItem item = (FileItem) iter.next();
	         if (!item.isFormField()) {
	             InputStream stream = item.getInputStream();
	             ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
	             byte[] b = new byte[1000];
	             int n;
	             while ((n = stream.read(b)) != -1)
	                 out.write(b,n);
	             stream.close();
	             out.close();
	             out.toByteArray();
	             byte[] file = out.toByteArray();
	             try {  
	            	 demoServiceInvoke.Invoke service = demoServiceInvoke.getInstance(request);
		             List list = service.getExcel(file,acOrgNo); 
		             request.getSession().setAttribute("recordInfo",list);
		             request.getSession().setAttribute("error","");
	             } catch(Exception e) {
	            	 request.getSession().setAttribute("recordInfo",null);
	             	 request.getSession().setAttribute("error",e.getMessage());
	             }
	         }
	    }
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐