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++;
}
Related
I want to display all the rows from a table with the corresponding column names above, which works. The problem is that it removes the first row from the results below the column names. It's as if the column row is somehow counted as a row in the while loop that displays the results, but I can't figure it out.
If I remove the column names code shown below all of the results are shown.
//COLUMN NAMES
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
All of the code shown below.
$query = "SELECT * FROM `" . $_SESSION['sess_table'] . "` ORDER by ID ASC";
if ($result = $mysqli->query($query)) {
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle($excelTitle);
$headingsrow = $result->fetch_assoc();
$headings = array_keys($headingsrow);
//COLUMN NAMES
$rowNumber = 1;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
//RESULTS
$rowNumber = 3;
while ($row = $result->fetch_row()) {
$col = 'A';
foreach($row as $key => $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}
$objPHPExcel->getActiveSheet()->freezePane('A2');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $excelFilename . '.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit();
}
In your code,
$rowNumber = 1;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
you have increased the $col value instead of increasing $rowNumber value.
try this,
$rowNumber = 1;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$rowNumber++;
}
You're fetching the first row to retrieve your headings, but then discarding it even though it contains data that you want to write as well
if ($result = $mysqli->query($query)) {
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle($excelTitle);
$row = $result->fetch_assoc();
$headings = array_keys($row);
//COLUMN NAMES
$rowNumber = 1;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
//RESULTS
$rowNumber = 3;
do {
$col = 'A';
foreach($row as $key => $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
} while ($row = $result->fetch_row());
}
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++;
}
This is my code,
Please tell me what am I missing?
// Field names in the first row
$sql1 = "SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='test'
AND `TABLE_NAME`='user_tab'";
$res1 = mysql_query($sql1);
while ($row2 = mysql_fetch_assoc($res1)) {
$fields[] = $row2['COLUMN_NAME'];
}
//Data
$sql = "SELECT * FROM user_tab";
$res = mysql_query($sql);
echo date('H:i:s'), " Load from Excel5 template", EOL;
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load("mytest.xls");
$logo = new PHPExcel_Worksheet_HeaderFooterDrawing();
$logo->setName('Logo');
$logo->setPath('image.jpg'); //Path is OK & tested under PHP
$logo->setHeight(38); //If image is larger/smaller than that, image will be proportionally resized
$objPHPExcel->getActiveSheet()->getHeaderFooter()->addImage($logo, PHPExcel_Worksheet_HeaderFooter::IMAGE_HEADER_LEFT);
echo date('H:i:s'), " Add new data to the template", EOL;
$col = 0;
foreach ($fields as $key => $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $value);
$col++;
}
$row = 2;
while ($row_data = mysql_fetch_assoc($res)) {
$col = 0;
foreach ($row_data as $key => $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$col++;
}
$row++;
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));
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