PHPExcel Multi Sheet Loop Issue - php

I'm developing a small class that will allow you to pass queries and it will create a worksheet for each query.
FYI: This class is still in development and I will be reducing down into smaller functions.
My issue is that for some reason my sheet increment is off and I cant figure out where to put it.
I am calling my class like this:
$ex2 = new ExportToExcel2('Somefile');
$ex2->AddSheet('Sheet1', 'Select * from Division;');
$ex2->AddSheet('Sheet2', 'Select * from Zone');
$ex2->ExportMultiSheet();
I should have two tabs, "Sheet1" and "Sheet2". This is how my sheet ends up looking. All the data is on Sheet1 and Worksheet.
Here is my class:
class ExportToExcel2 {
public $AllSheetData = [];
protected $SheetData = [];
protected $PHPExcel = '';
protected $FileName = '';
function __construct($_filename) {
$this->FileName = $_filename;
$this->PHPExcel = new PHPExcel;
}
public function AddSheet($_WorkSheetName, $_Query) {
$this->SheetData['Sheet_Name'] = $_WorkSheetName;
$this->SheetData['Query'] = $_Query;
$this->AllSheetData[] = $this->SheetData;
unset($this->SheetData);
}
public function ExportMultiSheet() {
$Result='';
$count=0;
$this->PHPExcel->setActiveSheetIndex(0);
foreach($this->AllSheetData as $subarray)
{
foreach($subarray as $key => $value)
{
if($count>0)
{
$this->PHPExcel->createSheet($count);
$this->PHPExcel->setActiveSheetIndex($count);
}
if($key == 'Query') {
$Result = dbQuery($value);
//set header row
$row = 1; // 1-based index
$row_data = sqlsrv_fetch_array($Result, SQLSRV_FETCH_ASSOC);
$col = 0;
foreach(array_keys($row_data) as $key) {
$this->PHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $key);
$col++;
}
//set body rows
$row2 = 2;
while($row_data = sqlsrv_fetch_array($Result, SQLSRV_FETCH_ASSOC)) {
$col2 = 0;
foreach($row_data as $key=>$value) {
$this->PHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col2, $row2, $value);
$col2++;
}
$row2++;
}
$count++;
}
if($key =='Sheet_Name') {
$this->PHPExcel->getActiveSheet()->setTitle($value);
}
//set all columns to align left
$this->PHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
//show gridlines?
$this->PHPExcel->getActiveSheet()->setShowGridlines(true);
//set columns a through z to auto width
for($col = 'A'; $col !== 'Z'; $col++) {
$this->PHPExcel->getActiveSheet()
->getColumnDimension($col)
->setAutoSize(true);
}
}
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->PHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
}
Any ideas on where to place my $count++?
Solved, Here is the finished class(until its not finished again)
class ExportToExcel2 {
public $AllSheetData = [];
protected $SheetData = [];
protected $PHPExcel = '';
protected $FileName = '';
function __construct($_filename) {
$this->FileName = $_filename;
$this->PHPExcel = new PHPExcel;
//clean the output buffer before download
ob_clean();
}
public function AddSheet($_WorkSheetName, $_Query) {
$this->SheetData['Sheet_Name'] = $_WorkSheetName;
$this->SheetData['Query'] = $_Query;
$this->AllSheetData[] = $this->SheetData;
unset($this->SheetData);
}
public function ExportMultiSheet($_ExportType='xls') {
if(!empty($this->AllSheetData)) {
$count=0;$Result='';
$this->PHPExcel->setActiveSheetIndex(0);
foreach($this->AllSheetData as $subarray) {
if($count>0){
$this->PHPExcel->createSheet(null);
$this->PHPExcel->setActiveSheetIndex($count);
}
$count++;
foreach($subarray as $key => $value) {
if($key == 'Query') {
$Result = dbQuery($value);
$this->SetHeaderCells($Result);
$this->SetbodyCells($Result);
}
if($key =='Sheet_Name') {
$this->PHPExcel->getActiveSheet()->setTitle($value);
}
}
}
$this->ExportType($_ExportType);
}
}
public function ExportSingleSheet($_Query, $_ExportType='xls') {
$Result = dbQuery($_Query);
$this->SetHeaderCells($Result);
$this->SetBodyCells($Result);
$this->SetProperties();
$this->ExportType($_ExportType);
}
private function ExportType($_ExportType) {
if($_ExportType=='xls') {
$this->DownloadXLS();
}
else if($_ExportType=='csv') {
$this->DownloadCSV();
}
}
private function SetProperties() {
//set all columns to align left
$this->PHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
//show gridlines?
$this->PHPExcel->getActiveSheet()->setShowGridlines(true);
//set columns a through z to auto width
for($col = 'A'; $col !== 'Z'; $col++) {
$this->PHPExcel->getActiveSheet()
->getColumnDimension($col)
->setAutoSize(true);
}
//set the first sheet to open first
$this->PHPExcel->setActiveSheetIndex(0);
}
private function DownloadXLS() {
$this->SetProperties();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$this->FileName.'-'.date("y.m.d").'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->PHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
private function DownloadCSV() {
$this->SetProperties();
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="'.$this->FileName.'-'.date("y.m.d").'.csv"');
header('Cache-Control: max-age=0');
$objWriter = new PHPExcel_Writer_CSV($this->PHPExcel);
$objWriter->save("php://output");
exit;
}
private function SetHeaderCells($Result) {
$row = 1; // 1-based index
$row_data = sqlsrv_fetch_array($Result, SQLSRV_FETCH_ASSOC);
$col = 0;
foreach(array_keys($row_data) as $key) {
$this->PHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $key);
$col++;
}
}
private function SetBodyCells($Result) {
$row2 = 4;
while($row_data = sqlsrv_fetch_array($Result, SQLSRV_FETCH_ASSOC)) {
$col2 = 0;
foreach($row_data as $key=>$value) {
$this->PHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col2, $row2, $value);
$col2++;
}
$row2++;
}
}
}

You should first move the sheet generation code from the foreach($subarray loop to your foreach($this->AllSheetData (since you want to add new sheet for every.. well.. new sheet. Not for every new sheet property).
You should then use a very similar code to the one you had, and $counter will be used only within that part of the code. Note that to create a new sheet and place is as the last one, you should simply pass null to the createSheet() method.
So your code should look like this:
public function ExportMultiSheet() {
...
$count = 0;
foreach($this->AllSheetData as $subarray)
{
if ($count > 0)
{
$this->PHPExcel->createSheet(null);
$this->PHPExcel->setActiveSheetIndex($count);
}
$count++
foreach($subarray as $key => $value)
...
}
...

Related

phpexcel to html only a portion

I'm working on phpexcel. The File I'm working on is from tally export file. I have to read this file and save the data to each user .
For example from this file I need only A2:G10 as HTML/TABLE. So i can display to the particular member (Adv. Chandra Mogan) individually.
I NEED A TABLE FOR ONLY A PORTION OF THE SHEET
What I have done so far:
protected function doExcelUpdate() {
$inputFileName = $this->getParameter('temp_directory') . '/file.xls';
if (!file_exists($inputFileName)) {
$this->addFlash('sonata_flash_error', 'File: not found in temp directory');
return;
}
$this->addFlash('sonata_flash_info', 'File: exist');
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
$this->addFlash('sonata_flash_error', 'Error in PHPExcel');
return;
}
$sheet = $objPHPExcel->getSheet(0);
if (!$sheet) {
$this->addFlash('sonata_flash_error', 'Error in reading sheet');
return;
}
$objPHPExcel->getSheet(0)
->getStyle('A1:G10')
->getProtection()
->setLocked(
PHPExcel_Style_Protection::PROTECTION_PROTECTED
);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->setSheetIndex(0);
$objWriter->save($this->getParameter('temp_directory') . '/output.html');
}
A1:G10 is not locked. Entire sheet is printed.
First point: "Locking" doesn't change the sheet size, or set a "view window"; it protects parts of the sheet from being edited.
Second point: "Locking" is a feature of Excel, and supported for excel writers, but not for HTML.
Third point: there is no direct mechanism to write only part of a worksheet.
As a suggestion, you might create a new blank worksheet, and then copy the data/style information from the cell range that you want in your your main worksheet to that new worksheet starting from cell A1; then send that worksheet to the HTML Writer.
You can't achive this using locked or hidden cells when you use HTML writer.
You can use a workaround creating a new worksheet and after adding the portion you want to display.
For mantain style on new worksheet (like font, color, border) you must extract it for each cell from the orginal worksheet and apply to the copied cells.
The same thing is for merged cells in the old worksheet.
Your function doExcelUpdate() should be like this:
protected function doExcelUpdate()
{
$inputFileName = $this->getParameter('temp_directory').'/file.xls';
if (!file_exists($inputFileName)) {
$this->addFlash('sonata_flash_error', 'File: not found in temp directory');
return;
}
$this->addFlash('sonata_flash_info', 'File: exist');
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$originalPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
$this->addFlash('sonata_flash_error', 'Error in PHPExcel');
return;
}
$originalSheet = $originalPHPExcel->getSheet(0);
if (!$sheet) {
$this->addFlash('sonata_flash_error', 'Error in reading sheet');
return;
}
// Get the data of portion you want to output
$data = $originalSheet->rangeToArray('A1:G11');
$newPHPExcel = new PHPExcel;
$newPHPExcel->setActiveSheetIndex(0);
$newSheet = $newPHPExcel->getActiveSheet();
$newSheet->fromArray($data);
// Duplicate style for each cell from original sheet to the new sheet
for ($i = 1; $i < 11; $i++) {
for ($j = 0; $j <= 6; $j++) {
$style = $originalSheet->getStyleByColumnAndRow($j, $i);
$newSheet->duplicateStyle($style, PHPExcel_Cell::stringFromColumnIndex($j).(string)$i);
}
}
// Merge the same cells that are merged in the original sheet
foreach ($originalSheet->getMergeCells() as $cells) {
$inRange = false;
foreach (explode(':', $cells) as $cell) {
$inRange = $originalSheet->getCell($cell)->isInRange('A1:G11');
}
// Merge only if in range of the portion of file you want to output
if ($inRange) {
$newSheet->mergeCells($cells);
}
}
$objWriter = PHPExcel_IOFactory::createWriter($newPHPExcel, 'HTML');
$objWriter->save($this->getParameter('temp_directory').'/output.html');
}
UP TO NOW FOR THE WORK TO BE COMPLETED, I HAVE DONE THIS,
private function doExcelUpdate() {
$inputFileName = $this->getParameter('temp_directory') . '/file.xls';
$synopsis = PHPExcel_IOFactory::load($inputFileName)->getSheet(0);
$column = $synopsis->getHighestColumn();
$row = $synopsis->getHighestRow();
$this->cleanUserPayment();
$this->doExcelUpdateTable($synopsis, $column, $row);
$this->deleteExcelFile();
}
private function cleanUserPayment() {
$em = $this->getDoctrine()->getManager();
$classMetaData = $em->getClassMetadata('AppBundle\Entity\UserPayment');
$connection = $em->getConnection();
$dbPlatform = $connection->getDatabasePlatform();
$connection->beginTransaction();
try {
$connection->query('SET FOREIGN_KEY_CHECKS=0');
$q = $dbPlatform->getTruncateTableSql($classMetaData->getTableName());
$connection->executeUpdate($q);
$connection->query('SET FOREIGN_KEY_CHECKS=1');
$connection->commit();
} catch (\Exception $e) {
$connection->rollback();
}
}
private function doExcelUpdateTable($synopsis, $column, $row) {
set_time_limit(300);
$t = [];
for ($r = 1; $r <= $row; $r++) {
for ($c = "A"; $c <= $column; $c++) {
$cell = $synopsis->getCell($c . $r)->getFormattedValue();
if ($cell == 'Ledger:') {
$t[] = $r;
}
}
}
$t[] = $row+1;
$numItems = count($t);
$i = 0;
$em = $this->getDoctrine()->getManager();
foreach ($t as $key => $value) {
if (++$i != $numItems) {
$up = new UserPayment();
$up->setName($synopsis->getCell('B' . $value)->getFormattedValue());
$up->setMessage($this->doExcelUpdateTableCreate($synopsis, $column, $value, $t[$key + 1]));
$em->persist($up);
// $this->addFlash('sonata_flash_error', 'Output: ' . $synopsis->getCell('B' . $value)->getFormattedValue() . $this->doExcelUpdateTableCreate($synopsis, $column, $value, $t[$key + 1]));
}
}
$em->flush();
$this->addFlash('sonata_flash_success', "Successfully updated user bills. Total data updated::" . count($t));
}
private function doExcelUpdateTableCreate($synopsis, $column, $rowS, $rowE) {
$mr = NULL;
$x = 0;
$alphas = range('A', $column);
$oneTable = '<table border="1">';
for ($r = $rowS; $r < $rowE; $r++) {
$oneTable .= "<tr>";
for ($c = "A"; $c <= $column; $c++) {
if ($x > 0) {
$x--;
continue;
}
$mr = NULL;
$x = 0;
$cell = $synopsis->getCell($c . $r);
$cellVal = $cell->getFormattedValue();
if ($cellVal == NULL) {
$cellVal = " ";
}
$cellRange = $cell->getMergeRange();
if ($cellRange) {
$mr = substr($cellRange, strpos($cellRange, ":") + 1, 1);
$upto = array_search($mr, $alphas);
$x = ($upto - array_search($c, $alphas));
$oneTable .= "<td colspan=" . ($x + 1) . " style='text-align:right;'>" . $cellVal . "</td>";
} else {
$oneTable .= "<td>" . $cellVal . "</td>";
}
}
$oneTable .= "</tr>";
}
$oneTable .= "</table>";
return $oneTable;
}
private function deleteExcelFile() {
$filesystem = new \Symfony\Component\Filesystem\Filesystem();
$filesystem->remove($this->getParameter('temp_directory') . '/file.xls');
}

Searching in Excel with PHP

I am trying to find the last cells value of a row in an Excel table by the first cells value using PHPExcel. The user enters the value of the first cell in the row (which is the "command" parameter) and if it exists in the table my function is supposed to jump to the last cell of the row and return the value. This is my code:
public function search($command, $excelobj) {
foreach ($excelobj->getWorksheetIterator() as $worksheet) {
foreach ($worksheet->getRowIterator() as $row) {
$rowiterator = $worksheet->getRowIterator();
foreach ($row->getCellIterator() as $cell) {
$celliterator = $row->getCellIterator();
$head = $celliterator->current()->getValue();
if(strcmp($head, $command)) {
for ($i = 0; $i == 8; $i++) {
$celliterator->next();
}
return $cell->getValue();
}
}
}
}
return false;
}
For some reason it always returns wrong.
I solved it myself, this code functions correctly:
public function search($command, $excelobj) {
foreach ($excelobj->getWorksheetIterator() as $worksheet) {
foreach ($worksheet->getRowIterator() as $row) {
$rowiterator = $worksheet->getRowIterator();
$celliterator = $row->getCellIterator();
$celliterator->setIterateOnlyExistingCells(true);
$head = $celliterator->current()->getValue();
while (strcmp($head, $command) !== 0 && $rowiterator->valid()) {
$rowiterator->next();
$row = new PHPExcel_Worksheet_Row($worksheet, $rowiterator->key());
$celliterator = $row->getCellIterator();
$head = $celliterator->current()->getValue();
}
for ($i = 0; $i <= 7; $i++) {
$celliterator->next();
}
return $celliterator->current()->getValue();
}
}
return false;
}

PHPExcel Class Does Not Display First Row from Database

I wrote a class to export excel files with a worksheet for each query passed to it.
For some reason my worksheets are missing the first row of results. The headers show up properly, but the first row under the header is missing.
If I DO NOT call the SetHeaderCells() function the first row is there, so I know it is something in my class.
SetHeaderCells() NOT called:
But if I do call the SetHeaderCells() then the first row is missing.
Calling this class with:
if(gfQSGetGetVar('Export')=='xls') {
$ex2 = new ExportToExcel2('Somefile');
$ex2->AddSheet('Sheet1', 'Select * from Division;');
$ex2->AddSheet('Sheet2', 'Select * from Zone');
$ex2->ExportMultiSheet();
}
Class:
require_once 'PhpExcel.php';
class ExportToExcel2 {
public $AllSheetData = [];
protected $SheetData = [];
protected $PHPExcel = '';
protected $FileName = '';
function __construct($_filename) {
$this->FileName = $_filename;
$this->PHPExcel = new PHPExcel;
//clean the output buffer before download
ob_clean();
}
public function AddSheet($_WorkSheetName, $_Query) {
$this->SheetData['Sheet_Name'] = $_WorkSheetName;
$this->SheetData['Query'] = $_Query;
$this->AllSheetData[] = $this->SheetData;
unset($this->SheetData);
}
public function ExportMultiSheet($_ExportType='xls') {
if(!empty($this->AllSheetData)) {
$count=0;$Result='';
$this->PHPExcel->setActiveSheetIndex(0);
foreach($this->AllSheetData as $subarray) {
if($count>0){
$this->PHPExcel->createSheet(null);
$this->PHPExcel->setActiveSheetIndex($count);
}
$count++;
foreach($subarray as $key => $value) {
if($key == 'Query') {
$Result = dbQuery($value);
$this->SetHeaderCells($Result);
$this->SetbodyCells($Result);
}
if($key =='Sheet_Name') {
$this->PHPExcel->getActiveSheet()->setTitle($value);
}
}
}
$this->ExportType($_ExportType);
}
}
public function ExportSingleSheet($_Query, $_ExportType='xls') {
$Result = dbQuery($_Query);
$this->SetHeaderCells($Result);
$this->SetBodyCells($Result);
$this->SetProperties();
$this->ExportType($_ExportType);
}
private function ExportType($_ExportType) {
if($_ExportType=='xls') {
$this->DownloadXLS();
}
else if($_ExportType=='csv') {
$this->DownloadCSV();
}
}
private function SetProperties() {
//set all columns to align left
$this->PHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
//show gridlines?
$this->PHPExcel->getActiveSheet()->setShowGridlines(true);
//set columns a through z to auto width
for($col = 'A'; $col !== 'Z'; $col++) {
$this->PHPExcel->getActiveSheet()
->getColumnDimension($col)
->setAutoSize(true);
}
//set the first sheet to open first
$this->PHPExcel->setActiveSheetIndex(0);
}
private function DownloadXLS() {
$this->SetProperties();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$this->FileName.'-'.date("y.m.d").'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->PHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
private function DownloadCSV() {
$this->SetProperties();
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="'.$this->FileName.'-'.date("y.m.d").'.csv"');
header('Cache-Control: max-age=0');
$objWriter = new PHPExcel_Writer_CSV($this->PHPExcel);
$objWriter->save("php://output");
exit;
}
private function SetHeaderCells($Result) {
$row = 1; // 1-based index
$row_data = sqlsrv_fetch_array($Result, SQLSRV_FETCH_ASSOC);
$col = 0;
foreach(array_keys($row_data) as $key) {
$this->PHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $key);
$col++;
}
}
private function SetBodyCells($Result) {
$row2 = 2;
while($row_data = sqlsrv_fetch_array($Result, SQLSRV_FETCH_ASSOC)) {
$col2 = 0;
foreach($row_data as $key=>$value) {
$this->PHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col2, $row2, $value);
$col2++;
}
$row2++;
}
}
}
Solved!
The way I resolved this was to use the recommended Metadata function.
Changed the SetHeaderCells function to this:
private function SetHeaderCells($Result) {
$row = 1; // 1-based index
$col = 0;
foreach( sqlsrv_field_metadata($Result) as $fieldMetadata ) {
foreach( $fieldMetadata as $name => $value) {
if($name=='Name') {
$this->PHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$col++;
}
}
}
}
You're fetching the first record and using the $row_data keys to set headers; but then that record has already been fetched, so when you move on to the data you're immediately going into a fetch loop that will start with the second record in your resultset
I don't believe sqlsrv supports a rewind function, but can you perhaps use sqlsrv_field_metadata() to get the header information rather than fetching the first record.
Alternatively, something like:
private function SetHeaderCells($Result) {
$row = 1; // 1-based index
$row_data = sqlsrv_fetch_array($Result, SQLSRV_FETCH_ASSOC);
$col = 0;
foreach(array_keys($row_data) as $key) {
$this->PHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $key);
$col++;
}
return $row_data;
}
private function SetBodyCells($Result, $row_data) {
$row2 = 2;
do {
$col2 = 0;
foreach($row_data as $key=>$value) {
$this->PHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col2, $row2, $value);
$col2++;
}
$row2++;
} while($row_data = sqlsrv_fetch_array($Result, SQLSRV_FETCH_ASSOC));
}
$Result = dbQuery($_Query);
$firstRow = $this->SetHeaderCells($Result);
$this->SetBodyCells($Result, $firstRow);
but using the metadata for the headings would be a better approach

PHPExcel multiple foreach loops

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());
}

Upload csv files to database

HI Guys I have A problem I want to upload csv files and store to database
But doesn't work..
I tried to debug it several times but not working hope you help me guys.
I Have a csv file containing..
firstname lastname middlename gender
test test test male
Then when I upload this csv file doesn't work.
This is my code..
<?php
session_start();
include ("config.php");
$extension = end(explode(".",basename($_FILES['file']['name'])));
if (isset($_FILES['file']) && $_FILES['file']['size'] < 10485760 && $extension== 'csv')
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
try
{
$connection = new pdo("mysql:host=$hostname;dbname=upload",$username,$password);
if
$upload = $connection->prepare("INSERT INTO tbl_upload(firstname,lastname,middlename,gender)
VALUES (?,?,?,?)");
if($handle !== false)
{
fgets($handle);
while (($data = fgetcsv($handle, 10000, ',') !== false))
{
$upload->execute($data);
}
fclose($handle);
$connection = null;
echo "<p class='bg-success'>Upload Success</p>";
header ("location: index.php");
}
}
catch(pdoExecption $e)
{
die($e->getmessage());
}
}
else
{
header("location:config.php");
}
?>
thanks for the help..
Your method is not viable.
To import CSV you can either use the mysqlimport utility or you have to split the csv records in separate fields to match your INSERT statement. You cannot just feed CSV to the insert statement and hope that it sorts out things for itself.
I can give you a little CsvImport class I wrote some time ago make importing CSV a little bit easier
<?php
final class CsvImport {
private $file = "";
private $fields = array(); //array("field1", "field2", "field3"); ...
private $data = array(); //array([1] => array("value", "value", "value") ...
private $delimiter = "";
private $fieldCount = 0;
private $rowCount = 0;
private $internalCounter = 0;
private $loaded = false;
public function __construct($_file, $_delimiter = "") {
$this->file = $_file;
if(is_file($this->file) == true) {
if(($handle = fopen($this->file, "r")) !== false) {
//If the delimiter is not set try to suggest it
if(strlen($_delimiter) == 0) {
$this->delimiter = $this->suggestDelimiter();
} else {
$this->delimiter = $_delimiter;
}
if(strlen($this->delimiter) > 0) {
$row = 0;
while(($data = fgetcsv($handle, 0, $this->delimiter)) !== false) {
if($row == 0) {
$this->fieldCount = count($data);
}
if($this->fieldCount > 0) {
for($c = 0; $c < $this->fieldCount; $c++) {
if($row == 0) {
$this->fieldCount = count($data);
$this->fields[] = $data[$c];
} else {
$this->data[$row][$this->fields[$c]] = utf8_encode($data[$c]);
}
}
}
$row++;
}
$this->rowCount = $row;
if($this->fieldCount > 0) {
$this->loaded = true;
}
}
}
}
}
public function getNextRow() {
$retVal = false;
if($this->loaded == true) {
if($this->internalCounter < $this->rowCount) {
$this->internalCounter++;
$retVal = true;
} else {
$this->internalCounter = 0;
}
}
return $retVal;
}
public function readField($field) {
$retVal = false;
if($this->isLoaded() == true) {
if(isset($this->data[$this->internalCounter][$field]) == true) {
$retVal = $this->data[$this->internalCounter][$field];
}
}
return $retVal;
}
public function resetInternalCounter() {
$this->internalCounter = 0;
}
public function getFieldCount() {
return $this->fieldCount;
}
public function getRowCount() {
return $this->rowCount;
}
public function getFieldList() {
return $this->fields;
}
public function getDelimiter() {
return $this->delimiter;
}
public function isLoaded() {
return $this->loaded;
}
private function suggestDelimiter() {
$retVal = "";
$file = fopen($this->file, 'r');
$content = fgets($file);
fclose($file);
if(strlen($content) > 0) {
$list = array(
"," => substr_count($content, ","),
"." => substr_count($content, "."),
"&" => substr_count($content, "&"),
"%" => substr_count($content, "%"),
"-" => substr_count($content, "-"),
";" => substr_count($content, ";"),
"'" => substr_count($content, "'"),
"\"" => substr_count($content, "\""),
);
$maxCount = 0;
foreach($list as $key => $value) {
if($value > 0) {
if($value > $maxCount) {
$retVal = $key;
$maxCount = $value;
}
}
}
}
return $retVal;
}
private function __clone() { }
}
?>
The usage is as simple as this:
$file = "/path/to/file.csv";
$import = new CsvImport($file);
if($import->isLoaded() == true) {
while($import->getNextRow()) {
foreach($import->getFieldList() as $fieldName) {
$value = $import->readField($fieldName);
echo $fieldName . " => " . $value . "<br />";
}
}
}
I am sure you can build your queries with all field names in the foreach loop and fire each query in the while loop.

Categories