用codeigniter输出Excel

我试图从我的codeigniter项目在Excel中得到一个报告,但我完全丧失了如何做到这一点。 它已经运行良好,只是想在Excel中的输出,而不是一个页面。

任何提示/指针/解释?

谢谢!

我将从codeIgniter网站或本教程中引用您的这篇wiki文章

使用PHPExcel库

把类文件夹放在你的codeigniter应用程序库中,并调用PHPExcel类

这与codeigniter正常工作

如果你需要快速和肮脏的东西(可能只在FF工作),我使用这个JS解决scheme:

function exportExcel(html) { window.open('data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent( '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=ProgId content=Excel.Sheet><style>body {font-family:Arial} .ean {mso-number-format:0000000000000;}</style></head><body><table>'+html.replace(/[♫^]/gi,'')+'</table></body></html>')); } 

然后在表格的标题标签中链接

 <a href="#" onclick="javascript:exportExcel($(this).parents(&quot;.table1&quot;).html());">Excel</a> 

它将以HTML工作表的forms打开,这对我很有用。 正如你可以在JSfunction代码中看到的,你可以添加样式到列和/或replace你不需要在输出中的一些字符。

将PHPExcel与codeigniter集成最简单的方法

  1. 首先从网站https://phpexcel.codeplex.com/下载Php Excel。

  2. 然后提取副本并放入codeignitor的application / third_party文件夹中。

  3. 然后转到文件夹应用程序/库并创build一个文件并将其命名为Excel.php。 并放置下面的代码:

    require_once APPPATH。“/ third_party / PHPExcel.php”; / /如果需要更改path。

    class Excel扩展了PHPExcel {public function __construct(){parent :: __ construct(); }}

4.现在创build一个类似于Export.php的控制器,并在其动作中放置代码:

 $this->load->library('Excel'); $query = $this->db->get('users'); $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties()->setTitle("export")->setDescription("none"); $objPHPExcel->setActiveSheetIndex(0); $col = 0; foreach ($header as $field) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field); $objPHPExcel->getActiveSheet()->getStyle('A1:Z1')->getFont()->setBold(true); $col++; } // Fetching the table data $row = 2; foreach($query as $data) { $col = 0; foreach ($fields as $field) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data[$field]); //change if required. $col++; } $row++; } $objPHPExcel->setActiveSheetIndex(0); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); // Sending headers to force the user to download the file header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'.'export_'.$table_name.'.xls"'); header('Cache-Control: max-age=0'); $objWriter->save('php://output');