ExcelModel.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/13
  6. * Time: 下午3:04
  7. */
  8. namespace app\backend\modules\order\services\models;
  9. class ExcelModel
  10. {
  11. protected function column_str($key)
  12. {
  13. $array = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ', 'BA', 'BB', 'BC', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BK', 'BL', 'BM', 'BN', 'BO', 'BP', 'BQ', 'BR', 'BS', 'BT', 'BU', 'BV', 'BW', 'BX', 'BY', 'BZ', 'CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CI', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CQ', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ'
  14. );
  15. return $array[$key];
  16. }
  17. protected function column($key, $columnnum = 1)
  18. {
  19. return $this->column_str($key) . $columnnum;
  20. }
  21. public function export($list, $params = array())
  22. {
  23. if (PHP_SAPI == 'cli') {
  24. die('This example should only be run from a Web Browser');
  25. }
  26. //ob_end_clean();
  27. require_once base_path() . '/vendor/phpoffice/phpexcel/Classes/PHPExcel.php';
  28. $excel = new \PHPExcel();
  29. $excel->getProperties()->setCreator("芸众商城")->setLastModifiedBy("芸众商城")->setTitle("Office 2007 XLSX Test Document")->setSubject("Office 2007 XLSX Test Document")->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")->setKeywords("office 2007 openxml php")->setCategory("report file");
  30. $sheet = $excel->setActiveSheetIndex(0);
  31. $rownum = 1;
  32. foreach ($params['columns'] as $key => $column) {
  33. $sheet->setCellValue($this->column($key, $rownum), $column['title']);
  34. if (!empty($column['width'])) {
  35. $sheet->getColumnDimension($this->column_str($key))->setWidth($column['width']);
  36. }
  37. }
  38. $rownum++;
  39. foreach ($list as $row) {
  40. $len = count($params['columns']);
  41. for ($i = 0; $i < $len; $i++) {
  42. $value = $row[$params['columns'][$i]['field']];
  43. $value = @iconv("utf-8", "gbk", $value);
  44. $value = @iconv("gbk", "utf-8", $value);
  45. $sheet->setCellValueExplicit($this->column($i, $rownum), $value, \PHPExcel_Cell_DataType::TYPE_STRING);
  46. }
  47. $rownum++;
  48. }
  49. $excel->getActiveSheet()->setTitle($params['title']);
  50. $filename = $params['title'] . '-' . date('Y-m-d H:i', time());
  51. header('Content-Type: application/vnd.ms-excel');
  52. header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
  53. header('Cache-Control: max-age=0');
  54. $writer = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
  55. $writer->save('php://output');
  56. exit;
  57. }
  58. }