如何使用js生成excel文件

时间:2014-01-06 06:06:18

标签: javascript php postgresql phpexcel

我有一个UI,显示员工的CRUD(创建,读取,更新和删除)帐户。现在,我想添加一个generate按钮,当它点击时,会弹出一个窗口,显示并询问是否要使用excel报告打开或保存UI下网格线中的以下数据。而且,我已经有了EXcelPhp库。

这是我的'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)
{
    // ...



}**

到目前为止我尝试过的只是设置生成按钮而且还没有动作。这是在我的try.js

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

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

如果不使用服务器端语言/脚本,则无法生成Excel文件。您可以准备它的外观并添加一些功能,使其功能如写,删除等。

答案 1 :(得分:0)

您可以在没有任何服务器端处理的情况下生成Excel电子表格,但是在浏览器支持方面你会有一个地狱般的时间。

理论上你可以用js生成一个excel格式的文件,然后用数据URI做一个window.open

例如,这是一个javascript生成的图像:

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

然而..在大多数Excel数据URI浏览器中可能都不支持它:

这是另一个类似的问题:

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