123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php namespace Excel\Lib;
- use Dever;
- Dever::apply('autoload', 'excel', 'vendor');
- class Import extends Core
- {
- public function act($file = '', $sheet = 0, $offset = 0)
- {
- $file = mb_convert_encoding($file, "UTF-8", "gbk");
- if(empty($file) OR !file_exists($file)) {
- Dever::error('file not exists!');
- }
- try {
- $type = \PhpOffice\PhpSpreadsheet\IOFactory::identify($file);
- $read = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($type);
-
- $excel = $read->load($file);
- } catch (\Exception $e) {
- Dever::error('加载文件发生错误:"'.$file.'": '. $e->getMessage());
- }
- $sheet = $excel->getSheet($sheet);
- $columnH = $sheet->getHighestColumn();
- $columnCnt = array_search($columnH, $this->cell);
- if (!$columnCnt) {
- $columnCnt = $offset - 1;
- }
- if ($columnCnt < 0) {
- $columnCnt = 0;
- }
- $rowCnt = $sheet->getHighestRow();
-
- $data = array();
- for ($_row = 1; $_row <= $rowCnt; $_row++) {
- for ($_column = 0; $_column <= $columnCnt; $_column++) {
- $cellId = $this->cell[$_column].$_row;
-
- $cellValue = $sheet->getCell($cellId)->getCalculatedValue();
- if ($cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText\RichText) {
- $cellValue = $cellValue->__toString();
- }
- $data[$_row][$this->cell[$_column]] = $cellValue;
- }
- }
- $draws = $sheet->getDrawingCollection();
-
- foreach ($draws as $img) {
- list($startColumn, $startRow) = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::coordinateFromString($img->getCoordinates());
- $pic = Dever::load('save', 'upload')->act(1, $img->getPath());
-
-
- $data[$startRow][$startColumn] = $pic['url'];
- }
- return $data;
- }
- }
|