简体   繁体   中英

How to generate an excel file using js

I have a UI that shows a CRUD(create, read, update and delete) account of an employee. Now, I want to add a generate button that when its click, a window will pop-up that will show and ask if the following data in the grid lines under the UI are to be open or saved using the excel report.Also, I have already EXcelPhp library.

Here's my code for my 'actions.class.php':

public function executeLoadEmployeeList(sfWebRequest $request)
{

    // $start = $request->getParameter('start') ? $request->getParameter('start'): 2;
    // $limit = $request->getParameter('limit') ? $request->getParameter('limit'): 2;
    $query = pg_escape_string($request->getParameter('query'));     
    $start = $request->getParameter('start');
    $limit = $request->getParameter('limit');

    if(isset($limit))
    {
        $page = $start / $limit;
        $page ++;
    }
    else
        $page = 1;


    $criteria = Doctrine_Query::create();//what is the query?? is it select,inset,update,delete?
    $criteria->select("(fname || ' ' || lname)  AS fullname, department");
    $criteria->from('Employees'); // Select * from profile
    $criteria->orderBy('id'); // order by id

    //print $criteria->getSqlQuery();
    //die();

    if($query!=null)
      {
        $criteria->where("(fname ilike '%$query%' or lname ilike '%$query%' or department ilike '%$query%')"); //where (uname ilike '%$query%' or status ilike '%$query%')
      }             

    $allData = $criteria->fetchArray();


    // print "<pre>";       
    // print_r($allData);
    // die();   

    $this->pager = new sfDoctrinePager('Employees', 20); //what is sfdoctrine about? dont mind this.. this is a symphony built in class for pager
    $this->pager->setQuery($criteria);
    $this->pager->setPage($page);
    $this->pager->init();//What is the purpose of this line? //initialize sfDoctrinePager
    $result['data'] = $this->pager->getResults();

    $result['totalCount'] = count($allData);
    $result['limit'] = $limit;
    $result['page'] = $page;
    $result['query'] = $query;

    die(json_encode($result));      
}

public function executeAddEmployee(sfWebRequest $request)
{
    try{
        $fname = $request->getParameter('fname');
        $lname = $request->getParameter('lname');
        $department = $request->getParameter('department');

        $Employee = new Employees();
        $Employee->fname = $fname;
        $Employee->lname = $lname;
        $Employee->department = $department;
        //save the data to the database
        $Employee->save();

        $data = array("success"=> true, "data"=>"Employee Added.");
    }
    catch(Exception $e)
    {   
        $data = array("success"=> false, "data"=>$e->getMessage());
    }     
        //$data is a return value of trycatch
        die(json_encode($data));
}

public function executeDeleteEmployee(sfWebRequest $request)
{
     try{
        //what is Doctrine::getTable's purpose // to get the table profile
        $this->forward404Unless($Employee = Doctrine::getTable('Employees')->find(array($request->getParameter('id'))), sprintf('Employee ID in Form does not exist (%s).', $request->getParameter('id')));     

        $Employee->delete();

        $data = array("success"=> true, "data"=>"Employee record is Deleted.");
     } catch(Exception $e) {
        $data = array("success"=> false, "data"=>$e->getMessage());
     }
        //$data is a return value of trycatch
        die(json_encode($data));
}

public function executeEditEmployee(sfWebRequest $request)
{
    try{
        $this->forward404Unless($Employee = Doctrine::getTable('Employees')->find(array($request->getParameter('id'))), sprintf('Employee ID in Form does not exist (%s).', array($request->getParameter('id'))));

        $criteria = Doctrine_Query::create();
        $criteria->select('fname,lname,department');
        $criteria->from('Employees');
        $criteria->where('id = ?', $request->getParameter('id'));//('id = ?', $request->getParameter('id') means... id = $request->getParameter('id')
        $result = $criteria->fetchArray();

        $record['fname'] = $Employee['fname'];
        $record['lname'] = $Employee['lname'];
        $record['department'] = $Employee['department'];

        $data = array("success"=> true, "data"=>$record);

    } catch(Exception $e) {
        $data = array("success"=> false, "data"=>$e->getMessage());
    }     
        //$data is a return value of trycatch
        die(json_encode($data));
}

public function executeUpdateEmployee(sfWebRequest $request)
{
    try{

        $Employee = Doctrine::getTable('Employees')->find(array($request->getParameter('id')));        
        $Employee->fname = $request->getParameter('fname');
        $Employee->lname = $request->getParameter('lname');
        $Employee->department = $request->getParameter('department');

        //save the update to the database
        $Employee->save();

        $data = array("success"=> true, "data"=>"Employee Successfully Updated.");
    }
    catch(Exception $e)
    {
        $data = array("success"=> false, "data"=>$e->getMessage());
    }     
        //$data is a return value of trycatch
        die(json_encode($data));
}
public function executeGenerateEmployee(sfWebRequest $request)
{
    // ...



}**

What I've tried so far is setting only the generate button and there's no action yet. This is under my try.js :

var generateItem = new Ext.Action ({
  text: 'Generate Excel Report',
  width: 60,
  enabled: true,
});

Could someone help me regarding this issue?

You can not generate an excel file without using a server side language / script. You can just prepare how it Will look and add some functions to make it functional like write, delete etc.

You can generate an Excel spreadsheet without any server side processing however you'll have a hellish time with browser support.

In theory you could generate an excel formatted file in js then simply do a window.open with the data URI.

for example here's a javascript generated image:

window.open('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC');

however.. it'll probably not be supported in most of the browsers for Excel data URIs:

here's another similar question:

Data URI used to export to CSV/Excel (no server-side request) : browser support/limitations?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM