I am currently using PHPExcel to output an order's information to Excel.
I have the following foreach-statements that ultimate generate my data such as the city in which the order was made in, the restaurant name, etc.
Pardon the nested foreach-loops - it was the only way I could nest through all that many cities and legal entities and restaurants to generate the data the customer wanted.
Using an answer posted here which dynamically generates the row and column integer, I tried it in my code.
$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;
}
}
}
}
}
When I ran my code, and attempted to open the .xlsx file, I received a "corrupted Excel spreadsheet" error in Excel.
To make sure that my rows and column (respectively) indices are correct, I printed them out:
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
From that observation, I see that the rows and columns indices I am using are incorrect. They are both in the wrong location and reset/incremented incorrectly.
My question is - how do I increment and reset my column and row indices correctly?
You have forgot to increment Column index I guess
try following
$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++;
}
}
}
Note- in excel each Cell should have unique RowID and ColumnID
Using KCdod's answer, I got it to work:
$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;
}
}
}
}
}
I am using 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)
In a project with PHP, I assigned values in a multidimensional array, and this function works fine.
Thanks.
Related
I have a table that has 27 columns and I am using fpdf to create pdf file.
I wonder how can I make all the columns align right except the 1st 2?
Here is my code.
#Create the table
function BasicTable($header,$data) {
#Create the header.
foreach ($header as $col)
$this->Cell(18,5,$col,1);
$this->Ln();
#Get the data
foreach ($data as $row) {
foreach ($row as $col)
#$this->Cell(18,5,$col,1,'R');
$this->Cell(18,5, $col, 1, 0);
$this->Ln();
}
}
}
Update Code (Working)
#Create the table
function BasicTable($header,$data) {
#Create the header.
foreach ($header as $col)
$this->Cell(18,5,$col,1);
$this->Ln();
#Get the data
foreach ($data as $row) {
$cnt = 0;
foreach ($row as $col) {
if($cnt < 2){
$this->Cell(18,5,$col,1);
}
else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
$this->Ln();
}
}
}
You should check col value on each row,
#Create the table
function BasicTable($header,$data) {
#Create the header.
foreach ($header as $col)
$this->Cell(18,5,$col,1);
$this->Ln();
#Get the data
foreach ($data as $row) {
$cnt = 0;
foreach ($row as $col) {
if($cnt < 2){
$this->Cell(18,5,$col,1,'R');
}
else {
$this->Cell(18,5, $col, 1, 0);
}
$cnt++;
}
$this->Ln();
}
}
I also found extra "}" in your function.
Updated code based on post above
#Create the table
function BasicTable($header,$data) {
#Create the header.
foreach ($header as $col)
$this->Cell(18,5,$col,1);
$this->Ln();
#Get the data
foreach ($data as $row) {
$cnt = 0;
foreach ($row as $col) {
if($cnt < 2){
$this->Cell(18,5,$col,1);
}
else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
$this->Ln();
}
}
}
I want to select and write specific column names as I want how can I do that ?
the situation is :
$stockQuery = mysql_query("SELECT stokId,urunAdi,tlFiyat,stokMiktari,tlFiyat*stokMiktari,urunGrup,maliyetTipi FROM proje_stok where projeID =".$projeID);
but I do want to select lik
select * from proje_stok where ......
and my current writing code is
while($row_data = mysql_fetch_assoc($stockQuery)) {
$col = 0;
if ($col == 0) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order);
$col++;
}
foreach($row_data as $key=>$value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$col++;
}
$row++;
$order++;
}
Instead of that I want to use the code like
foreach($row_data as $key=>$value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value['HEREWHATIWANTTOPUTCOLUMNNAME']);
$col++;
}
is there any suggestion to deal with ?
regards
$col = $row = $order = 1;
if ($row_data = mysql_fetch_assoc($stockQuery))
{
foreach ($row_data as $key => $value)
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(++$col, $row, $key);
do
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col = 1, ++$row, $order++);
foreach ($row_data as $value)
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(++$col, $row, $value);
}
while ($row_data = mysql_fetch_assoc($stockQuery));
}
I would like to produce something like this in an excel file but it produces this which is not what i want. I would like the output to be exactly the same. If the number of tests are four, then four tests are dynamically printed vertically in a cell in excel file as in the word document plus the additional details
here is more details of the number of tests and their details.
$trd = $this->getRequestedTestsDisplay2($labref); //An array of objects 4 in total
$coa_details = $this->getAssayDissSummary($labref); //details as shown http://nqcl.alphybay.com/word.png
phpword code
for ($i = 0; $i < count($trd); $i++) {
foreach ($coa_details as $coa) {
if ($coa->test_id == $trd[$i]->test_id) {
$determined = $coa->determined;
$remarks = $coa->verdict;
}
}
$table3->addRow(400);
$table3->addCell(1500)->addText($trd[$i]->name);
$table3->addCell(1500)->addText($trd[$i]->methods);
$table3->addCell(1700)->addText($trd[$i]->compedia);
$table3->addCell(1800)->addText($trd[$i]->specification);
$table3->addCell(1700)->addText('DETERMINED', $style3);
$table3->addCell(1500)->addText($trd[$i]->complies);
}
phpxcel code
$row = 19;
$col=1;
$worksheet= $objPHPExcel->getActiveSheet();
for ($i = 0; $i < count($trd); $i++) {
foreach ($coa_details as $coa) {
if ($coa->test_id == $trd[$i]->test_id) {
$determined = $coa->determined;
$remarks = $coa->verdict;
}
$worksheet
->setCellValueByColumnAndRow($col, $row, $trd[$i]->name)
->setCellValueByColumnAndRow($col, $row, $trd[$i]->methods)
->setCellValueByColumnAndRow($col, $row, $trd[$i]->compedia)
->setCellValueByColumnAndRow($col, $row, $trd[$i]->specification)
->setCellValueByColumnAndRow($col, $row, $trd[$i]->complies);
$col++;
}
$row++;
}
Perhaps resetting column for each row, and not overwriting each value in the same row/column:
$row = 19;
$worksheet= $objPHPExcel->getActiveSheet();
for ($i = 0; $i < count($trd); $i++) {
$col=1;
foreach ($coa_details as $coa) {
if ($coa->test_id == $trd[$i]->test_id) {
$determined = $coa->determined;
$remarks = $coa->verdict;
}
$worksheet
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->name)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->methods)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->compedia)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->specification)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->complies);
}
$row++;
}
EDIT
You mean something like:
$row = 19;
$worksheet= $objPHPExcel->getActiveSheet();
for ($i = 0; $i < count($trd); $i++) {
$col=1;
foreach ($coa_details as $coa) {
if ($coa->test_id == $trd[$i]->test_id) {
$determined = $coa->determined;
$remarks = $coa->verdict;
}
}
$worksheet
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->name)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->methods)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->compedia)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->specification)
->setCellValueByColumnAndRow($col++, $row, $trd[$i]->complies);
}
$row++;
}
So I am attempting to loop through and print these values to an excel file. I was able to figure that out but am now attempting to add code to be able to explode the original values I was getting.
$col = 1;
while($row_data = mysql_fetch_assoc($result)) {
$row = 1;
if ($col == 1) {
$row_headings = array_keys($row_data);
foreach($row_headings as $value) {
Everything works until here. The values we get come out as 111X222X333 so I want to explode them and then query a new array of values
list($sid,$gid,$qid) = explode("X", $value)
$results="select * from lime questions where sid =".$sid." and gid =".$gid." and qid".$qid;
}
foreach(mysql_fetch_assoc($results)){
these were the 3 lines I added to an already working loop. I am wondering what I am doing wrong here.
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $results);
$row++;
}
}
$row = 1;
$col++;
}
foreach($row_data as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$col++;
}
Where am I messing up the loops
I am using this piece of code in PHP to query a database and import the data to an excel file. Currently I am getting the data from the database, but I can't get the headers.
Can anyone tell me how to get the headers from the database?
$objPHPExcel = new PHPExcel();
$col = 1;
while($row_data = mysql_fetch_assoc($result)) {
$row = 1;
foreach($row_data as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$col++;
}
$objPHPExcel = new PHPExcel();
$col = 1;
while($row_data = mysql_fetch_assoc($result)) {
$row = 1;
if ($col == 1) {
$row_headings = array_keys($row_data);
foreach($row_headings as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$row = 1;
$col++;
}
foreach($row_data as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$col++;
}