1

我正在使用 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__));
}
4

4 回答 4

4
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 对象并保存到一个目录中(在本例中,我使用 tmp 进入具有 777 权限的根文件夹)。

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

于 2013-10-10T21:21:48.973 回答
4

更好的方法是将 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;
}
于 2014-06-17T09:24:34.267 回答
0

如果你只是想要特定的文件,你可以这样使用:

   $loader->registerClasses(
    array(
        "PHPMailer"         => "../app/library/PHPMailer.php",
        "Helper"            => "../app/library/Helper.php"
    )
);
于 2013-10-22T12:50:02.627 回答
0

我搜索了很多这个问题的解决方案。我尝试了 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 报告的人

于 2018-09-29T10:33:17.093 回答