Excel.php 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\common;
  3. class Excel
  4. {
  5. public function __construct()
  6. {
  7. include_once(CMF_ROOT . '/extend/Excel/PHPExcel.php');
  8. }
  9. public function import($file = '', $cell = [], $crop = 0, $sheet = 0)
  10. {
  11. $file = iconv("utf-8", "gb2312", $file); //转码
  12. if (empty($file) OR !file_exists($file)) {
  13. die('file not exists!');
  14. }
  15. $objRead = new \PHPExcel_Reader_Excel2007(); //建立reader对象
  16. if (!$objRead->canRead($file)) {
  17. $objRead = new \PHPExcel_Reader_Excel5();
  18. if (!$objRead->canRead($file)) {
  19. die('No Excel!');
  20. }
  21. }
  22. $cellName = ['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'];
  23. $obj = $objRead->load($file); //建立excel对象
  24. $currSheet = $obj->getSheet($sheet); //获取指定的sheet表
  25. $columnH = $currSheet->getHighestColumn(); //取得最大的列号
  26. $columnCnt = array_search($columnH, $cellName);
  27. $rowCnt = $currSheet->getHighestRow(); //获取总行数
  28. $data = [];
  29. for ($_row = 1; $_row <= $rowCnt; $_row++) { //读取内容
  30. if ($_row > $crop) {
  31. for ($_column = 0; $_column <= $columnCnt; $_column++) {
  32. $cellId = $cellName[$_column] . $_row;
  33. $cellValue = $currSheet->getCell($cellId)->getValue();
  34. //$cellValue = $currSheet->getCell($cellId)->getCalculatedValue(); #获取公式计算的值
  35. if ($cellValue instanceof \PHPExcel_RichText) { //富文本转换字符串
  36. $cellValue = $cellValue->__toString();
  37. } else {
  38. $cellValue = (string)$cellValue;
  39. }
  40. if (!empty($cell[$_column])) {
  41. $data[$_row][$cell[$_column]] = $cellValue;
  42. } else {
  43. $data[$_row][] = $cellValue;
  44. }
  45. }
  46. }
  47. }
  48. return array_values($data);
  49. }
  50. /**
  51. * @param $expTitle
  52. * @param $expCellName [['a','A']]
  53. * @param $expTableData [['a'=>1]]
  54. * @param $textValue ['a']
  55. */
  56. public function export($expTitle, $expCellName, $expTableData, $textValue=[])
  57. {
  58. $xlsTitle = iconv('utf-8', 'gb2312', $expTitle); //文件名称
  59. $fileName = $expTitle . date('_YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
  60. $cellNum = count($expCellName);
  61. $dataNum = count($expTableData);
  62. $objPHPExcel = new \PHPExcel();
  63. $cellName = ['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'];
  64. for ($i = 0; $i < $cellNum; $i++) {
  65. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '1', $expCellName[$i][1]);
  66. }
  67. // Miscellaneous glyphs, UTF-8
  68. for ($i = 0; $i < $dataNum; $i++) {
  69. for ($j = 0; $j < $cellNum; $j++) {
  70. if (in_array($expCellName[$j][0],$textValue)) {
  71. $objPHPExcel->getActiveSheet(0)->setCellValueExplicit($cellName[$j] . ($i + 2), $expTableData[$i][$expCellName[$j][0]],\PHPExcel_Cell_DataType::TYPE_STRING);
  72. } else {
  73. $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 2), $expTableData[$i][$expCellName[$j][0]]);
  74. }
  75. }
  76. }
  77. header('pragma:public');
  78. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
  79. header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
  80. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  81. $objWriter->save('php://output');
  82. exit;
  83. }
  84. }