diff --git a/composer.json b/composer.json index ce99ea6..73e892b 100755 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "spomky-labs/otphp": "^8.3.2", "christian-riesen/otp": "2.5.0", "swiftmailer/swiftmailer": "5.4.9", - "bacon/bacon-qr-code": "^1.0.3" + "bacon/bacon-qr-code": "^1.0.3", + "phpoffice/phpspreadsheet": "1.2.0" }, "autoload": { "psr-4": { diff --git a/src/ExcelUtil.php b/src/ExcelUtil.php new file mode 100755 index 0000000..851c9f7 --- /dev/null +++ b/src/ExcelUtil.php @@ -0,0 +1,193 @@ +getProperties()->setCreator("Maarten Balliauw") + ->setLastModifiedBy("Maarten Balliauw") + ->setTitle("Office 2007 XLSX Test Document") + ->setSubject("Office 2007 XLSX Test Document") + ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") + ->setKeywords("office 2007 openxml php") + ->setCategory("Test result file"); + + foreach ($titles as $key => $title) { + $workSheet = $objPHPExcel->createSheet($key); + // Set title + $workSheet->setTitle($title); + // Set Data. + foreach ($datas[$key] as $rowIndex => $row) { + $columnCount = count($row); + if ($columnCount > self::MAX_CREATE_COLUMN) { + array_splice($row, $columnCount); + } + foreach ($row as $columnIndex => $columnValue) { + $workSheet->setCellValue(self::getLetter($columnIndex) . ($rowIndex + 1), $columnValue); + } + } + } + + /** + * $type + * private static $writers = [ + * 'Xls' => Writer\Xls::class, + * 'Xlsx' => Writer\Xlsx::class, + * 'Ods' => Writer\Ods::class, + * 'Csv' => Writer\Csv::class, + * 'Html' => Writer\Html::class, + * 'Tcpdf' => Writer\Pdf\Tcpdf::class, + * 'Dompdf' => Writer\Pdf\Dompdf::class, + * 'Mpdf' => Writer\Pdf\Mpdf::class, + * ]; + */ + $objWriter = IOFactory::createWriter($objPHPExcel, $type); + $objWriter->save($localFileName); + return true; + } catch (\Exception $e) { + return false; + } + } + + /** + * 根据columnIndex获得标准Excel列号. + * 例如:$columnIndex=1,则返回B + * 例如:$columnIndex=26,则返回AA + * @param $columnIndex + * @return mixed|string + */ + private static function getLetter($columnIndex) + { + if ($columnIndex < self::LETTER_COUNT) { + return self::LETTER[$columnIndex]; + } else { + $letter1 = self::LETTER[(int)($columnIndex / self::LETTER_COUNT) - 1]; + $letter2 = self::LETTER[($columnIndex % self::LETTER_COUNT)]; + return $letter1 . $letter2; + } + } + + /** + * 读取Excel文件 + * @param $localFileName string 本地excel文件 + * @param array $sheets array + * @return array|bool + */ + public static function read($localFileName, $sheets = [0]) + { + try { + if (!$localFileName || !is_file($localFileName)) { + return false; + } + + $excelObject = IOFactory::load($localFileName); + if (!$excelObject) { + return false; + } + + //获取array结构化数据. + $cellStack = []; + foreach ($sheets as $index) { + $workSheetObject = $excelObject->getSheet($index); + //末列字母值 + $columnString = $workSheetObject->getHighestColumn(); + //行数 + $rowCount = $workSheetObject->getHighestRow(); + + //从第二行开始拿数据(首行为header) A1:AD1 + for ($i = 1; $i <= $rowCount; $i++) { + array_push($cellStack, $workSheetObject->rangeToArray('A' . $i . ':' . $columnString . $i)[0]); + } + } + + return $cellStack; + } catch (\Exception $e) { + return false; + } + } +} \ No newline at end of file