用PHPExcel和Codeigniter导出XLS时ID问题

时间:2018-10-10 00:21:59

标签: php postgresql codeigniter phpexcel

使用PHPExcelCodeigniter时,我无法导出XLS文件,导出没有错误,但是查询是带有另一个表的LEFT JOIN,这是PostgreSQL查询

SELECT * 
FROM consultas 
LEFT JOIN consulta_respuestas ON consultas."ID"= consulta_respuestas.consultaid 
LEFT JOIN usuarios ON consultas.responsable = usuarios."ID"

查询工作得很好,问题是在导出ID列来自usuarios表的文件时,我希望它来自Consultas表。

是否可以忽略usuarios表中的ID?

这是导出代码:

class Excel_export extends CI_Controller {

 function index()
 {
  $this->load->model("excel_export_model");
  $data["employee_data"] = $this->excel_export_model->fetch_data();
  $this->load->view("excel_export_view", $data);
 }

 function action()
 {
  $this->load->model("excel_export_model");
  $this->load->library("excel");
  $object = new PHPExcel();

  $object->setActiveSheetIndex(0);

  $table_columns = array("ID", "Fecha Consulta", "Nombre", "Consulta", "País", "Ciudad", "Escolaridad", "Ocupación", "Vía", "Responsable", "Respuesta", "Fecha Respuesta", "Palabra Clave");

  $column = 0;

  foreach($table_columns as $field)
  {
   $object->getActiveSheet()->setCellValueByColumnAndRow($column, 1, $field);
   $column++;
  }

  $employee_data = $this->excel_export_model->fetch_data();

  $excel_row = 2;

  foreach($employee_data as $row)
  {
   $object->getActiveSheet()->setCellValueByColumnAndRow(0, $excel_row, $row->ID);
   $object->getActiveSheet()->setCellValueByColumnAndRow(1, $excel_row, $row->consulta_fecha);
   $object->getActiveSheet()->setCellValueByColumnAndRow(2, $excel_row, $row->nombre);
   $object->getActiveSheet()->setCellValueByColumnAndRow(3, $excel_row, $row->consulta);
   $object->getActiveSheet()->setCellValueByColumnAndRow(4, $excel_row, $row->pais);
   $object->getActiveSheet()->setCellValueByColumnAndRow(5, $excel_row, $row->ciudad);
   $object->getActiveSheet()->setCellValueByColumnAndRow(6, $excel_row, $row->escolaridad);
   $object->getActiveSheet()->setCellValueByColumnAndRow(7, $excel_row, $row->ocupacion);
   $object->getActiveSheet()->setCellValueByColumnAndRow(8, $excel_row, $row->via);
   $object->getActiveSheet()->setCellValueByColumnAndRow(9, $excel_row, $row->username);
   $object->getActiveSheet()->setCellValueByColumnAndRow(10, $excel_row, $row->respuesta);
   $object->getActiveSheet()->setCellValueByColumnAndRow(11, $excel_row, $row->cierre_consulta);
   $object->getActiveSheet()->setCellValueByColumnAndRow(12, $excel_row, $row->palabraclave);
   $excel_row++;
  }

  $object_writer = PHPExcel_IOFactory::createWriter($object, 'Excel5');
  header('Content-Type: application/vnd.ms-excel');
  header('Content-Disposition: attachment;filename="Reporte.xls"');
  $object_writer->save('php://output');
 }

0 个答案:

没有答案