2010-10-29 59 views
1

好吧我正在動態生成單元格座標但我得到這個錯誤,爲什麼?無效的單元格座標phpExcel

錯誤:

Exception: Invalid cell coordinate. in /var/www/test/excel/pe/Classes/PHPExcel/Cell.php on line 490 

Call Stack: 
    0.0003  81132 1. {main}() /var/www/test/excel/createExcelReport.php:0 
    0.0225 2691000 2. PHPExcel_Worksheet->setCellValue() /var/www/test/excel/createExcelReport.php:92 
    0.0225 2691316 3. PHPExcel_Worksheet->getCell() /var/www/test/excel/pe/Classes/PHPExcel/Worksheet.php:841 
    0.0597 8336788 4. PHPExcel_Cell::coordinateFromString() /var/www/test/excel/pe/Classes/PHPExcel/Worksheet.php:940 

下面是代碼:

// Add some data 
echo date('H:i:s') . " Add some data<br />\n"; 
$objPHPExcel->setActiveSheetIndex(0); 

// *********************** TEST ************************************ // 

// Set the defaults 
$limit_col  = 6; 
$limit_row  = 10; 
$current_col = 'A'; 
$current_row = 1; 

// Create the row and column arrays starting at index 1 
$row_arr = array(1 => $current_row); 
$column_arr = array(1 => $current_col); 

// Build the column array 
while(count($column_arr) < $limit_col) { 
    $column_arr[] = ++$current_col; 
} 

// Build the row array 
while(count($row_arr) < $limit_row) { 
    $row_arr[] = ++$current_row; 
} 

//echo "Row<pre>".print_r($row_arr, true)."</pre><br />"; 
//echo "Column<pre>".print_r($column_arr, true)."</pre><br />"; 

foreach($row_arr as $row_number) { 
    $excel_matrix[$row_number] = $column_arr; 
} 

foreach($excel_matrix as $row_number => $column_position) { 
    foreach($column_position as $column_key => $column_value) { 
     $cell = (string)$row_number.''.$column_value; 
     echo "Cell: ".$cell."<br />\n"; 
     $objPHPExcel->getActiveSheet()->SetCellValue($cell, 'Hello'); 
    } 
} 

// *********************** TEST ************************************ // 


//$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello'); 
//$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!'); 
//$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello'); 
//$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!'); 
+0

檢查在拋出異常的代碼。它可能會提供深入瞭解發生了什麼... – ircmaxell 2010-10-29 18:03:43

+0

如果我硬編碼它的工作正常,值$ objPHPExcel-> getActiveSheet() - > SetCellValue('A1','你好'); – 2010-10-29 18:04:53

+0

然後嘗試'var_dump()''$ cell'變量。這些變化是它被設置爲除你認爲(或需要)以外的東西... – ircmaxell 2010-10-29 18:06:22

回答

1

跑過列/行的值是錯誤的。

應該是:

$cell = $column_value.$row_number; 
+0

doh !!這是一個容易犯的錯誤 – 2010-10-29 18:41:01