我是Yii的新人。我正在做一个导出到Excel选项的场景。我已经完成了一半。这些值将填充到必要的字段中。但是如果存在空值,Excel中的单元格将获取下一个列值,因此发生了不匹配,那么如何在Excel导出中执行空白单元格呢?
if(!empty($cmApprRes)){
foreach($cmApprRes as $log){
$arrData[$i][]=CHtml::encode($log->cm_log_description);
}
}
else
//if there is no data in the log table the corresponding column should be shown as blank.
//but that is not working please suggest an option for else
{
}提前谢谢。
发布于 2014-06-04 13:27:15
这是固定的。抱歉,这是一个简单的步骤。
if(!empty($cmApprRes)){
foreach($cmApprRes as $log){
$arrData[$i][]=CHtml::encode($log->cm_log_description);
}
}
else
{
$arrData[$i][]="";
}发布于 2016-01-30 04:04:19
如果您试图通过自己编写解决方案来解决Excel导出需求,那么我建议您避免重复发明轮子(除非完全必要或出于某种原因,需要),而应使用第三方库。您可以使用yiiBasicXls (https://github.com/faustow/yiiBasicXls)。它的使用非常简单:
Installation
下载XlsExporter.php并将其移动到您自己的Yii应用程序的/protected/components/中。
使用
一旦XlsExporter组件就位,您只需要:
data:活动记录数据集(required)title:字符串,将用作显示在导出行顶部的标题。默认为false.header:布尔值,用于显示/隐藏页眉。默认为要导出的字段的false.fields:数组。默认为false.type:字符串,用于解释为最终用户导出的内容(使用复数),如'users‘、'cars’等。默认为'lines'.
示例
例如(在某些控制器上):
public function actionDownloadReport() {
$fields = array('email', 'firstName', 'lastName', 'type');
$criteria = new CDbCriteria();
$criteria->select = $fields;
$criteria->condition = "firstName = 'John'";
$criteria->order = 'lastName';
$users = users::model()->findAll($criteria);
XlsExporter::downloadXls('report', $users, 'List of users called John', true, $fields, 'users');
}准备就绪后,调用yoursite.com/somecontroller/downloadReport将在浏览器中触发xls文件的下载。
https://stackoverflow.com/questions/24029294
复制相似问题