我正在尝试从50个excel文件中删除一行,为此我在flex中创建一个工具来浏览文件夹,查找xls文件然后对其执行操作。我在这里面临两个问题: 1.我有一个迭代文件夹的循环,一旦找到xls,它应该调用一个函数来删除该行并返回到for循环并继续文件夹中的其他excel文件。但它没有这样做。
2.我无法删除并在excel中输入。我正在使用as3xls与excel一起工作。在这里我设置了一个我不需要的行的值,因为我不知道如何删除它。
我是初学者,所以请帮助。
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 :(得分: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)
}
}