99use PhpOffice \PhpSpreadsheet \Writer \Xlsx ;
1010use PhpOffice \PhpSpreadsheet \Writer \Csv ;
1111use PhpOffice \PhpSpreadsheet \Worksheet \Drawing ;
12+ use PhpOffice \PhpSpreadsheet \IOFactory ;
1213
1314/**
1415 * 导出导入Excel
@@ -22,10 +23,12 @@ class Excel
2223 /**
2324 * 导出Excel
2425 *
25- * @param array $list
26- * @param array $header
27- * @param string $filename
28- * @param string $title
26+ * @param array $list 数据
27+ * @param array $header 数据处理格式
28+ * @param string $filename 导出的文件名
29+ * @param string $suffix 导出的格式
30+ * @param string $path 导出的存放地址 无则不在服务器存放
31+ * @param string $image 导出的格式 可以用 大写字母 或者 数字 标识 哪一列
2932 * @return bool
3033 * @throws \PhpOffice\PhpSpreadsheet\Exception
3134 * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
@@ -244,62 +247,54 @@ public static function exportCsvData($list = [], $header = [], $filename = '')
244247 /**
245248 * 导入
246249 *
247- * @param $filePath
248- * @param int $startRow
250+ * @param $filePath excel的服务器存放地址 可以取临时地址
251+ * @param int $startRow 开始和行数
252+ * @param bool $hasImg 导出的时候是否有图片
253+ * @param string $suffix 格式
254+ * @param string $imageFilePath 作为临时使用的 图片存放的地址
249255 * @return array|mixed
250256 * @throws Exception
251257 * @throws \PhpOffice\PhpSpreadsheet\Exception
252258 * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
253259 */
254- public static function import ($ filePath , $ startRow = 1 )
260+ public static function import ($ filePath , $ startRow = 1 , $ hasImg = false , $ suffix = ' Xlsx ' , $ imageFilePath = null )
255261 {
256- $ reader = new \PhpOffice \PhpSpreadsheet \Reader \Xlsx ();
257- $ reader ->setReadDataOnly (true );
258- if (!$ reader ->canRead ($ filePath )) {
259- $ reader = new \PhpOffice \PhpSpreadsheet \Reader \Xls ();
260- // setReadDataOnly Set read data only 只读单元格的数据,不格式化 e.g. 读时间会变成一个数据等
261- $ reader ->setReadDataOnly (true );
262-
263- if (!$ reader ->canRead ($ filePath )) {
264- throw new Exception ('不能读取Excel ' );
262+ if ($ hasImg ){
263+ if ($ imageFilePath == null ){
264+ $ imageFilePath = '. ' . DIRECTORY_SEPARATOR . 'execlImg ' . DIRECTORY_SEPARATOR . \date ('Y-m-d ' ). DIRECTORY_SEPARATOR ;
265265 }
266+ if (!file_exists ($ imageFilePath )) {
267+ //如果目录不存在则递归创建
268+ mkdir ($ imageFilePath , 0777 , true );
269+ }
270+ }
271+ $ reader = IOFactory::createReader ($ suffix );
272+ if (!$ reader ->canRead ($ filePath )) {
273+ throw new Exception ('不能读取Excel ' );
266274 }
267275
268276 $ spreadsheet = $ reader ->load ($ filePath );
269- $ sheetCount = $ spreadsheet ->getSheetCount ();// 获取sheet的数量
277+ $ sheetCount = $ spreadsheet ->getSheetCount ();// 获取sheet(工作表)的数量
270278
271279 // 获取所有的sheet表格数据
272280 $ excleDatas = [];
273281 $ emptyRowNum = 0 ;
274282 for ($ i = 0 ; $ i < $ sheetCount ; $ i ++) {
275- $ currentSheet = $ spreadsheet ->getSheet ($ i ); // 读取excel文件中的第一个工作表
276- $ allColumn = $ currentSheet ->getHighestColumn (); // 取得最大的列号
277- $ allColumn = Coordinate::columnIndexFromString ($ allColumn ); // 由列名转为列数('AB'->28)
278- $ allRow = $ currentSheet ->getHighestRow (); // 取得一共有多少行
279-
280- $ arr = [];
281- for ($ currentRow = $ startRow ; $ currentRow <= $ allRow ; $ currentRow ++) {
282- // 从第1列开始输出
283- for ($ currentColumn = 1 ; $ currentColumn <= $ allColumn ; $ currentColumn ++) {
284- $ val = $ currentSheet ->getCellByColumnAndRow ($ currentColumn , $ currentRow )->getValue ();
285- $ arr [$ currentRow ][] = trim ($ val );
286- }
287-
288- // $arr[$currentRow] = array_filter($arr[$currentRow]);
289- // 统计连续空行
290- if (empty ($ arr [$ currentRow ]) && $ emptyRowNum <= 50 ) {
291- $ emptyRowNum ++ ;
292- } else {
293- $ emptyRowNum = 0 ;
294- }
295- // 防止坑队友的同事在excel里面弄出很多的空行,陷入很漫长的循环中,设置如果连续超过50个空行就退出循环,返回结果
296- // 连续50行数据为空,不再读取后面行的数据,防止读满内存
297- if ($ emptyRowNum > 50 ) {
298- break ;
283+ $ objWorksheet = $ spreadsheet ->getSheet ($ i ); // 读取excel文件中的第一个工作表
284+ $ data = $ objWorksheet ->toArray ();
285+ if ($ hasImg ){
286+ foreach ($ objWorksheet ->getDrawingCollection () as $ drawing ) {
287+ list ($ startColumn , $ startRow ) = Coordinate::coordinateFromString ($ drawing ->getCoordinates ());
288+ $ imageFileName = $ drawing ->getCoordinates () . mt_rand (1000 , 9999 );
289+ $ imageFileName .= '. ' .$ drawing ->getExtension ();
290+ $ source = imagecreatefromjpeg ($ drawing ->getPath ());
291+ imagejpeg ($ source , $ imageFilePath . $ imageFileName );
292+
293+ $ startColumn = self ::ABC2decimal ($ startColumn );
294+ $ data [$ startRow -1 ][$ startColumn ] = $ imageFilePath . $ imageFileName ;
299295 }
300296 }
301-
302- $ excleDatas [$ i ] = $ arr ; // 多个sheet的数组的集合
297+ $ excleDatas [$ i ] = $ data ; // 多个sheet的数组的集合
303298 }
304299
305300 // 这里我只需要用到第一个sheet的数据,所以只返回了第一个sheet的数据
@@ -310,6 +305,18 @@ public static function import($filePath, $startRow = 1)
310305 return $ returnData ;
311306 }
312307
308+ private static function ABC2decimal ($ abc ){
309+ $ ten = 0 ;
310+ $ len = strlen ($ abc );
311+ for ($ i =1 ;$ i <=$ len ;$ i ++){
312+ $ char = substr ($ abc ,0 -$ i ,1 );//反向获取单个字符
313+
314+ $ int = ord ($ char );
315+ $ ten += ($ int -65 )*pow (26 ,$ i -1 );
316+ }
317+ return $ ten ;
318+ }
319+
313320 /**
314321 * 格式化内容
315322 *
0 commit comments