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);
- //$read->setReadDataOnly(true);
- $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)->getValue();
- $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());
- //插入代码
- //$startColumn = $this->decimal($startColumn);//由于图片所在位置的列号为字母,转化为数字
- $data[$startRow][$startColumn] = $pic['url'];
- }
- return $data;
- }
- }
|