codeigniter PHPExcel公式在HTML中错误地返回零

bihw5rsg  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(196)

如果我使用Excel2007 writer,它可以工作,但当我将其编写为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);
    }
}

字符串
我不知道这个问题,我如何解决它?

jum4pzuy

jum4pzuy1#

解决了这个问题,在HTML writer中,单元格应该以XML格式编写,例如A1,它在mycode中以XML格式从$this->translate_cell($i);返回。

相关问题