Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to export excel file in SmartClient ?

    Hi Isomorphic,

    Currently I am using SmartClient LGPL edition, So Can i using export excel manually ? Because I saw that function excel export just be in SmartClient Pro. Can you guide me how to export data manually ? Thanks.

    #2
    Here is a snippet that may be useful for you:

    Code:
    ListGrid.addProperties({
    
        exportCSV: function() {
          if(!this.dataSource) { return; }
    
          if(isA.String(this.dataSource)) { this.dataSource = DataSource.get(this.dataSource); }
    
          var url = this.dataSource.dataURL+'?';
    
          var extension = arguments[0] ? arguments[0] : 'csv';
    
          var params = {
    	_dataSource: this.dataSource.ID,
    	_operationType: "fetch",
    	_downloadFilename: this.dataSource.ID + "." + extension,
    	_exportFormat: extension
          };
    
          if(this.dataSource.table) {
    	params._table = this.dataSource.table;
          }
    
          if(this.textMatchStyle) {
    	params._textMatchStyle = this.textMatchStyle;
          }
    
          if(extension == 'csv' || extension == 'xls') {
    	var allFields = this.getAllFields();
    	var visibleFields = [];
    	for(var i = 0; i < allFields.getLength(); i++) {
    	  var field = allFields.get(i);
    	  if(this.fieldIsVisible(field)) {
    	    visibleFields.push(field.name);
    	  }
    	}
    	params._exportFields = visibleFields.join(',');
          }
    
          isc.addProperties(params, this.getCriteria());
    
          for(var key in params) { url = url + '&' + encodeURIComponent(key) + '=' + encodeURIComponent(params[key]); }
    
          iframe.setContentsURL(url);
    
        }
    });
    You also need an IFRAME to direct download to:
    Code:
    HTMLFlow.create({
        ID: "iframe",
        name: "iframe",
        contentsType: "page",
        autoDraw: true,
        visibility: "hidden"
      });
    Method's name is ExportCSV, but it can export to anything - it depends only on your backend.
    It exports only visibile field, but it can't be changed in method's code.

    From the server side you should detect if _downloadFilename was set as one of parameters and if it was output a file with format according to _exportFormat and headers:
    Code:
    Content-Type: text/plain; charset=UTF-8
    Content-Disposition: attachment; filename="{downloadFilename}"

    Comment


      #3
      hi janusz ,

      Thanks for your help, but i am just beginner in smart client, can you guide me how to call process in server side ? My meaning that I created a button, when I click a button, call list.exportCSV() ( exportCSV is method we defined ). But I don't know how to call in server side to download file. Can you guide me more about this ? Because I must research and implement project using smartclient separately. So I am really not good about this framework. If you don't mind, can I know your email or yahoo or sky so that I can ask for your help directly.

      Thanks one more.
      Last edited by m74; 6 Jun 2010, 17:51.

      Comment


        #4
        After you click on the button and ListGrid.exportCSV will run, it will load an ordinary webpage to a hidden IFRAME on your page.

        Address of this webpage will be something like
        http://www.mypage.com/smartclient_datasource.php?&_dataSource=myDataSource&_operationType=fetch&_downloadFilename=myDataSource.csv&_exportFormat=csv&_exportFields=customer_id%2Corder_id2&customer_id=123

        Here we assume that you have a DataSource with ID = myDataSource, dataURL = http://www.mypage.com/smartclient_datasource.php
        And the ListGrid is currently showing fields customer_id and order_id and only those values from DataSource where customer_id = 123 - it means that you want to export a list of orders placed by customer 123.

        My approach was to put the whole backend in one file so in file smartclient_datasource.php on a server we have to detect if we are loading a DataSource to show in a ListGrid or to download as CSV/XLS. In the second condition the _exportFormat will be set to something like "csv", "xls". In the first condition it will be not set at all.

        Code:
        // very simplified smartclient_datasource.php
        
        // we load filtered data from a database
        $data = sql("SELECT customer_id, order_id, order_value, (...) FROM customers_orders WHERE customer_id = 123);
        
        // we check if are sending to ListGrid or to IFRAME
        if(isset($_GET['_exportFormat'])) {
          if($_GET['exportFormat'] == 'csv') outputDataAsCSV($data, $_GET['_exportFields']); exit();
          if($_GET['exportFormat'] == 'xls') outputDataAsXLS($data, $_GET['_exportFields']); exit();
        } else {
          outputDataAsJSON($data);
        }
        As you can see for export to CSV and XLS we have to filter out some fields, that we do not want to export. Our sample DataSource has more columns (like order_value) which in this example we do not want to export.

        For ListGrid we want to send all columns and SmartClient will take care of what is going to be visible.

        Comment


          #5
          Thanks so muck, it's work for me now.

          Comment

          Working...
          X