繁体   English   中英

在Phalcon php框架中创建Excel文件

[英]create Excel file in Phalcon php framework

我在phalcon php框架中工作,我试图创建简单的xsl文件,但是当我调用控制器时,我得到了空的excel表。 谁能告诉我代码中的错误。 这是我的方法:

public function createExcelAction () {

    $loader = new \Phalcon\Loader();

    $loader->registerClasses(array(
        "PHPExcel"                  => "C:/wamp/www/phalcon1/Classes/PHPExcel.php",
        "PHPExcel_Writer_Excel2007" => "C:/wamp/www/phalcon1/Classes/PHPExcel/Writer/PHPExcel_Writer_Excel2007.php"
    ));
    $loader->register();
    $excel = new PHPExcel();
    //$excel = new PHPExcel();
    //activate worksheet number 1
    $excel->setActiveSheetIndex(0);
    //name the worksheet
    $excel->getActiveSheet()->setTitle('test worksheet');
    //set cell A1 content with some text


    $excel->getActiveSheet()->setCellValue('A1', 'Rezultati pretrage');
    $excel->getActiveSheet()->setCellValue('A2', "Ime");
    $excel->getActiveSheet()->setCellValue('C2', "Prezime");
    $excel->getActiveSheet()->setCellValue('F2', "Adresa stanovanja");

    $br = rand(0, 1000000);
    $naziv = "Pobjeda-Rudet izvjestaj-" . $br . ".xls";
    //$filename='pobjeda_rudet.xlsx'; //save our workbook as this file name
    header('Content-Type: application/vnd.ms-excel'); //mime typež
    header('Content-Disposition: attachment;filename="' . $naziv . '"');
    // header('Content-Disposition: attachment;filename="'.$filename.'"'); 
    header('Cache-Control: max-age=0'); //no cache
    $objWriter = new PHPExcel_Writer_Excel2007($excel);
    $objWriter->save(str_replace('.php', '.xls', __FILE__));
}
public function createExcelAction() {
    global $config;

    $loader = new \Phalcon\Loader();

    $loader->registerDirs(
        array(
            $config->application->libraryDir."PHPExcel/"
        )
    );

    $loader->register();

    $excel = new PHPExcel();
    $excel->setActiveSheetIndex(0);
    $excel->getActiveSheet()->setTitle('test worksheet');

    $excel->getActiveSheet()->setCellValue('A1', 'Rezultati pretrage');
    $excel->getActiveSheet()->setCellValue('A2', "Ime");
    $excel->getActiveSheet()->setCellValue('C2', "Prezime");
    $excel->getActiveSheet()->setCellValue('F2', "Adresa stanovanja");

    $br = rand(0,1000000);
    $naziv = $br.".xls";
    $objWriter = new PHPExcel_Writer_Excel2007($excel);
    $objWriter->save('../tmp/'.$naziv);
}

首先-您需要将库放入phalcon_project / app / library /第二-注册库目录第三-创建对象(新的PHPExcel)

使用对象方法编写spreedshet

最后,创建PHPWritter Object并保存到目录中(在此示例中,将tmp用作具有777权限的根文件夹)。

PS:对不起,我的英语不好!

更好的方法是将PHPExcel扩展名放在app / library / PHPExcell目录中,然后在app / config / loader.php中注册

$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerDirs(
    array(
        $config->application->controllersDir,
        $config->application->modelsDir,
        $config->application->libraryDir . 'PHPExcel/Classes/',
    )
);

$loader->register();

然后在控制器的动作中创建对象并以适当的方式输出:

public function someAction() {
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getActiveSheet()->setTitle('test worksheet');
    $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'Rezultati pretrage')
        ->setCellValue('A2', "Ime")
        ->setCellValue('C2', "Prezime")
        ->setCellValue('F2', "Adresa stanovanja");

    // file name to output
    $fname = date("Ymd_his") . ".xlsx";

    // temp file name to save before output
    $temp_file = tempnam(sys_get_temp_dir(), 'phpexcel');

    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
    $objWriter->save($temp_file);

    $response = new \Phalcon\Http\Response();

    // Redirect output to a client’s web browser (Excel2007)
    $response->setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    $response->setHeader('Content-Disposition', 'attachment;filename="' . $fname . '"');
    $response->setHeader('Cache-Control', 'max-age=0');

    // If you're serving to IE 9, then the following may be needed
    $response->setHeader('Cache-Control', 'max-age=1');

    //Set the content of the response
    $response->setContent(file_get_contents($temp_file));

    // delete temp file
    unlink($temp_file);

    //Return the response
    return $response;
}

如果只需要特定的文件,则可以这样使用:

   $loader->registerClasses(
    array(
        "PHPMailer"         => "../app/library/PHPMailer.php",
        "Helper"            => "../app/library/Helper.php"
    )
);

我在这个问题的解决方案中搜索了很多。我尝试了PHPExcel,Jwage,但是以某种方式失败了。然后我尝试编写了一些简单的方法。希望它对某人有帮助。主要的帮助部分是,代码很容易理解,您可以放您的条件,例如if / else,switch,css样式(如果需要)。

在您的Phalcon控制器中尝试以下代码-

public function exportAction()
{
    //Declaring css
    $header_css = "PUT Your CSS if needed";
    $data_css = "PUT Your CSS if needed";
    //If data comes from model then
    //$data = Modelname::find([give your condition]);
    //As i'm using join query i feel good using createBuilder
    $data = $this->modelsManager->createBuilder()
          ->columns("comment.comment, comment.username, comment.email, comment.postedat,item.name,item.photo,item.view,item.categoryid,item.id")
          ->From('comment')
          ->innerjoin('item', 'comment.productid = item.id')
          ->where("comment.email = 'pass your value here' ")
          ->getQuery()
          ->execute(); 

    $table = "<table>
    <tr>
    <td style='$header_css'>Given Name</td>
    <td style='$header_css'>Email</td>
    <td style='$header_css'>Bird Name</td>
    <td style='$header_css'>Posted Time</td>
    <td style='$header_css'>Your comment</td>
    <td style='$header_css'>Total view by all </td>
    </tr>";
    foreach ($data as $row) {
        $table.= "<tr>
        <td style='$data_css'>$row->username</td>
        <td style='$data_css'>$row->email</td>
        <td style='$data_css'>$row->name</td>
        <td style='$data_css'>$row->postedat</td>
        <td style='$data_css'>$row->comment</td>
        <td style='$data_css'>$row->view</td>
        </tr>";
    }
    $table.= '</table>';

    header ("Content-type: application/xls;charset=UTF-8");
    header ("Content-Disposition: attachment; filename=Activity.xls" );
    return $table;



} 

我认为这对希望生成Excel报告的每个人都适用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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