Symfony, PHPExcel not accepting date array from database? - php

I'm having problems outputting an excel with its date object. I'm using PHPExcel on Symfony. I performed an SQL that grabs data from the database in my Symfony controller. Then I did a for loop to iterate through each rows of data. However when I added in the value of a date, it gives me an error:
FatalErrorException: Error: Cannot use object of type DateTime as array in ...\Symfony\vendor\phpoffice\phpexcel\Classes\PHPExcel\Cell\DefaultValueBinder.php line 86
Here is my code:
$excel = new PHPExcel();
$excel->setActiveSheetIndex(0);
// ..etc
for($results as $result) {
// codes..
->setCellValue('H'.$i, $result['date'])
}
It's not allowing me to output the excel because of this. Should I be converting it to a string?

I found the solution through looping through the cell with the date values and setting it as date by number format:
$excel = new PHPExcel();
$excel->setActiveSheetIndex(0);
// ..etc
$i = 3;
for($results as $result) {
$excel->getActiveSheet()->getStyle('H'.$i)
->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
$date = \PHPExcel_Shared_Date::PHPToExcel($result['date']);
// codes..
->setCellValue('H'.$i, $result['date'])
}

Related

Convert Entire Value in Spesific Column Date From Excel Using PHPSpreadSheet

I have xlsx tables and I use PhpSpreadsheet to parse them. One Column is date format. The problem is that PhpSpreadsheet returns the values from date-formatted cells in an unspecified format:
2-Jan-19 (in Excel) and 43467 (After Exporting)
And I need to save entire tables exactly as they look like in Excel Format, like "2-Jan-19" not "43467"
I've been read
Retrieve Date From Table Cell,
But it can't solve my problem
Below is the code I have right now:
$spreadsheet = $reader->load($_FILES['berkas_excel']['tmp_name']);
$sheetData = $spreadsheet->getSheet(0)->toArray();
echo count($sheetData);
for($i = 3;$i < count($sheetData) ;$i++)
{
$kode = $sheetData[$i]['0'];
$tgl = $sheetData[$i]['1']; //This is where the Date Column
$shift_rec = $sheetData[$i]['2'];
$grup = $sheetData[$i]['3'];
$pekan = $sheetData[$i]['4'];
$bulan = $sheetData[$i]['5'];
}
I hope somebody can help me to get "2-Jan-19" as the result, so I can insert it into my DB after that,
Thanks to anyone who willing to answer/help my problem right now

Generate .xlsx file using fromArray for a big amount of data

I need to write in a .xlsx file about 111.100 rows, using fromArray() but I have a strange error
I use phpspreadsheet library
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$columnLetter = 'A';
foreach ($columnNames as $columnName) {
// Allow to access AA column if needed and more
$sheet->setCellValue($columnLetter.'1', $columnName);
$columnLetter++;
}
$i = 2; // Beginning row for active sheet
$columnLetter = 'A';
foreach ($columnValues as $columnValue) {
$sheet->fromArray(array_values($columnValue), NULL, $columnLetter.$i);
$i++;
$columnLetter++;
}
// Create your Office 2007 Excel (XLSX Format)
$writer = new Xlsx($spreadsheet);
// In this case, we want to write the file in the public directory
// e.g /var/www/project/public/my_first_excel_symfony4.xlsx
$excelFilepath = $directory . '/'.$filename.'.xlsx';
// Create the file
$writer->save($excelFilepath);
And I get the exception :
message: "Invalid cell coordinate AAAA18272"
#code: 0
#file: "./vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Coordinate.php"
Can you help me please ?
Excel pages are limited. The limit is huge but still limited. This is a correct filter so you can't write if there is no space for it.
Anyway you shouldnt use excel pages for such a big amount of data, you can try fragmenting it into smaller pieces, but databases should be the way to manipulate such amount of information

Writing data read from Excel with Spout fails

I'm reading an excelsheet with Spout and directly writing this data to a sheet results in a error.
Trying to add a value with an unsupported type: object in vendor/box/spout/src/Spout/Writer/XLSX/Internal/Worksheet.php on line 231
Does anybody have the solution?
$reader = ReaderFactory::create(Type::XLSX);
$reader->open($sFileNameExcel);
$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($sWritePath.$sWriteFileName);
foreach ($reader->getSheetIterator() as $sheet)
{
if ($sheet->getName()=='mysheet')
{
foreach ($sheet->getRowIterator() as $row)
{
$writer->addRow($row);
}
}
}
$writer->close();
$reader->close();
My data consists of string, integer and double fields.
The sheet you're reading probably contains a date. The writer can't interpret dates though, hence the error.
Try configuring your reader this way: $reader->setShouldFormatDates(true); before calling open

Get named ranges in PHPExcel

Using PHPExcel, I would like to loop through all the named ranges in a workbook and then create a new tab for each one.
I am currently doing this in excel with a macro, like so:
Dim sheetName As String
sheetName = ActiveSheet.Name
Dim nName As Name
For Each nName In Names
If InStr(1, nName.RefersTo, sheetName) > 0 Then
Application.Goto Reference:=nName.Name
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = nName.Name
Selection.PasteSpecial Paste:=xlPasteValues
End If
Next nName
mystring = Sheets(1).Name
ActiveWorkbook.SaveCopyAs Filename:="P:\DP\CWBI\" & mystring & "_parsed.xls"
I can loop through active sheets in PHPExcel and dump them into an array for processing, like this:
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$sheets = $objReader->listWorksheetNames($file);
print_r ($sheets);
Does anyone know if it's possible to loop through all the named ranges?
Edit:
I'm trying the first suggestion below, but not getting any output. Here's the code:
$file="span_test.xlsx";
$objPHPExcel = PHPExcel_IOFactory::load($file);
foreach($objPHPExcel->getNamedRanges() as $name => $namedRange) {
echo $namedRange . "</br>";
}
Got it working now, my mistake in trying to echo an object. should be
print_r($name)
You have to actually load the workbook to access the named ranges
foreach($objPHPExcel->getNamedRanges() as $name => $namedRange) { ... }
which returns a PHPExcel_NamedRange object for each named range

PHPExcel excel read is not working for some cells with calculation

I use PHPExcel lib for read the excel file in Codeigniter project. It is not read some cells with calculation. It show as #VALUE! But some values with calculation is reading in same excel sheet. Whats wrong with those cells?
Cells with have following calculation is not reading
=+D109*1000
=+B16/B13
=+D23/$D$109
Cells with have following calculation is reading
=+B10-B11
=+C10-C11
But all cells are reading for some excel sheet. This issue come with xlsx format
This is my code
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$this->excel = $objReader->load($path);
$this->excel->setActiveSheetIndex($sheet);
$data = $this->excel->getActiveSheet()->toArray(null, true, true, true);
print_r($data);
I check with google. getCalculatedValue() should use for read calculated values. But i can't use it one by one. Is it has a method to read all sheet as array?
How ever i checked some cells with following way
$this->excel->getActiveSheet()->getCell('B18')->getCalculatedValue() // return #VALUE!
$this->excel->getActiveSheet()->getCell('B18')->getOldCalculatedValue() //return 0.4211
How i use old calculated value using toArray?
Finally I get old getOldCalculatedValue using loop.
$data = $this->excel->getActiveSheet()->toArray(null, true, true, true);
//This code add for some calculated cells were not reading in xlsx format
foreach ($data as $no => $row) {
foreach ($row as $key => $value) {
if (isset($value) && $value == '#VALUE!') {
$data[$no][$key] = $this->excel->getActiveSheet()->getCell($key.$no)->getOldCalculatedValue();
}
}
}

Categories