2015-12-30 186 views
3

如果我使用Excel2007編寫器,它工作,但是當我將它寫成HTML時,公式根本不起作用。PHPExcel公式不能在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); 
    } 
} 

有沒有人遇到過這個問題?我不知道這個問題,請別人保存我的一天。

+0

您的輸出是什麼?您的預期輸出是什麼? – AgeDeO

+0

@AgeDeO:輸出打印0(零),期望輸出明顯計算結果。 –

回答

0

解決了這個問題,在HTML編寫器中,單元格應該用大寫形式寫,例如A1,mycode中的小寫字母從$this->translate_cell($i);返回。

+0

你必須接受你自己的答案,並回答這個問題 –