Use Phpexcel to export data to excel in CI

Source: Internet
Author: User
  1. Class Table_export extends Ci_controller {

  2. function __construct ()

  3. {
  4. Parent::__construct ();

  5. Here you should add some sort of user validation

  6. To prevent strangers from pulling your table data
  7. }

  8. Function index ($table _name)

  9. {
  10. $this->load->database ();
  11. $query = $this->db->query ("select * from ' $table _name ' WHERE del= 1");
  12. $query = mb_convert_encoding ("gb2312", "UTF-8", $query);
  13. if (! $query)
  14. return false;

  15. Starting the Phpexcel Library

  16. $this->load->library (' Phpexcel ');
  17. $this->load->library (' phpexcel/iofactory ');

  18. $objPHPExcel = new Phpexcel ();

  19. $objPHPExcel->getproperties ()->settitle ("Export")->setdescription ("none");

  20. $objPHPExcel->setactivesheetindex (0)

  21. ->setcellvalue (' A1 ', iconv (' GBK ', ' utf-8 ', ' Chinese Hello '))
  22. ->setcellvalue (' B2 ', ' world! ')
  23. ->setcellvalue (' C1 ', ' Hello ');
  24. Field names in the first row
  25. $fields = $query->list_fields ();
  26. $col = 0;
  27. foreach ($fields as $field)
  28. {
  29. $objPHPExcel->getactivesheet ()->setcellvaluebycolumnandrow ($col, 1, $field);
  30. $col + +;
  31. }

  32. Fetching the table data

  33. $row = 2;
  34. foreach ($query->result () as $data)
  35. {
  36. $col = 0;
  37. foreach ($fields as $field)
  38. {
  39. $objPHPExcel->getactivesheet ()->setcellvaluebycolumnandrow ($col, $row, $data-$field);
  40. $col + +;
  41. }

  42. $row + +;

  43. }

  44. $objPHPExcel->setactivesheetindex (0);

  45. $objWriter = Iofactory::createwriter ($objPHPExcel, ' Excel5 ');

  46. Send headers force users to download files

  47. Header (' Content-type:application/vnd.ms-excel ');
  48. Header (' content-disposition:attachment;filename= ' products_ '. Date (' DMy '). XLS "');
  49. Header (' cache-control:max-age=0 ');

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

  51. }
  52. }
  53. ?>

Copy Code

Join the database with a table named products, and then access http://www.yourwebsite.com/table_export/index/products to export the Excel file.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.