我目前正在使用PHPExcel将订单信息输出到Excel。
我有以下foreach语句,最终生成我的数据,例如订单所在的城市,餐馆名称等。
原谅嵌套的foreach循环 - 这是我可以通过所有许多城市和法律实体和餐馆来生成客户想要的数据的唯一方式。
使用动态生成行和列整数的帖子here,我在我的代码中尝试了它。
$row = 1; // 1-based index
$col = 0;
foreach ($this->data['total_by_city'] as $city_id => $total_city){
$city_name = '';
foreach ($this->data['total_by_legal_entities'] as $legal_entity_id => $total_legal_entity) {
$legal_entity_name = '';
foreach ($this->data['restaurant_by_legal_entities'][$legal_entity_id] as $restaurant_id) {
$orders = $this->data['order_by_restaurants'][$restaurant_id];
$restaurant_name = '';
if ($orders)
{
foreach ($orders as $order_id => $order)
{
$restaurant_name = $order['restaurant_name'];
$legal_entity_name = $order['legal_entity'];
$city_name = $order['city'];
echo $row . ", ". $col . "<br>";
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['city']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['legal_entity']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['restaurant_name']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['payment_method']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_number']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['date_created']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['customer_name']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_type']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['amount']);
$row++;
echo $row . ", ". $col . "<br>";
$col = 0;
}
}
}
}
}
当我运行我的代码并尝试打开.xlsx文件时,我收到了一个损坏的Excel电子表格&#34; Excel中的错误。
为了确保我的行和列(分别)索引正确,我将它们打印出来:
1, 0
2, 8
2, 0
3, 8
3, 0
4, 8
4, 0
5, 8
5, 0
6, 8
6, 0
7, 8
7, 0
8, 8
8, 0
9, 8
9, 0
10, 8
10, 0
11, 8
11, 0
12, 8
12, 0
13, 8
13, 0
14, 8
14, 0
15, 8
15, 0
16, 8
从这个观察中,我看到我使用的行和列索引是不正确的。它们都处于错误的位置并且错误地重置/递增。
我的问题是 - 如何正确地增加和重置我的列和行索引?
答案 0 :(得分:3)
你忘了增加列索引我猜
尝试以下
$row = 1; // 1-based index
$col = 0;
foreach ($this->data['total_by_city'] as $city_id => $total_city){
$city_name = '';
foreach ($this->data['total_by_legal_entities'] as $legal_entity_id => $total_legal_entity) {
$legal_entity_name = '';
foreach ($this->data['restaurant_by_legal_entities'][$legal_entity_id] as $restaurant_id) {
$orders = $this->data['order_by_restaurants'][$restaurant_id];
$restaurant_name = '';
if ($orders)
{
foreach ($orders as $order_id => $order)
{
$restaurant_name = $order['restaurant_name'];
$legal_entity_name = $order['legal_entity'];
$city_name = $order['city'];
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['city']);
$col++; // Increment for each Cell
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['legal_entity']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['restaurant_name']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['payment_method']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_number']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['date_created']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['customer_name']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_type']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['amount']);
}
$col=0;
$row++;
}
}
}
注意 - 在excel中,每个Cell应该具有唯一的RowID和ColumnID
答案 1 :(得分:2)
使用KCdod的答案,我得到了它的工作:
$row = 1; // 1-based index
$col = 0;
foreach ($this->data['total_by_city'] as $city_id => $total_city){
$city_name = '';
foreach ($this->data['total_by_legal_entities'] as $legal_entity_id => $total_legal_entity) {
$legal_entity_name = '';
foreach ($this->data['restaurant_by_legal_entities'][$legal_entity_id] as $restaurant_id) {
$orders = $this->data['order_by_restaurants'][$restaurant_id];
$restaurant_name = '';
if ($orders)
{
foreach ($orders as $order_id => $order)
{
$restaurant_name = $order['restaurant_name'];
$legal_entity_name = $order['legal_entity'];
$city_name = $order['city'];
echo $row . ", ". $col . "<br>";
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['city']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['legal_entity']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['restaurant_name']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['payment_method']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_number']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['date_created']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['customer_name']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_type']);
$col++;
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['amount']);
$row++;
echo $row . ", ". $col . "<br>";
$col = 0;
}
}
}
}
}
答案 2 :(得分:0)
我正在使用from_array:
$arrayData = array(
array(NULL, 2010, 2011, 2012),
array('Q1', 12, 15, 21),
array('Q2', 56, 73, 86),
array('Q3', 52, 61, 69),
array('Q4', 30, 32, 0),);
$objPHPExcel->getActiveSheet()->fromArray(
$arrayData, // The data to set
NULL, // Array values with this value will not be set
'C3' ); // Top left coordinate of the worksheet range where we want to set these values (default is A1)
在使用PHP的项目中,我在多维数组中分配了值,这个函数运行正常。
感谢。