excel.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. error_reporting(0);
  3. /**
  4. * 导出excel(csv)
  5. * @data 导出数据
  6. * @headlist 第一行,列名
  7. * @fileName 输出Excel文件名
  8. */
  9. // $data = print_r(base64_encode(unserialize($_POST['post_data'])));
  10. //$data = array(array( "产品编号"=>"51","产品Id"=>"555"), array("产品Id"=>"665", "产品编号"=>"50"));
  11. // print_r($data);
  12. // }
  13. /*
  14. 语法mysqli_connect(host,username,passwd,dbname,port);
  15. mysqli_connect()方法具有返回值,返回值就是PHP与MySQL数据库建立连接的连接对象
  16. *host - MySQL数据库所在的计算机的ip地址
  17. * username - 登陆MySQL数据库的用户名称
  18. * passwd - 登陆MySQL数据库的用户密码(如果密码为空则写 “” 站位)
  19. * dbname - 要操作的数据库的名称
  20. * port - MySQL数据库所使用的端口号
  21. */
  22. //1.建立连接
  23. // $starttime=$_GET['val1'];
  24. // $endtime=$_GET['val2'];
  25. // $val3=$_GET['val3'];
  26. // $val4=$_GET['val4'];
  27. // $val5=$_GET['val5'];
  28. // $val6=$_GET['val6'];
  29. // $valArr = json_decode($_GET['vals']);
  30. // echo $valArr;
  31. $valArr = $_GET['vals'];
  32. $m=mysqli_connect('127.0.0.1','root','usestudio-1','NewShop','3306');
  33. //判断如果数据库连接不成功
  34. if ($m->connect_error) {
  35. //返回错误,结束运行,退出脚本。
  36. //echo json_encode(Array("Error" => $m->connect_errno . " - " . $m->connect_error));
  37. return;
  38. }
  39. //2.定义sql语句,设置字符集
  40. #$guid = trim(com_create_guid(),"{}");//随机生成的唯一标识码
  41. #$sql = "insert into jyc_test(UserId,UserName,UserPassWord,time) values('".$guid."','jyc','123456',now())";
  42. #$sql = "select * from jyc_test";
  43. //$sql = "CALL test_fc( '201810419327450') ";
  44. // $sql = "CALL SelectOrderInfo( $starttime, $endtime,$val3,$val4,$val5,$val6) ";
  45. // $sql = "CALL SelectOrderInfo( '2018-10-11','2018-10-12','','','1','10000') ";
  46. $sql = "CALL PrintExcel('".$valArr."');";
  47. //设置字符集
  48. $m->set_charset('utf8');
  49. $m->query("set character set 'utf8';");
  50. //执行存储过程语句
  51. // $res = $m->query($sql);
  52. //判断返回值的类型是否是布尔型,如果是布尔型,返回字符串"true";
  53. $arr = array();
  54. $arr1 = array();
  55. $all_rows = array(); //定义一个数组,用于存放结果集
  56. $r = array();
  57. $m -> multi_query(urldecode($sql));
  58. $s = array();
  59. if ($res=$m -> store_result()){
  60. while($row = $res->fetch_array(MYSQLI_ASSOC)){
  61. // array_push($s, $v);
  62. $arr["OrderId"]="A10107A";
  63. $arr["LogisticsChannel"]=" ";
  64. $arr["OrderNum"]="\t".$row['OrderNum'];
  65. $arr['UserReallyName']=$row['UserReallyName'];
  66. $arr['Address']=$row['Address'].$row['City'].$row['Province'];
  67. $arr['1']=" ";
  68. $arr['2']=" ";
  69. $arr['Area']=$row['Area'];
  70. $arr['City']=$row['City'];
  71. $arr['Province']=$row['Province'];
  72. $arr['CountryCode']="TH";
  73. $arr['PostalCode']=$row['PostalCode'];
  74. $arr['UserPhoneNum']=$row['UserPhoneNum'];
  75. $arr['IsCOD']="Y";
  76. $arr['Payment']=$row['Payment'];
  77. $arr['Service1']=" ";
  78. $arr['CurrencyCode']="THB";
  79. $arr['DeclaredValue']=$row['Amount'];
  80. $arr['Weight']="0.5";
  81. $arr['Volume']="26*17*16*1";
  82. $arr['IsInsured']="N";
  83. $arr['Insurance']=" ";
  84. $arr['Sensitive']="N";
  85. $arr['ServiceCode']="PDO";
  86. $arr['Remarks']=$row['Remarks'];
  87. $arr['ProductRemarks']=$row['ProductRemarks'];
  88. $arr['EnglishName']=' ';//$row['EnglishName'];
  89. $arr['SKU']=" ";
  90. $arr['attribute1']=" ";
  91. $arr['ProductIntroduce']=$row['ProductIntroduce'];
  92. $arr['ProductNumber']=$row['ProductNumber'];
  93. $arr['ProductPrice']=$row['ProductPrice'];
  94. //把结果集放入到数据中
  95. // array_push($all_rows, $row);
  96. array_push($arr1,$arr);
  97. // print_r($arr);
  98. }
  99. // // array_push($r, $s);
  100. }
  101. // $j = json_encode(count($r) > 1 ? $r : (count($r) == 0 ? $m -> affected_rows : $r[0]));
  102. // print_r($r);
  103. // echo($arr[Address]);
  104. //关闭数据库连接
  105. mysqli_close($m);
  106. $data = $arr1;
  107. $headlist= array('商家编码', '物流渠道', '销售订单号', '收件人姓名', '收件人地址','','', '收件人区/县', '收件人城市', '收件人省份','收件人国家二字简码','收件人邮编','收件人电话','是否COD','应收款额','增值服务','币种','货值','包裹重量','客户自测体积','是否保险','保额','是否有敏感货:Y/N','PDO ','备注','报关商品名称','报关商品英文名称','商家商品编码','产品属性(如颜色,尺寸)','商品销售链接','包裹内商品数量','单价');
  108. // print_r($headlist);
  109. $fileName='商家订单';
  110. // function csv_export($data = array(), $headlist = array(), $fileName) {
  111. header('Content-Type: text/csv; charset=utf-8');
  112. header('Content-Disposition: attachment;filename="'.$fileName.'.csv"');
  113. header('Cache-Control: max-age=0');
  114. //打开PHP文件句柄,php://output 表示直接输出到浏览器
  115. $fp = fopen('php://output', 'a');
  116. // fwrite($fp,xEFxBBxBF);
  117. fwrite($fp,chr(0xEF).chr(0xBB).chr(0xBF));
  118. //输出Excel列名信息
  119. foreach ($headlist as $key => $value) {
  120. //CSV的Excel支持GBK编码,一定要转换,否则乱码
  121. $headlist[$key] = iconv('gbk', 'utf-8', $value);
  122. }
  123. //将数据通过fputcsv写到文件句柄
  124. fputcsv($fp, $headlist);
  125. $count = count($data);
  126. for ($i = 0; $i < $count; $i++) {
  127. $row = $data[$i];
  128. foreach ($row as $key => $value) {
  129. //CSV的Excel支持GBK编码,一定要转换,否则乱码
  130. $row[$key] = iconv('utf-8', 'utf-8', $value);
  131. }
  132. fputcsv($fp, $row);
  133. }
  134. fclose($fp);
  135. ?>