SunshineDataGrid動態title和具有excel匯出(無需jsp支援)

來源:互聯網
上載者:User

   近日要做flex的DataGride遇到很多鬱悶的事情,title是靜態,每次都要寫一次title,用起來感覺不方便,重要的資料希望可以匯出到excel,但是裡面又沒現成的方法,我搜尋了一下,發現很多人都是用jsp輔助匯出,我覺得不實際,常出現亂碼。我就修改了一下,用了flex內建的方法實現了相關功能。

 

原始碼:

<?xml version="1.0" encoding="utf-8"?><br /><!-- viruscodecn@gmail.com --><br /><mx:DataGrid xmlns:mx="http://www.adobe.com/2006/mxml"<br /> creationComplete="initDataGrid()"><br /><mx:Script><br /><!--[CDATA[<br />import mx.controls.dataGridClasses.DataGridColumn;<br />import mx.controls.Alert;</p><p>/*<br /> *<value><data><ename></ename><cname></cname></data></value><br /> */<br />[Bindable]<br />public var titleXml:XML;</p><p>public var downloadPolicy:String="off";</p><p>private function initDataGrid():void<br />{<br />var titleArray:Array=new Array;</p><p>for each (var xml:XML in titleXml.data)<br />{<br />var dataGridColumn:DataGridColumn=new DataGridColumn;<br />dataGridColumn.dataField=XML(xml.ename).toString();<br />dataGridColumn.headerText=XML(xml.cname).toString();<br />titleArray.push(dataGridColumn);<br />}<br />this.columns=titleArray;</p><p>initRightContext();<br />}</p><p>//空白處右鍵<br />private function initRightContext():void<br />{<br />var contextMenu:ContextMenu=new ContextMenu();<br />contextMenu.hideBuiltInItems();</p><p>if (downloadPolicy == "on")<br />{<br />var saveExcelContextMenuItem:ContextMenuItem=new ContextMenuItem("匯出為Excel");<br />saveExcelContextMenuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, saveExcel);<br />contextMenu.customItems.push(saveExcelContextMenuItem);<br />}</p><p>this.contextMenu=contextMenu;<br />}</p><p>private function saveExcel(event:ContextMenuEvent):void<br />{<br />//loadDGInExcel(this, 'http://127.0.0.1:8080/SaleSystem/services/ExcelExport.jsp');<br />loadDGInExcelBySave(this);<br />}<br />]]><br /></mx:Script><br /><mx:Script><br /><![CDATA[<br />/**<br /> * Simple script to convert a Datagrid to a HTML table and then<br /> * pass it on to an external excel exporter<br /> *<br /> */</p><p>//Libs that are mostly used<br />//(only a number are necessary for the datagrid conversion and export)<br />import flash.errors.*;<br />import flash.events.*;<br />import flash.external.*;<br />import flash.net.URLRequest;<br />import flash.net.URLVariables;</p><p>/**<br /> * Convert the datagrid to a html table<br /> * Styling etc. can be done externally<br /> *<br /> * @param: dg Datagrid Contains the datagrid that needs to be converted<br /> * @returns: String<br /> */<br />private function convertDGToHTMLTable(dg:DataGrid):String<br />{<br />var font:String=dg.getStyle('fontFamily');<br />var size:String=dg.getStyle('fontSize');<br />var str:String='';<br />var colors:String='';<br />var style:String='style="font-family:' + font + ';font-size:' + size + 'pt;"';<br />var hcolor:Array;</p><p>if (dg.getStyle("headerColor") != undefined)<br />{<br />hcolor=[dg.getStyle("headerColor")];<br />}<br />else<br />{<br />hcolor=dg.getStyle("headerColors");<br />}</p><p>str+='<table width="' + dg.width + '" border="1"><thead><tr width="' + dg.width + '" style="background-color:#' + Number((hcolor[0])).toString(16) + '" mce_style="background-color:#' + Number((hcolor[0])).toString(16) + '">';</p><p>for (var i:int=0; i < dg.columns.length; i++)<br />{<br />colors=dg.getStyle("themeColor");</p><p>if (dg.columns[i].headerText != undefined)<br />{<br />str+="<th " + style + ">" + dg.columns[i].headerText + "</th>";<br />}<br />else<br />{<br />str+="<th " + style + ">" + dg.columns[i].dataField + "</th>";<br />}<br />}<br />str+="</tr></thead><tbody>";<br />colors=dg.getStyle("alternatingRowColors");</p><p>for (var j:int=0; j < dg.dataProvider.length; j++)<br />{<br />str+="<tr width=/"" + Math.ceil(dg.width) + "/">";</p><p>for (var k:int=0; k < dg.columns.length; k++)<br />{</p><p>if (dg.dataProvider.getItemAt(j) != undefined && dg.dataProvider.getItemAt(j) != null)<br />{</p><p>if (dg.columns[k].labelFunction != undefined)<br />{<br />str+="<td width=/"" + Math.ceil(dg.columns[k].width) + "/" " + style + ">" + dg.columns[k].labelFunction(dg.dataProvider.getItemAt(j), dg.columns[k].dataField) + "</td>";</p><p>}<br />else<br />{<br />str+="<td width=/"" + Math.ceil(dg.columns[k].width) + "/" " + style + ">" + dg.dataProvider.getItemAt(j)[dg.columns[k].dataField] + "</td>";<br />}<br />}<br />}<br />str+="</tr>";<br />}<br />str+="</tbody></table>";</p><p>return str;<br />}</p><p>private function loadDGInExcelBySave(dg:DataGrid):void<br />{<br />var ba:ByteArray=new ByteArray;<br />ba.writeUTF(convertDGToHTMLTable(dg));<br />var _fileRef:FileReference=new FileReference();<br />_fileRef.save(ba, "data.xls"); //儲存到磁碟,會出現個系統儲存對話方塊。<br />ba.clear();<br />}<br />]]--><br /></mx:Script><br /></mx:DataGrid>

 

應該使用比較簡單,如果大家使用有什麼問題可以線上聯絡我

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.