我在CodeIgniter中扩展了一个处理PHPExcel的控制器。到目前为止,到目前为止,我还没有到Z列附近。到了之后,我受到了欢迎:
致命错误:未捕获的异常'异常',消息'无效的单元格 在/PATH/phpexcel/PHPExcel/Cell.php:513中协调AA'堆栈跟踪:#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','采购订单......')#3 [内部函数]:Export-> index()#4 /PATH/system/core/CodeIgniter.php(297):call_user_func_array(Array, 数组)#5 /PATH/index.php(163):require_once('/ PATH ...')#6 {main} 在第513行的/PATH/phpexcel/PHPExcel/Cell.php中抛出
我已经编写了一些代码来生成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);
第138行是:
$objPHPExcel->getActiveSheet()->setCellValue($array_column_indices[$x], $array_values_options['label']);
重申一下,在MySQL查询中添加更多列之前,一切都很顺利。
PHPExcel对我来说是个谜,所以任何帮助都会很精彩。
编辑6月11日,格林威治标准时间上午10:05
我对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;
虽然这已经解决了Z列问题,但我在电子表格中看到了奇怪的事情:
8 + 23 = 31行数据,这是正确的,但是为什么公式介于两者之间,还有差距?不知道。
所以,前进2步,后退1步。
答案 0 :(得分:3)
从堆栈转储中,您尝试获取单元格&#39; AA&#39;这不是有效的Excel单元格引用...它缺少行号:< / p>
PHPExcel_Worksheet->setCellValue('AA', 'Purchase Order ...')
请注意,有一个内置函数可以将列号转换为列名PHPExcel_Cell::stringFromColumnIndex()
,或者您可以利用PHP增加字符串PERL样式的能力,或者(如果您更喜欢使用列的数字)使用工作表的setCellValueByColumnAndRow()
方法
答案 1 :(得分:0)
最后,我的内容很接近,但需要进行一些调整:
$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;
此处,$columns
变量会阻止列运行。
此外,在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]);
完成!