關於Flex的匯出操作.--從資料庫中讀出內容匯出

來源:互聯網
上載者:User

      網上很多的都是匯出當前頁,匯出頁面內容,等等,很少見到有匯出自己所需要的東西的,

 

      關於Flex的匯出操作.--從資料庫中讀出內容匯出

      這裡的匯出是--匯出資料庫中讀出的內容,可以按照查詢條件匯出。

 

      思想--做過了到現在不斷的進行思考後得到。其實自己寫的這個匯出也是相當的簡單。沒有任何技術含量。跟那些大蝦寫的東西完全沒法比擬,不過還是貼出來了,終究是可以解決一些問題。

 

      好了,還是來說說實際的吧。

 

      最近用FLEX開發了最近的這個項目,到現在,一期接近完工,下一期才是實際的東西--增加的功能。


     由於有相當多的頁面需要匯入匯出。

     這裡最主要的就是一個方法而已,

     這裡沒有給出可執行程式,不過我會儘力說得完整些。

 

     第一:事件來源,也就是點擊按鈕。

<mx:LinkButton width="30" icon="{excelIcon}" toolTip="匯出" click="loadDGInExcel(dataGrid,'http://localhost/wbm2/pages/scripts/excelexport.jsp');" /><br />//這裡的參數沒有用處,因為這裡的參數,是參照了網上的匯出本頁面而定製的,後來我改進了下,所以就沒有用了。

    第二:方法,主體之一,也就是點擊這個事件的具體方法。

private function loadDGInExcel(dg:DataGrid,url:String):void {</p><p>//Pass the htmltable in a variable so that it can be delivered<br />//to the backend script<br />var variables:URLVariables = new URLVariables();<br />download();</p><p>//variables.htmltable= convertDGToHTMLTable(dg);<br />variables.title = "網元配置";<br />variables.table = 'NETCFGTBL';<br />//variables.params = StringUtil.trim(nodeName.text)+';'+svrType.selectedItem.data;<br />//variables.htmltable= request4doanload.lastResult//convertDataToHtml();<br />//Alert.show((request4doanload.lastResult as XML).toXMLString()+'');</p><p>//Setup a new request and make sure that we are<br />//sending the data through a post</p><p>//var u:URLRequest = new URLRequest(url);<br />var u:URLRequest = new URLRequest();<br />u.url = "http://localhost/wbm2/download.do?svrType="+svrType.selectedItem.data+'&nodeName='+StringUtil.trim(nodeName.text);<br />u.data = variables; //Pass the variables<br />u.method = URLRequestMethod.POST; //Don't forget that we need to send as POST</p><p>//Navigate to the script<br />//We can use _self here, since the script will through a filedownload header<br />//which results in offering a download to the user (and still remaining in you Flex app.)<br /> navigateToURL(u,"_blank");//navigateToURL(u,"_self");<br />}

      第三:就是具體的Servlet了。download.do這個Servlet。這個Servlet我封裝了一下,不過沒有關係,就算大家沒有封裝同樣的用。
這裡的DownLoadBs是一個業務層。我這裡用的是Spring+Servlet,所以用過的應該能夠看懂。

public void doPost(HttpServletRequest request, HttpServletResponse response)<br />throws ServletException, IOException {</p><p>System.out.println("-----------2>");<br />foreachRequest(request);</p><p>String table = getString(request, "table");</p><p>DownLoadBs netCfgBs = (DownLoadBs) getObj("downLoadBs");<br />if(table.equalsIgnoreCase("NETCFGTBL")){</p><p>netCfgBs.downloadnetcfg(request, response);<br />}</p><p>}

     第四就是業務層的jxl到處資料了,這個我想大家都不用我多講了吧.就此差不多了.
後面貼出的就是一些候補的內容.

DownLoadBs.Java<br />@Override<br />public void downloadnetcfg(HttpServletRequest request,<br />HttpServletResponse response) {</p><p>try {<br />//<br />// String params = getString(request, "params");<br />// String[] args = params.split(";");<br />Integer svrType = getInteger(request, "svrType");<br />String nodeName = getString(request, "nodeName");</p><p>List<Map<String, Object>> netcfg = downLoadDao.netcfgList(svrType,<br />nodeName);</p><p>String fileName = "downloadFile";<br />String tableName = "downloadFile";</p><p>String[] cols = { "NODENO", "NODENAME", "SVRTYPE", "IPADDR",<br />"WWWIP", "OSTYPE", "BASICFREQUENCE", "MEMORY", "DISKCAP",<br />"MEMO" };<br />String[] names = { "節點編號", "節點名稱", "節點類型", "節點地址", "公網IP", "作業系統",<br />"主頻", "記憶體", "磁碟", "備忘" };<br />Object[] types = { 0, "", 0, "", "", 0, 0, 0, 0, "" };</p><p>new ToyExcel().doPrint(fileName, tableName, cols, names, types,<br />netcfg, response);<br />} catch (Exception e) {</p><p>e.printStackTrace();<br />} finally {<br />System.out.println("success");<br />}<br />}

     然後就是得到這個資料的內容.也即是list

public List<Map<String, Object>> netcfgList(Integer svrType, String nodeName)<br />throws Exception {</p><p>sql = new StringBuffer();<br />sql.append("select NODENO, NODENAME, SVRTYPE, IPADDR, ");<br />sql.append("WWWIP, OSTYPE, BASICFREQUENCE, MEMORY, DISKCAP, MEMO ");<br />sql.append("from NETCFGTBL where 1=1 ");</p><p>if (nodeName != null && !nodeName.equals("")) {<br />sql.append(" and NODENAME like '%").append(nodeName).append("%' ");<br />}<br />if (svrType != null && svrType != 0) {<br />sql.append(" and SVRTYPE=").append(svrType);<br />}</p><p>return query4List(sql);<br />}

 自此我所有的都貼出來了,所有的都是公司項目的源碼,當然,對於你們沒有任何用處,有用的就是這個思想而已.我想大家改進一下就可以用了.暫時這個沒有問題,不過我會繼續測試,而且找更好的替代方法.這個也是第二個替代品,第一個就是網上說得很多的匯出頁面內容,那個很不友好.所以改進下,而且還會找到更多的版本,希望對大家有協助.

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.