当Excel表格的列数大于26列时,表格的最大列数$allColumn会是“A+A”,循环列的时候是范围是ord(A)到ord($allColumn);但是ord($allColumn)=65=ord(A),所以会导致数据丢失。
研究了一下自带的import方法,一下是解决代码
for ($currentRow = 1; $currentRow <= 1; $currentRow++) { //判断是否超过26列 if(strlen($allColumn)>=2){ //先处理前面26列 for ($currentColumn = ord('A'); $currentColumn <= ord('Z'); $currentColumn++) { $val = $currentSheet->getCellByColumnAndRow($currentColumn - 65, $currentRow)->getValue(); $fields[] = $val; } //超过26列的部分处理 for ($currentColumn = ord('A'); $currentColumn <= ord(substr($allColumn,1)); $currentColumn++) { $val = $currentSheet->getCellByColumnAndRow($currentColumn - 65+26, $currentRow)->getValue(); $fields[] = $val; } } //未超过26列时处理 else{ for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) { $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue(); $fields[] = $val; } } } $insert = []; for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) { $values = []; if(strlen($allColumn)>=2){ for ($currentColumn = ord('A'); $currentColumn <= ord('Z'); $currentColumn++) { $val = $currentSheet->getCellByColumnAndRow($currentColumn - 65, $currentRow)->getValue(); $values[] = is_null($val) ? '' : $val; } for ($currentColumn = ord('A'); $currentColumn <= ord(substr($allColumn,1)); $currentColumn++) { $val = $currentSheet->getCellByColumnAndRow($currentColumn - 65+26, $currentRow)->getValue(); $values[] = is_null($val) ? '' : $val; } } else{ for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) { $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue(); /* * ord()将字符转为十进制数 */; $values[] = is_null($val) ? '' : $val; // echo iconv('utf-8','gb2312', $val)."t"; } }```
希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。