PHPexcel.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. require_once('../lib/PHPExcel.php'); // 引入PHPExcel库
  3. $objPHPExcel = new PHPExcel(); // 创建PHPExcel对象,即创建一个Excel文件
  4. $objPHPExcel->setActiveSheetIndex(0); // 设置表格缺省编码为utf8
  5. $objPHPExcel->getActiveSheet()->setTitle('订单列表'); // 设置工作表标题
  6. // 设置表头
  7. $objPHPExcel->getActiveSheet()->setCellValue('A1', '订单ID');
  8. $objPHPExcel->getActiveSheet()->setCellValue('B1', '订单号');
  9. // $objPHPExcel->getActiveSheet()->setCellValue('C1', '订单来源');
  10. $objPHPExcel->getActiveSheet()->setCellValue('D1', '商品名称');
  11. $objPHPExcel->getActiveSheet()->setCellValue('E1', '姓名');
  12. $objPHPExcel->getActiveSheet()->setCellValue('F1', '身份证号');
  13. $objPHPExcel->getActiveSheet()->setCellValue('G1', '手机号码');
  14. $objPHPExcel->getActiveSheet()->setCellValue('H1', '省份');
  15. $objPHPExcel->getActiveSheet()->setCellValue('I1', '城市');
  16. $objPHPExcel->getActiveSheet()->setCellValue('J1', '县区');
  17. $objPHPExcel->getActiveSheet()->setCellValue('K1', '详细地址');
  18. $objPHPExcel->getActiveSheet()->setCellValue('L1', '处理状态');
  19. $objPHPExcel->getActiveSheet()->setCellValue('M1', '号码状态');
  20. $objPHPExcel->getActiveSheet()->setCellValue('N1', '备注');
  21. $objPHPExcel->getActiveSheet()->setCellValue('O1', '下单时间');
  22. // 填充数据
  23. include_once("../untils/conn.php");
  24. mysqli_query($con, "set names utf8");
  25. if ($con) {
  26. if ($db) {
  27. $sql = "SELECT oder.id,oder.oderid, oder.channel, oder.goods, oder.name, oder.uid, oder.phone, oder.sf, oder.city, oder.xian, oder.dizhi, oder.status, oder.beizhu, oder.time, oder.card_sta, channel.channel_name
  28. FROM oder
  29. LEFT JOIN channel
  30. ON oder.channel = channel.channel_id
  31. ORDER BY oder.id DESC";
  32. $data = mysqli_query($con, $sql);
  33. $num = 2; // 从第二行开始输出数据
  34. while ($row = mysqli_fetch_array($data)) {
  35. $status = '';
  36. switch ($row['status']) {
  37. case '0':
  38. $status = '未处理';
  39. break;
  40. case '1':
  41. $status = '已下单';
  42. break;
  43. case '2':
  44. $status = '已发货';
  45. break;
  46. case '3':
  47. $status = '开卡失败';
  48. break;
  49. }
  50. $card_sta = '';
  51. switch ($row['card_sta']) {
  52. case '0':
  53. $card_sta = '未激活';
  54. break;
  55. case '1':
  56. $card_sta = '已激活';
  57. break;
  58. }
  59. // 将内容强制设置为文本格式
  60. $objPHPExcel->getActiveSheet()->setCellValueExplicit('A'.$num, $row['id'], PHPExcel_Cell_DataType::TYPE_STRING);
  61. $objPHPExcel->getActiveSheet()->setCellValueExplicit('B'.$num, $row['oderid'], PHPExcel_Cell_DataType::TYPE_STRING);
  62. // $objPHPExcel->getActiveSheet()->setCellValueExplicit('C'.$num, $row['channel'], PHPExcel_Cell_DataType::TYPE_STRING);
  63. $objPHPExcel->getActiveSheet()->setCellValueExplicit('D'.$num, $row['goods'], PHPExcel_Cell_DataType::TYPE_STRING);
  64. $objPHPExcel->getActiveSheet()->setCellValueExplicit('E'.$num, $row['name'], PHPExcel_Cell_DataType::TYPE_STRING);
  65. $objPHPExcel->getActiveSheet()->setCellValueExplicit('F'.$num, $row['uid'], PHPExcel_Cell_DataType::TYPE_STRING);
  66. $objPHPExcel->getActiveSheet()->setCellValueExplicit('G'.$num, $row['phone'], PHPExcel_Cell_DataType::TYPE_STRING);
  67. $objPHPExcel->getActiveSheet()->setCellValueExplicit('H'.$num, $row['sf'], PHPExcel_Cell_DataType::TYPE_STRING);
  68. $objPHPExcel->getActiveSheet()->setCellValueExplicit('I'.$num, $row['city'], PHPExcel_Cell_DataType::TYPE_STRING);
  69. $objPHPExcel->getActiveSheet()->setCellValueExplicit('J'.$num, $row['xian'], PHPExcel_Cell_DataType::TYPE_STRING);
  70. $objPHPExcel->getActiveSheet()->setCellValueExplicit('K'.$num, $row['dizhi'], PHPExcel_Cell_DataType::TYPE_STRING);
  71. $objPHPExcel->getActiveSheet()->setCellValueExplicit('L'.$num, $status, PHPExcel_Cell_DataType::TYPE_STRING);
  72. $objPHPExcel->getActiveSheet()->setCellValueExplicit('M'.$num, $card_sta, PHPExcel_Cell_DataType::TYPE_STRING);
  73. $objPHPExcel->getActiveSheet()->setCellValueExplicit('N'.$num, $row['beizhu'], PHPExcel_Cell_DataType::TYPE_STRING);
  74. $objPHPExcel->getActiveSheet()->setCellValue('O'.$num, date('Y-m-d H:i:s', $row['time']));
  75. $num++;
  76. }
  77. }
  78. }
  79. // 设置文件名并为Excel文件格式添加扩展名
  80. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  81. $filename = 'order'.date('YmdHis').'.xlsx'; // 定义Excel名称,如:order20211019141825.xlsx
  82. // 下载Excel文件
  83. header('Content-Disposition: attachment;filename="'.$filename.'"');
  84. header('Content-Type: application/octet-stream');
  85. header('Content-Transfer-Encoding: binary');
  86. header('Cache-Control: must-revalidate');
  87. header('Pragma: public');
  88. header('Expires: 0');
  89. $objWriter->save('php://output'); // 将Excel文件输出给浏览器
  90. ?>