0

我已经成功地让 PHPExcel 编写了一个漂亮的、格式良好的电子表格。我的问题是下一步该怎么做。如果文件写入成功并保存,我需要执行一些数据库更新。如果尚未写入文件,则不得进行这些更新。

我已经四处搜索,但找不到任何关于此的内容。

这是我当前的“写”代码:

    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
    $file['success'] = $objWriter->save($file['path'].$file['filename']);   

$file 数组的其他部分在别处指定。这样我可以将数组作为函数结果传递,并在我的代码的其他部分使用路径和文件名。当前 $file['success'] 调试为 NULL。

编辑:类内方法的结构

    public function export_ap_files {

    //fetch data to go into the XLS file
    $data = $this->get_invoices_for_payment();

    //build excel files
    if ( !empty($data['ap_invoices']) ) {

                    //code to build the XLSX is in another method
        $ap_xls = $this->export_ap_excel($data['ap_invoices']);

        //mark invoices as sent for payment - if the XLSX file has not been written then this bit should not proceed
        foreach ($data['ap_invoices'] as $invoice) {

            //CakePHP save code goes here
        }

    }

    die('done');

    }
4

1 回答 1

1

PHPExcel 在失败时抛出异常,因此将代码包装在 try/catch 块中

编辑

// fetch data to go into the XLS file
$data = $this->get_invoices_for_payment();

// build excel files
if ( !empty($data['ap_invoices']) ) {

    try {
        // code to build the XLSX is in another method
        $ap_xls = $this->export_ap_excel($data['ap_invoices']);
    } catch (Exception $e) {
        die('Failed to create Excel file: ' . $e->getMessage());
    }

    // mark invoices as sent for payment
    // if the XLSX file has not been written then this bit should not proceed
    foreach ($data['ap_invoices'] as $invoice) {

        //CakePHP save code goes here
    }
}

die('done');
于 2013-07-30T09:28:29.013 回答