ExcelTool.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Tool\MayouTool;
  3. use Tool\MayouTool\Excel\Export;
  4. use Tool\MayouTool\Exception\FileException;
  5. class ExcelTool
  6. {
  7. /**
  8. * 导出csv文件
  9. * @param array $headers 列名
  10. * @param array $datas 数据
  11. * @param string $fileName 文件名
  12. */
  13. public static function exportToCsv($headers,$datas,$fileName)
  14. {
  15. $export = new Export();
  16. $export->exportToCsv($headers,$datas,$fileName);
  17. }
  18. /**
  19. * 导出excel文件
  20. * @param array $headers 列名
  21. * @param array $datas 数据
  22. * @param string $fileName 文件名
  23. */
  24. public static function exportToExcel($headers,$datas,$fileName)
  25. {
  26. $export = new Export();
  27. $export->exportToExcel($headers,$datas,$fileName);
  28. }
  29. /**
  30. * 获取csv内容
  31. * @param string $filePath 文件内容
  32. */
  33. public static function importCsv($filePath)
  34. {
  35. $handle = fopen($filePath,"r");
  36. if(!$handle){
  37. throw new FileException("文件打开失败");
  38. }
  39. $list=[];
  40. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  41. $num = count($data);
  42. for ($c=0; $c < $num; $c++) {
  43. $arr[]=$data[$c];
  44. }
  45. $list[]=$arr;
  46. unset($arr);
  47. }
  48. fclose($handle);
  49. return $list;
  50. }
  51. /**
  52. * 获取excel的内容
  53. * @param $filePath
  54. */
  55. public static function importExcel($filePath)
  56. {
  57. $export = new Export();
  58. return $export->importExcel($filePath);
  59. }
  60. }