Export.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Tool\ShanTaoTool\Excel;
  3. use PhpOffice\PhpSpreadsheet\IOFactory;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. class Export
  6. {
  7. /**
  8. * params $headerList 头部列表信息(一维数组) 必传
  9. * params $data 导出的数据(二维数组) 必传
  10. * params $filename 文件名称转码 必传
  11. * PS:出现数字格式化情况,可添加看不见的符号,使其正常,如:"\t"
  12. **/
  13. public function exportToCsv($headerList = [] , $data = [] , $fileName = ''){
  14. //文件名称转码
  15. // $fileName = iconv('UTF-8', 'GBK', $fileName);
  16. //设置header头
  17. header('Content-Type: application/vnd.ms-excel');
  18. header('Content-Disposition: attachment;filename=' . $fileName . '.csv');
  19. header('Cache-Control: max-age=0');
  20. //打开PHP文件句柄,php://output,表示直接输出到浏览器
  21. $fp = fopen("php://output","a");
  22. fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF)); // 添加 BOM
  23. //输出Excel列表名称信息
  24. foreach ($headerList as $key => $value) {
  25. // $headerList[$key] = iconv('UTF-8', 'GBK', $value);//CSV的EXCEL支持BGK编码,一定要转换,否则乱码
  26. $headerList[$key] = $value;//CSV的EXCEL支持BGK编码,一定要转换,否则乱码
  27. }
  28. //使用fputcsv将数据写入文件句柄
  29. fputcsv($fp, $headerList);
  30. //计数器
  31. $num = 0;
  32. //每隔$limit行,刷新一下输出buffer,不要太大亦不要太小
  33. $limit = 100000;
  34. //逐行去除数据,不浪费内存
  35. $count = count($data);
  36. for($i = 0 ; $i < $count ; $i++){
  37. $num++;
  38. //刷新一下输出buffer,防止由于数据过多造成问题
  39. if($limit == $num){
  40. ob_flush();
  41. flush();
  42. $num = 0;
  43. }
  44. $row = $data[$i];
  45. foreach ($row as $key => $value) {
  46. $value = is_numeric($value) ? $value."\t" : $value;//解决输出长度较长的数字型变成科学计数法
  47. $row[$key] = $value;
  48. // $row[$key] = iconv('UTF-8', 'GBK', $value);
  49. }
  50. fputcsv($fp, $row);
  51. }
  52. }
  53. public function exportToExcel($headerList = [] , $datas = [] , $fileName = '')
  54. {
  55. $filename = $fileName.'.xlsx';
  56. header('Content-Type: application/vnd.ms-excel');
  57. header('Content-Disposition: attachment;filename="'.$filename.'"');
  58. header('Cache-Control: max-age=0');
  59. $spreadsheet = new Spreadsheet();
  60. $worksheet = $spreadsheet->getActiveSheet();
  61. $worksheet->setTitle($fileName);
  62. foreach ($headerList as $key=>$header){
  63. //设置标题
  64. $worksheet->setCellValueByColumnAndRow((int)$key+1, 1, $header);
  65. }
  66. foreach ($datas as $kk=>$data){
  67. $tmp = 0;
  68. foreach ($data as $k=>$val){
  69. //设置数据
  70. $worksheet->setCellValueByColumnAndRow((int)$tmp+1,(int)$kk+2,$val);
  71. $tmp++;
  72. }
  73. }
  74. $writer =IOFactory::createWriter($spreadsheet, 'Xlsx');
  75. $writer->save('php://output');
  76. }
  77. public function importExcel($file)
  78. {
  79. $reader = IOFactory::createReader('Xlsx');
  80. //打开文件、载入excel表格
  81. $spreadsheet = $reader->load($file);
  82. # 获取活动工作薄
  83. return $spreadsheet->getActiveSheet()->toArray();
  84. }
  85. }