2011年12月27日 星期二

Flex DataGrid 複製至剪貼簿 CSV格式


<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600"
creationComplete="start(event)"
>
<fx:Script>
<![CDATA[
import flash.desktop.Clipboard;
import flash.desktop.ClipboardFormats;

import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import mx.utils.UIDUtil;

protected function start(event:FlexEvent):void
{
  var provider:ArrayCollection = new ArrayCollection();
  for(var i:int=0 ; i < 10 ; i++)
  {
    provider.addItem( {id:UIDUtil.createUID() , index:i , value:i + "號"});
  }
  grid.dataProvider = provider;
}

protected function copy(event:MouseEvent):void
{
  var csv:String = "";
  var item:Object;
  csv = "ID" + "\t" + "Index" + "\t" + "Value" + "\n";
  for(var i:int ; i < grid.dataProvider.length ; i++)
  {
    item = grid.dataProvider.getItemAt(i);
    csv += item['id'] + '\t' + item['index'] + '\t' + item['value'] + '\n';
  }

//寫入剪貼簿
Clipboard.generalClipboard.clear();
Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT , csv , false);
}

]]>
</fx:Script>
<s:VGroup>
<s:DataGrid id="grid"
  verticalScrollPolicy="off"
  variableRowHeight="false"
  editable="false"
  selectionMode="singleRow"
  alternatingRowColors="{new Array(0xF0F0F0, 0xFFFFFF)}">
  <s:columns>
   <s:ArrayList>
    <s:GridColumn dataField="id" headerText="ID" resizable="false"    minWidth="300"/>
    <s:GridColumn dataField="index" headerText="Index" resizable="false"   minWidth="80"/>
    <s:GridColumn dataField="value" headerText="Value" resizable="false" minWidth="80"/>
   </s:ArrayList>
  </s:columns>
</s:DataGrid>

<s:Button label="複製到剪貼簿" click="copy(event)"/>
</s:VGroup>
</s:Application>



















如此可以簡單的貼到execel中。

CSV格式參考文件

沒有留言:

張貼留言