2013-10-01 45 views
0

我想從50個excel文件中刪除一行,爲此我製作了一個flex工具來瀏覽文件夾,找到xls文件並然後在它上面執行操作。我在這裏面臨兩個問題: 1.我有一個循環在文件夾上迭代,一旦找到xls,它應該調用一個函數來刪除該行並返回for循環並繼續其他Excel文件夾中的文件。但它並沒有這樣做。Flex:無法從文件夾中加載Excel文件並更新它們(刪除)

2.我無法在excel中刪除和行。我正在使用as3xls與excel一起工作。在此我將該行的值設置爲空白,因爲我不需要知道如何刪除它。

我是新來flex,所以請幫忙。

enter code here 

<?xml version="1.0" encoding="utf-8"?> 
<mx:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" 
    creationComplete="onCreate(event)"> 


     <fx:Script> 

       <![CDATA[ 
       import com.as3xls.xls.Cell; 
       import com.as3xls.xls.ExcelFile; 
       import com.as3xls.xls.Sheet; 

       import flash.filesystem.FileMode; 
       import flash.utils.Timer; 
       //  import flash.events.TimerEvents; 



       import mx.collections.ArrayCollection; 
       import mx.controls.Alert; 

       private var sheet:Sheet; 
       private var interval: uint; 
       private var loadedFile:ByteArray; 

        function trigger():void { setTimeout(doIt, 10000); } 
        function doIt():void { } 


       private function onCreate(e:Event):void 
       { 

        var fileDirectry:File = File.documentsDirectory.resolvePath("D:/temp"); 
        var excelFile:File = null; 
        var files:Array = fileDirectry.getDirectoryListing(); 
        for(var i:int = 0; i < files.length; i++){ 
          var temp:File = files[i]; 
          if(temp.extension == "xls"){ 
            excelFile = temp; 
            break; 

            //var request:URLRequest = new URLRequest(excelFile.nativePath); 
            //var urlLoader:URLLoader = new URLLoader(request); 
            //urlLoader.addEventListener(Event.COMPLETE, onURLLoaderComplete); // Once file loaded, function call onURLLoaderComplete 
            //urlLoader.dataFormat = URLLoaderDataFormat.BINARY; // to Read Data in Binary Format 
            //urlLoader.load(request); 
            //trigger(); 
        //  interval=setTimout(onCreate(event) ,200); 


          } 
        } 
        var request:URLRequest = new URLRequest(excelFile.nativePath); 
        var urlLoader:URLLoader = new URLLoader(request); 
        urlLoader.addEventListener(Event.COMPLETE, onURLLoaderComplete); // Once file loaded, function call onURLLoaderComplete 
        urlLoader.dataFormat = URLLoaderDataFormat.BINARY; // to Read Data in Binary Format 
        urlLoader.load(request); 


       } 
       private function onURLLoaderComplete(event:Event):void 
       { 
       loadedFile = event.target.data; 
       var excelFile:ExcelFile = new ExcelFile(); 
       excelFile.loadFromByteArray(loadedFile); 
       sheet = excelFile.sheets[0]; // Reads sheet1 
       //trace(sheet.getCell(1,1).value); 
       //Alert.show(sheet.getCell(0,0).value)// getCell(Row, Col) 

       var rows:int = sheet.rows; 
       var cols:int = sheet.cols; 
       for(var i:int = 0; i < rows; i++){ 
        for(var j:int = 0; j < cols; j++){ 
          if(sheet.getCell(i,j).toString() == "second" || sheet.getCell(i,j).toString() == "Second") 
          { 
            Alert.show(sheet.getCell(i,j-1).value) 
             for (var k:int =0;k< cols;k++){ 
             sheet.setCell(i,k,''); 



             } 

             excelFile.sheets.addItem(sheet); 
             var ba:ByteArray = excelFile.saveToByteArray(); 
             var fr:FileReference = new FileReference(); 
             fr.save(ba,"SampleExport1.xls"); 


             //sheet. 
            //TypeLib name and TypeDef Id 
          } 
        } 
       }    
       //Alert.show(sheet.getCell(0,0).value) 
       //return ; 
       //DG.dataProvider=sheet.values; // Imports all excel cells to Datagrid 
       } 





       ]]> 
     </fx:Script> 




     <fx:Declarations> 
       <!-- Place non-visual elements (e.g., services, value objects) here --> 
     </fx:Declarations> 
</mx:WindowedApplication> 

回答

0

問題是加載是一個異步操作,所以你需要創建某種系統來暫停循環的處理,直到加載完成。像這樣的東西應該工作:

public class ProcessFilesCommand 
{ 
    private var _files:Array; 

    private var _index:int = 0; 

    public function processFiles(path:String):void 
    { 
     _index = 0; 
     var fileDirectry:File = File.documentsDirectory.resolvePath(path); 
     _files = fileDirectry.getDirectoryListing(); 

     if(!_files || _files.length == 0) 
      return; //or dispatch a complete event 

     processFileAt(0); 
    } 

    private function cleanFile(data:Object):void 
    { 
     //do your excel stuff here 
    } 

    private function processFileAt(index:int):void 
    { 
     trace("ProcessFilesCommand.processFileAt(" + index + ")"); 

     if(index >= _files.length) 
     { 
      //maybe a good spot to dispatch a complete event... 
      return; 
     } 

     var file:File = File(_files[_index]); 

     if(file.isDirectory || file.extension == null || file.extension.toLowerCase() != "xls") 
     { 
      processFileAt(++_index); 
     } 
     else 
     { 
      var request:URLRequest = new URLRequest(file.nativePath); 
      var loader:URLLoader = new URLLoader(request); 
      loader.addEventListener(Event.COMPLETE, loader_completeHandler); // Once file loaded, function call onURLLoaderComplete 
      loader.dataFormat = URLLoaderDataFormat.BINARY; // to Read Data in Binary Format 
      loader.load(request); 
     } 
    } 

    private function loader_completeHandler(event:Event):void 
    { 
     trace("ProcessFilesCommand.loader_completeHandler(event)"); 

     cleanFile(event.target.data); 
     processFileAt(++_index) 
    } 
} 
+0

感謝您的答覆。但仍然沒有解決問題.Bean的loader_completeHandler函數仍然被調用一次。我想這不會產生足夠的文件延遲加載。 – Kavya

+0

這可以在批量裝載機的幫助下完成嗎? – Kavya

+0

@Kavya;不,它的工作原理應該如此,我敢肯定。 loader_completeHandler的最後一行是加載之後的最後一行。 – drkstr1

相關問題