我已经成功地让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');
}
答案 0 :(得分: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');