2015-09-07 69 views
2

我正在使用codeigniter框架,在這裏我正在用MySQL數據創建excel文件。我需要創建頭表(即第一個循環)數據到第一個工作表,細節表(即第二個循環)數據到第二個工作表中。下面我給出了我的代碼,這將生成與下一個數據相同的表單。任何人都可以提出一些想法來解決這個問題。如何自定義excel數據的兩個工作表在一個excel與PHP?

$out = '"S.no","HeaderID","InvoiceID","InvoiceNo","doc_no","InvoiceDate","PartyCode","doc_type","CurrencyID","Remarks","loc_amt","doc_amt"'."\r\n"; 
     $i=1;  
     foreach($export_list as $d) 
     { 
      $out .= $i.',"'.$d->slsid.'","'.'0'.'","'.$d->reference_no.'","'.' '.'","'.$d->date.'","'.$d->customer_code.'","'.' '.'","'.' '.'","'.$d->internal_note.'","'.'0'.'","'.$d->total.'"'."\r\n"; 
      $i++;     
     }  

     $out .= '"S.no","HeaderID","DetailID","ProductID","Description","Qty","loc_amt","doc_amt"'."\r\n"; 
     $i=1;  
     foreach($export_detail as $d) 
     { 
      $out .= $i.',"'.$d->sale_id.'","'.$d->id.'","'.$d->product_code.'","'.' '.'","'.$d->quantity.'","'.'0'.'","'.$d->gross_total.'"'."\r\n"; 
      $i++;     
     } 

     header('Content-Description: File Transfer'); 
     header('Content-Type: application/octet-stream'); 
     header('Content-Disposition: attachment; filename=Users.xls'); 
     header('Content-Transfer-Encoding: binary'); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     header('Pragma: public'); 
     echo "\xEF\xBB\xBF"; // UTF-8 BOM 
     echo $out; 
     exit; 

在此先感謝。

+0

嘗試使用PHPExcel –

+0

在PHPExcel中,您可以更改工作表添加標題到工作表等 –

+0

爲此我需要下載PHPExcel核心文件並添加到我的模塊中嗎?然後如何形成excel文件。如果你有任何鏈接給我,我會遵循。 –

回答

1

是的,必須下載PHPExcel library for codeigniter。

我有一些示例代碼,可以幫助您在PHPExcel上工作。

function test_excel() 
     { 
      $this->load->library('excel'); 
      $this->excel->setActiveSheetIndex(0); 
      $this->excel->getActiveSheet()->setTitle('test worksheet'); 
      $this->excel->getActiveSheet()->setCellValue('A1', 'User id'); 
      $this->excel->getActiveSheet()->setCellValue('B1', 'User name'); 
      $this->excel->getActiveSheet()->setCellValue('C1', 'Email'); 
      $this->excel->getActiveSheet()->setCellValue('D1', 'Status'); 
      $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); 
      $this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true); 
      $this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true); 
      $this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true); 
      $query = $this->db->query("SELECT * FROM users"); 
      $k=2; 
      foreach($query->result_array() as $row) 
      { 
       $this->excel->getActiveSheet()->setCellValue("A".$k, $row['USER_ID']); 
       $this->excel->getActiveSheet()->setCellValue("B".$k, $row['FIRST_NAME']); 
       $this->excel->getActiveSheet()->setCellValue("C".$k, $row['USER_EMAIL']); 
       $this->excel->getActiveSheet()->setCellValue("D".$k, $row['USER_TYPE_ID']); 
       $k++; 
      } 
      $filename='just_some_random_name.xls'; 
      header('Content-Type: application/vnd.ms-excel'); 
      header('Content-Disposition: attachment;filename="'.$filename.'"'); 
      header('Cache-Control: max-age=0'); //no cache 

      $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5'); 

      $objWriter->save('php://output'); 
     } 

希望它能幫助你。

+0

對於此代碼,我需要添加任何核心模塊,或者我只是我可以使用它。 –

+0

您必須下載PHPExcel庫,然後放入third_party文件夾並將此代碼添加到您的模塊中。 –

+0

它會工作我正在使用這個。 –

0

嗨@Jagan阿卡什檢查這一點,

public function test_excel() 
    { 
     $this->load->library('excel'); 
     $this->excel->setActiveSheetIndex(0); 
     $this->excel->getActiveSheet()->setTitle('test worksheet'); 

     $this->excel->getActiveSheet()->setCellValue('A1', 'User id'); 
     $this->excel->getActiveSheet()->setCellValue('B1', 'User name'); 
     $this->excel->getActiveSheet()->setCellValue('C1', 'Email'); 
     $this->excel->getActiveSheet()->setCellValue('D1', 'Status'); 
     $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); 
     $this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true); 
     $this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true); 
     $this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true); 

     $query = $this->db->query("SELECT * FROM user limit 2"); 
     $k=2; 
     foreach($query->result_array() as $row) 
     { 
      $this->excel->getActiveSheet()->setCellValue("A".$k, $row['user_id']); 
      $this->excel->getActiveSheet()->setCellValue("B".$k, $row['first_name']); 
      $this->excel->getActiveSheet()->setCellValue("C".$k, $row['last_name']); 
      $this->excel->getActiveSheet()->setCellValue("D".$k, $row['user_email']); 
      $k++; 
     } 

     $init_cnt = $this->excel->getSheetCount(); 

     $this->excel->createSheet($init_cnt); 
     $this->excel->setActiveSheetIndex($init_cnt); 
     $this->excel->getActiveSheet()->setTitle('test 1'); 

     $this->excel->getActiveSheet()->setCellValue('A1', '1'); 
     $this->excel->getActiveSheet()->setCellValue('B1', '2'); 
     $this->excel->getActiveSheet()->setCellValue('C1', '3'); 
     $this->excel->getActiveSheet()->setCellValue('D1', '4'); 
     $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); 
     $this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true); 
     $this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true); 
     $this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true); 

     $query = $this->db->query("SELECT * FROM user limit 2,2"); 
     $k=2; 
     foreach($query->result_array() as $row) 
     { 
      $this->excel->getActiveSheet()->setCellValue("A".$k, $row['user_id']); 
      $this->excel->getActiveSheet()->setCellValue("B".$k, $row['first_name']); 
      $this->excel->getActiveSheet()->setCellValue("C".$k, $row['last_name']); 
      $this->excel->getActiveSheet()->setCellValue("D".$k, $row['user_email']); 
      $k++; 
     } 

     $init_cnt = $this->excel->getSheetCount(); 

     $this->excel->createSheet($init_cnt); 
     $this->excel->setActiveSheetIndex($init_cnt); 
     $this->excel->getActiveSheet()->setTitle('test 2'); 

     $this->excel->getActiveSheet()->setCellValue('A1', '12'); 
     $this->excel->getActiveSheet()->setCellValue('B1', '22'); 
     $this->excel->getActiveSheet()->setCellValue('C1', '32'); 
     $this->excel->getActiveSheet()->setCellValue('D1', '42'); 
     $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); 
     $this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true); 
     $this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true); 
     $this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true); 

     $query = $this->db->query("SELECT * FROM user limit 4,2"); 
     $k=2; 
     foreach($query->result_array() as $row) 
     { 
      $this->excel->getActiveSheet()->setCellValue("A".$k, $row['user_id']); 
      $this->excel->getActiveSheet()->setCellValue("B".$k, $row['first_name']); 
      $this->excel->getActiveSheet()->setCellValue("C".$k, $row['last_name']); 
      $this->excel->getActiveSheet()->setCellValue("D".$k, $row['user_email']); 
      $k++; 
     } 

     $this->excel->setActiveSheetIndex(0); 

     $filename='just_some_random_name.xls'; 
     header('Content-Type: application/vnd.ms-excel'); 
     header('Content-Disposition: attachment;filename="'.$filename.'"'); 
     header('Cache-Control: max-age=0'); 

     $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5'); 
     $objWriter->save('php://output'); 
    } 

這會給回答問題的全部。

相關問題