【问题标题】:PHPExcel formula not working in HTMLPHPExcel 公式在 HTML 中不起作用
【发布时间】:2015-12-30 14:14:22
【问题描述】:

如果我使用 Excel2007 编写器,它可以工作,但是当我将其编写为 HTML 时,公式根本不起作用。

这是我的代码。它在 CodeIgniter 中

function monthly_plan(){
    $data['page_title'] = 'Monthly Plan';
    $data['main'] = 'reports/monthly_plan';
    $data['lines'] = $this->line_model->getLines();

    if($this->input->post('generate')){
        $m = $this->input->post('month');
        $pid = $this->input->post('process_id');
        $date_start = date('Y-'.$m.'-01');
        $date_end = date('Y-'.$m.'-t');
        $last_month = date('Y-m-d', strtotime($date_start.' -1 day'));
        $date_max = date('t');
        $schedules = $this->getScheduleInProcess($pid, $date_start, $date_end, true, true);
        $process = $this->process_model->getProcess($pid);

        $separators = []; $item_names = [];

        // EXCEL

        $this->load->library('excel');

        $this->excel->setActiveSheetIndex(0);
        $sheet = $this->excel->getActiveSheet();

        $sheet->setTitle('Planning Bulanan');
        $sheet->setCellValue('A2', 'Process');
        $sheet->setCellValue('B2', $process->process_name);
        $sheet->setCellValue('C2', 'L/M');

        $last_num = 2; // row terakhir
        $last_num_counter = 2; // row terakhir untuk counter

        for($i = 4; $i <= $date_max + 3; $i++){
            $cell = $this->translate_cell($i);
            $sheet->setCellValue($cell.'2', date(($i - 3).'-M'));
        }

        foreach($schedules as $schedule){
            $item = getItem2($schedule->item_id);
            $route = $this->routing_model->getSingleRoute($schedule->routing_id);

            $sheet->setCellValue('A'.(++$last_num), $item->item_number);
            $sheet->setCellValue('B'.$last_num, 'Ori PLAN Dlvry');
            $sheet->setCellValue('C'.$last_num, '');

            $item_names[] = $last_num;

            $sheet->setCellValue('A'.(++$last_num), '');
            $sheet->setCellValue('B'.$last_num, 'Cum Plan');
            $sheet->setCellValue('C'.$last_num, '0');

            $sheet->setCellValue('A'.(++$last_num), $this->is_lot($route) ? '':$route->ct);
            $sheet->setCellValue('B'.$last_num, 'plan prod');
            $sheet->setCellValue('C'.$last_num, '');

            $sheet->setCellValue('A'.(++$last_num), $this->is_lot($route) ? '':$route->efficiency);
            $sheet->setCellValue('B'.$last_num, 'ACT');
            $sheet->setCellValue('C'.$last_num, '');

            $sheet->setCellValue('A'.(++$last_num), $this->is_lot($route) ? $route->capacity:"=60*A".($last_num - 1)."/A".($last_num - 2));
            $sheet->setCellValue('B'.$last_num, 'Cum Act');
            $sheet->setCellValue('C'.$last_num, $this->getStockToDate($schedule->item_id, $process, $last_month));

            $sheet->setCellValue('A'.(++$last_num), '');
            $sheet->setCellValue('B'.$last_num, 'Diff');
            $sheet->setCellValue('C'.$last_num, '');

            $sheet->setCellValue('A'.(++$last_num), '');
            $sheet->setCellValue('B'.$last_num, '');

            for($i = 4; $i <= $date_max + 3; $i++){
                $cell = $this->translate_cell($i);
                $pc = $this->translate_cell($i-1); // previous cell
                $date = date('Y-'.$m.'-'.($i - 3));

                $sheet->setCellValue($cell.($last_num_counter + 1), $this->getPlanningOnDate($route->process_id, $route->item_id, $date)); // ori plan
                $sheet->setCellValue($cell.($last_num_counter + 2), "=$pc".($last_num_counter + 2)."+".$cell.($last_num_counter + 1)); // cum plan
                $sheet->setCellValue($cell.($last_num_counter + 3), ''); // plan prod
                $sheet->setCellValue($cell.($last_num_counter + 4), ''); // act
                $sheet->setCellValue($cell.($last_num_counter + 5), "=$pc".($last_num_counter + 5)."+".$cell.($last_num_counter + 4)); // cum act
                $sheet->setCellValue($cell.($last_num_counter + 6), "=$cell".($last_num_counter + 5)."+$cell".($last_num_counter + 1)); //div
            }
            $last_num_counter += 7;

            $separators[] = $last_num_counter;
        }

        $last_col = $sheet->getHighestDataColumn();
        $last_row = $sheet->getHighestDataRow();
        $ranges = $this->createColumnsArray($last_col);

        $sheet->freezePane('C3');
        $sheet->getStyle("A2:$last_col$last_row")->applyFromArray(array(
            'borders'=>['allborders'=>['style'=>PHPExcel_Style_Border::BORDER_THIN]],
            'font'=>['size'=>'8']
        ));

        $sheet->getStyle("A3:A$last_row")->applyFromArray(array(
            'alignment'=>['horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER]
        ));

        $sheet->getStyle("C2:C$last_row")->applyFromArray(array(
            'alignment'=>['horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER]
        ));

        $sheet->getStyle("C1:C$last_row")->applyFromArray(array(
            'borders'=>['allborders'=>['style'=>PHPExcel_Style_Border::BORDER_NONE]]
        ));

        foreach($ranges as $range){
            if($range != 'C'){
                $sheet->getColumnDimension($range)->setAutoSize(true);
            }
        }

        for($i = 4; $i <= $date_max + 3; $i++){
            $cell = $this->translate_cell($i);
            $pc = $this->translate_cell($i-1); // previous cell
            $date = date('Y-'.$m.'-'.($i - 3));

            if(isDayOff($date)){
                $sheet->getStyle("{$cell}2:$cell$last_row")->applyFromArray(array(
                    'fill'=>['type'=>PHPExcel_Style_Fill::FILL_SOLID, 'color'=>['rgb'=>'CBCBCB']]
                ));
            }
        }

        foreach($separators as $s){
            $sheet->getStyle("A$s:$last_col$s")->applyFromArray(array(
                'borders'=>['allborders'=>['style'=>PHPExcel_Style_Border::BORDER_NONE]]
            ));
        }

        foreach($item_names as $in){
            $sheet->getStyle("A$in")->applyFromArray(array(
                'font'=>['bold'=>true]
            ));
        }

        // $filename='Planning Bulanan.xlsx';
        // header('Content-Type: application/vnd.ms-excel'); //mime type
        // header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
        // header('Cache-Control: max-age=0'); //no cache

        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'HTML'); //Excel2007
        $objWriter->save('php://output');
    }else{
        $this->load->view($this->tpl, $data);
    }
}

有人遇到过这个问题吗?我不知道这个问题,请有人拯救我的一天。

【问题讨论】:

  • 你的输出是什么,你的预期输出是什么?
  • @AgeDeO : 输出打印 0(零),预期输出显然是计算结果。

标签: php codeigniter phpexcel


【解决方案1】:

解决了这个问题,在 HTML writer 单元格中应该以大写形式编写,例如 A1,在从 $this-&gt;translate_cell($i); 返回的 mycode 中是小写。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多