php PHPExcel:无效的单元格坐标错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24147526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPExcel: Invalid cell coordinate error
提问by Wayne Smallman
I'm extending a controller in CodeIgniter that handles PHPExcel. So far, I've not gotten anywhere near column Z, until now. Having arrived, I'm greeted with:
我在处理 PHPExcel 的 CodeIgniter 中扩展了一个控制器。到目前为止,我还没有接近 Z 列,直到现在。到达后,我受到了欢迎:
Fatal error: Uncaught exception 'Exception' with message 'Invalid cell coordinate AA' in /PATH/phpexcel/PHPExcel/Cell.php:513 Stack trace: #0 /PATH/phpexcel/PHPExcel/Worksheet.php(1119): PHPExcel_Cell::coordinateFromString('AA') #1 /PATH/phpexcel/PHPExcel/Worksheet.php(1022): PHPExcel_Worksheet->getCell('AA') #2 /PATH/application/modules/bookings/controllers/export.php(138): PHPExcel_Worksheet->setCellValue('AA', 'Purchase Order ...') #3 [internal function]: Export->index() #4 /PATH/system/core/CodeIgniter.php(297): call_user_func_array(Array, Array) #5 /PATH/index.php(163): require_once('/PATH...') #6 {main} thrown in /PATH/phpexcel/PHPExcel/Cell.php on line 513
致命错误:在 /PATH/phpexcel/PHPExcel/Cell.php:513 堆栈跟踪:#0 /PATH/phpexcel/PHPExcel/Worksheet.php(1119): PHPExcel_Cell: :coordinateFromString('AA') #1 /PATH/phpexcel/PHPExcel/Worksheet.php(1022): PHPExcel_Worksheet->getCell('AA') #2 /PATH/application/modules/bookings/controllers/export.php(138 ): PHPExcel_Worksheet->setCellValue('AA', 'Purchase Order ...') #3 [内部函数]:Export->index() #4 /PATH/system/core/CodeIgniter.php(297): call_user_func_array( Array, Array) #5 /PATH/index.php(163): require_once('/PATH...') #6 {main} 在第 513 行的 /PATH/phpexcel/PHPExcel/Cell.php 中抛出
I've written some code to generate AB-AZ BB-BZ columns, but — as you can see above — PHPExcel isn't having it.
我已经编写了一些代码来生成 AB-AZ BB-BZ 列,但是 - 正如您在上面看到的 - PHPExcel 没有它。
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_sqlite;
$cacheSettings = array('memoryCacheSize' => '32MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle($arrayAttr['form']['title']);
$objPHPExcel->getProperties()->setDescription("Export of Advanced Search");
$objPHPExcel->getProperties()->setCreator("Me!");
$objPHPExcel->getProperties()->setLastModifiedBy("Me!");
// Assign cell values
$objPHPExcel->setActiveSheetIndex(0);
$a=1;
$p=1;
for ($s=65; $s<=90; $s++):
// Assign the letters of the alphabet.
for ($i=65; $i<=90; $i++):
if (chr($s) == "A"):
$array_column_indices[$a] = chr($i) . $p;
else:
$array_column_indices[$a] = chr($s-1) . chr($i);
endif;
$a++;
endfor;
$p++;
endfor;
$b=2;
foreach ($arrayAttr['results']['data_search']['results'] as $index_search_advanced => $array_value_search_advanced):
$x=1;
foreach ($arrayAttr['results']['data_search']['results'][0] as $keys => $values):
if (array_key_exists($keys, $arrayAttr['results']['config_database_tables']['tables'][$arrayAttr['results']['group']]['bookings'])
&& ($keys != 'clients_options')):
$objPHPExcel->getActiveSheet()->setCellValue($array_column_indices[$x],
$arrayAttr['results']['config_database_tables']['tables'][$arrayAttr['results']['group']]['bookings'][$keys]);
$x++;
elseif (array_key_exists($keys, $arrayAttr['results']['config_database_tables']['tables'][$arrayAttr['results']['group']]['bookings_attendees'])
&& ($keys != 'clients_options')):
$objPHPExcel->getActiveSheet()->setCellValue($array_column_indices[$x],
$arrayAttr['results']['config_database_tables']['tables'][$arrayAttr['results']['group']]['bookings_attendees'][$keys]);
$x++;
elseif (array_key_exists($keys, $arrayAttr['results']['config_database_tables']['tables'][$arrayAttr['results']['group']]['venues'])
&& ($keys != 'clients_options')):
$objPHPExcel->getActiveSheet()->setCellValue($array_column_indices[$x],
$arrayAttr['results']['config_database_tables']['tables'][$arrayAttr['results']['group']]['venues'][$keys]);
$x++;
elseif (array_key_exists($keys, $arrayAttr['results']['config_database_tables']['tables'][$arrayAttr['results']['group']]['values'])
&& ($keys != 'clients_options')):
$objPHPExcel->getActiveSheet()->setCellValue($array_column_indices[$x],
$arrayAttr['results']['config_database_tables']['tables'][$arrayAttr['results']['group']]['values'][$keys]);
$x++;
elseif ($keys == 'clients_options'):
$array_options_for_booking = explode(chr(0x1D), $values);
$a_=0;
$array_options_sorted = array();
foreach ($array_options_for_booking as $option):
if (strstr($option, chr(0x1F))):
$array_options_sorted[] = implode('', array_slice(explode(chr(0x1F), $option), $a_, 1));
else:
$array_options_sorted[] = $option;
endif;
$a_++;
endforeach;
// Write the custom column values.
foreach ($array_options_fields as $index_options => $array_values_options):
$objPHPExcel->getActiveSheet()->setCellValue($array_column_indices[$x], $array_values_options['label']);
$x++;
endforeach;
endif;
endforeach;
// Keep a count of the actual columns within the results, rather than just the columns used when displaying the results.
$c=1;
foreach ($array_value_search_advanced as $value_search_advanced):
// "U" is the column where the custom values first occur.
if (strstr($array_column_indices[$c], "U")):
$array_options_for_booking = explode(chr(0x1D), $value_search_advanced);
$a_=0;
$array_options_sorted = array();
foreach ($array_options_for_booking as $option):
if (strstr($option, chr(0x1F))):
$array_options_sorted[] = implode('', array_slice(explode(chr(0x1F), $option), $a_, 1));
else:
$array_options_sorted[] = $option;
endif;
$a_++;
endforeach;
$d=$c;
$e=1;
// Write the custom column values.
foreach ($array_options_sorted as $index_options => $array_values_options):
$objPHPExcel->getActiveSheet()->setCellValue($array_column_indices[$d] . $b, $array_values_options);
$d++;
$e++;
endforeach;
$c=($d-1);
// Else, if the columns are Venue Rate...
elseif (strstr($array_column_indices[$c], "E")):
// ... apply the number format to the cells.
$objPHPExcel->getActiveSheet()->getStyle($array_column_indices[$c] . $b)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
// Write the regular column values.
$objPHPExcel->getActiveSheet()->setCellValue($array_column_indices[$c] . $b, $value_search_advanced);
// Else, if the Number of Nights...
elseif (strstr($array_column_indices[$c], "O")):
// ... apply the number format to the cells.
$objPHPExcel->getActiveSheet()->getStyle($array_column_indices[$c] . $b)->getNumberFormat()->setFormatCode();
// Write the regular column values.
$objPHPExcel->getActiveSheet()->setCellValue($array_column_indices[$c] . $b, $value_search_advanced);
else:
// Write the regular column values.
$objPHPExcel->getActiveSheet()->setCellValue($array_column_indices[$c] . $b, $value_search_advanced);
endif;
$c++;
endforeach;
$b++;
endforeach;
$z = ($b-1);
//print_r($objPHPExcel);
$objPHPExcel->getActiveSheet()->setCellValueExplicit("E" . $b, '=SUM(E2:E' . $z . ')');
$objPHPExcel->getActiveSheet()->setCellValueExplicit("O" . $b, '=SUM(O2:O' . $z . ')');
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Search Results.xls"');
// Save it as a Micrtosoft Excel 2003 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel5");
$objWriter->save("library/profiles/reports/spreadsheet.xls");
$objWriter->save('php://output');
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
Line 138 is:
第 138 行是:
$objPHPExcel->getActiveSheet()->setCellValue($array_column_indices[$x], $array_values_options['label']);
Just to reiterate, everything was smooth as silk until I added more columns in the MySQL query.
重申一下,在我在 MySQL 查询中添加更多列之前,一切都很顺利。
PHPExcel is something of a mystery to me, so any assistance would be wonderful.
PHPExcel 对我来说是个谜,所以任何帮助都会很棒。
Edit 11 June, 10:05am GMT
编辑 6 月 11 日,格林威治标准时间上午 10:05
I've made some changes to the for() loops:
我对 for() 循环做了一些更改:
$a=1;
$p=1;
$r=0;
for ($s=65; $s<=90; $s++):
// Assign the letters of the alphabet.
for ($i=65; $i<=90; $i++):
if (chr($s) == "A"):
$array_column_indices[$a] = chr($i) . $p;
else:
$array_column_indices[$a] = chr($s-1) . chr($i) . $p;
endif;
$a++;
endfor;
$r++;
endfor;
While this has gotten around the column Z problem, I'm seeing strange things in the spreadsheet:
虽然这已经解决了 Z 列问题,但我在电子表格中看到了一些奇怪的东西:
- After the column names in row 1, I have 10 row gap of empty cells.
- A block of 8 rows with data.
- 13 rows of empty cells.
- A row containing a number of cells with SUM() formula in them.
- Almost a 100 rows of empty cells.
- A block of 23 rows with data.
- 在第 1 行的列名之后,我有 10 行空白单元格。
- 包含数据的 8 行块。
- 13 行空单元格。
- 一行包含许多带有 SUM() 公式的单元格。
- 几乎有 100 行空单元格。
- 包含数据的 23 行块。
8 + 23 = 31 rows of data, which is correct, but as to why the formulas are in between, and the gaps? No idea.
8 + 23 = 31 行数据,这是正确的,但至于为什么公式介于两者之间,以及差距?不知道。
So, 2 steps forwards, 1 step backward.
所以,前进2步,后退1步。
回答by Mark Baker
From the stack dump, you're trying to get cell 'AA', which isn't a valid Excel cell reference... it's missing a row number:
从堆栈转储中,您试图获取单元格“AA”,这不是有效的 Excel 单元格引用……它缺少行号:
PHPExcel_Worksheet->setCellValue('AA', 'Purchase Order ...')
Note that there is a built-in function that can convert a column number to a column name, PHPExcel_Cell::stringFromColumnIndex()
or you can take advantage of PHP's ability to increment strings PERL-style, or (if you prefer to work with numbers for columns) use the worksheet's setCellValueByColumnAndRow()
method
请注意,有一个内置函数可以将列号转换为列名,PHPExcel_Cell::stringFromColumnIndex()
或者您可以利用 PHP 以 PERL 样式递增字符串的能力,或者(如果您更喜欢使用列号)使用工作表的setCellValueByColumnAndRow()
方法
回答by Wayne Smallman
In the end, what I had was close, but required a few tweaks:
最后,我所拥有的很接近,但需要进行一些调整:
$columns = ( ... count(of columns) ... );
$iterator_for_column_range=1;
// Assign the letters of the alphabet to the column ranges.
for ($x = 65; $x <= 90; $x++):
for ($y = 65; $y <= 90; $y++):
if (chr($x) == "A"):
$array_column_indices[$iterator_for_column_range] = chr($y);
else:
$array_column_indices[$iterator_for_column_range] = chr($x-1) . chr($y);
endif;
if ($iterator_for_column_range == $columns) break;
$iterator_for_column_range++;
endfor;
endfor;
Here, the $columns
variable stops the columns from running on.
在这里,$columns
变量阻止列继续运行。
Also, made changes in the foreach
loops to the iterator variables, so that they only iterate once each pass:
此外,在foreach
循环中对迭代器变量进行了更改,以便它们每次只迭代一次:
$objPHPExcel->getActiveSheet()->setCellValue($array_column_indices[**$index_of_columns_for_column_name**] . **$index_of_range**, $arrayAttr['results']['config_database_tables']['tables'][$arrayAttr['results']['group']]['bookings'][$keys]);
Done!
完毕!