php - 创建新工作表 PHPExcel

标签 php excel cakephp

我正在尝试创建另一个工作表,一切正常。但是我现在需要的是根据变量创建 1 。例如:

我有两种选择,一种用于验证,一种用于结果。 一切都由一个名为 $resultado 的 bool 变量决定。

我在 CakePHP 中有我的组件

function ExcelCargaMasivaComponent() {
    $this->xls = new PHPExcel();
    $this->sheet = $this->xls->getActiveSheet();
    $this->sheet->setTitle("Worksheet");
    $this->sheet->getDefaultStyle()->getFont()->setName('Verdana');
    $this->xls->createSheet();
    $this->xls->setActiveSheetIndex(1);
    $this->validations = $this->xls->getActiveSheet();
    $this->validations->setTitle('Validations');
}

其中 this->validations 是第二个工作表。现在,我需要这个工作表有一个不同的名称,因此我想将其他数据封装在一个函数中。所以我的函数以这种方式生成想要的条件:

function ExcelCargaMasivaComponent() {
    $this->xls = new PHPExcel();
    $this->sheet = $this->xls->getActiveSheet();
    $this->sheet->setTitle("Worksheet");
    $this->sheet->getDefaultStyle()->getFont()->setName('Verdana');
    $this->xls->createSheet();
    $this->xls->setActiveSheetIndex(1);
}

function generate($title = 'Report', $headers = array(), $data = array(), $uid = false, $resultados = false){
    if($resultados){
       $this->validations = $this->xls->getActiveSheet();
       $this->validations->setTitle('Resultados');
    }else{
       $this->validations = $this->xls->getActiveSheet();
       $this->validations->setTitle('Validations');
    }
}

我这样做是为了让第二张表根据变量具有不同的名称和不同的数据,但我无法让它工作。我只生成 1 个标题取决于变量的工作表,这不是我想要的。

最佳答案

创建新工作表 PHPExcel

您好,我真的不知道我的回答是否真的能解决您的问题。然而,这对我来说似乎很吸引人。

答案:
只需尝试对您的 generate 方法执行以下操作,因为我提供了以下代码片段:

function ExcelCargaMasivaComponent() {
    $this->xls = new PHPExcel();
    $this->sheet = $this->xls->getActiveSheet();
    $this->sheet->setTitle("Worksheet");
    $this->sheet->getDefaultStyle()->getFont()->setName('Verdana');
//  $this->xls->createSheet(); // comment out this lines as we keep
//  $this->xls->setActiveSheetIndex(1);  // them in our generate method
}

function generate($title = 'Report', $headers = array(), $data = array(), $uid = false, $resultados = false) {
    if ($resultados) {
       $this->xls->createSheet(0);
       $this->xls->setActiveSheetIndex(0); // This is the first required line
       $this->validations = $this->xls->getActiveSheet();
       $this->validations->setTitle('Resultados');
    } else {
       $this->xls->createSheet(1);           
       $this->xls->setActiveSheetIndex(1); // This is the second required line
       $this->validations = $this->xls->getActiveSheet();
       $this->validations->setTitle('Validations');
    }
}

如需进一步引用,请参阅以下 SO Q&A Thread too.

关于php - 创建新工作表 PHPExcel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33896463/

相关文章:

cakephp - 只检索至少有一个关联的记录

php - windows VC9 ffmpeg 和魔法棒

php - 第 1 行第 6 列错误 : XML declaration allowed only at the start of the document

excel - 识别红色细胞

excel - Excel 中的 FV 和 PV 公式

php - 计算 Cakephp 查询生成器的行数

javascript - 使用 AJAX 从 Javascript 运行整个 PHP 文件

php - 寻找有关如何处理面向公众的对象 ID 的建议

vba - 如何激活特定工作簿和特定工作表?

$.post通过jquery提交后,Cakephp清空$this->data