PHPExcel:将Excel插入SQL数据库时,单元格坐标A无效


PHPExcel: Invalid cell coordinate A when inserting Excel into SQL database

在PHP中,我循环遍历excel文件并将其插入MSSQL数据库。我得到这个错误:

未捕获异常"PHPExcel_exception",消息为"无效单元格坐标A"

如果我只运行循环中的一个查询,就不会出现这个错误。两个查询分别起作用。所以我确信这与这两个查询正在运行有关。使用以下代码,在两个表中都插入了一行,然后出现错误。关于如何解决这个问题有什么想法吗?

这是代码。。。

$dbc = odbc_connect(DB_DRIVER, DB_USER, DB_PASSWORD);
$inputFileName = 'lib/test.xlsx';
try {
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
//  Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn();
//  Loop through each row of the worksheet in turn
for ($row = 2; $row <= $highestRow; $row++){ 
//  Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                NULL,
                                TRUE,
                                FALSE);
$name = ms_escape_string($rowData[0][0]);
$city = ms_escape_string($rowData[0][2]);
$state = ms_escape_string($rowData[0][3]);
$phone = ms_escape_string($rowData[0][4]);
$website = ms_escape_string($rowData[0][5]);
$profit_status = ms_escape_string($rowData[0][6]);
$query = "insert into account2 ([name], [city], [state], [phone], [website], [type], [created_by], [last_modified_by]) 
values ('$name', '$city', '$state', '$phone', '$website', '6', '3', '3')
SELECT SCOPE_IDENTITY() AS ins_id";
$data = odbc_exec($dbc, $query);
if (odbc_next_result($data)){
    while ($row = odbc_fetch_object($data)) {
        $account_id = $row->ins_id;
    }
    $query = "insert into account_hic2 (account_id, profit_status)
values ('$account_id', '$profit_status')";
}
$data2 = odbc_exec($dbc, $query);
odbc_free_result($data);
odbc_free_result($data2);
}

错误消息表明您在某个时刻错误地设置了$row的值,可能将其设置为null或空字符串。。。

或者可能是资源,因为您在数据库中使用相同的变量名,在使用它来跟踪Excel行号的同一循环中获取