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());
	             }
	         }
	    }
	}
}

相关文章

一:display:flex布局display:flex是一种布局方式。它即可以...
1. flex设置元素垂直居中对齐在之前的一篇文章中记载过如何...
移动端开发知识点pc端软件和移动端apppc端软件是什么,有哪些...
最近挺忙的,准备考试,还有其他的事,没时间研究东西,快周...
display:flex;把容器设置为弹性盒模型(设置为弹性盒模型之后...
我在网页上运行了一个Flex应用程序,我想使用Command←组合键...