2016-03-05 95 views

回答

1

我已经使用PHPExcel完成了它。看看这个链接PHPExcel加载到您的库文件http://blog.mohamadikhwan.com/2013/01/integrating-phpexcel-into-codeigniter/

下面的代码是下载在Excel

$this->load->library('excel'); 
    //Create a new Object 
    $objPHPExcel = new PHPExcel(); 
    // Set the active Excel worksheet to sheet 0 
    $objPHPExcel->setActiveSheetIndex(0); 



    $heading=array('Name','DOB'); //set title in excel sheet 
    $rowNumberH = 1; //set in which row title is to be printed 
    $colH = 'A'; //set in which column title is to be printed 
    foreach($heading as $h){ 
     $objPHPExcel->getActiveSheet()->setCellValue($colH.$rowNumberH,$h); 
     $colH++;  
    } 


    $export_excel = $this->db->query("YOUR QUERY")->result_array(); 
    $rowCount = 2; // set the starting row from which the data should be printed 
    foreach($export_excel as $excel) 
    {   
     $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $excel['field name']); 
     $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $excel['field name']); 
     $rowCount++; 
    } 

    // Instantiate a Writer 
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5'); 

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

    $objWriter->save('php://output'); 
    exit(); 
+0

它的工作谢谢你,我想用白色和背景中的文字为黑色的标题单元格($标题)着色 –

1

值如果你想要的是你并不需要一个图书馆CSV数据。 Codeigniter支持开箱即用:

$this->load->dbutil(); 
$this->load->helper('file'); 
$this->load->helper('download'); 
$q   = $this->db->get($yourTableName); 
$delimiter = ","; 
$nuline = "\r\n"; 

force_download($yourTableName.'.csv', 
       $this->dbutil->csv_from_result($q, $delimiter, $nuline)); 
相关问题