I am trying to import an xlxs file that that some custom formats embedded within it. If I open the styles.xml file I can see the formats as follows:
When I am reading the data from these cells the custom formats aren't being applied, so I assume I need to do this manually with setNumberFormat - How can I read these custom format codes to format these values correctly?
Edit: Here is the code I am using to access the formatted value, slightly unusual as I am accessing PHPSpreadsheet classes via Laravel Excel.
foreach ($row->getCellIterator() as $cell) {
$value = (new Cell($cell))->getDelegate()->getFormattedValue();
dump($value);
}
Here is a dump of the results I get using this:
Item 10 should be 01099391 rather than 1099391
You are probably looking for \PhpOffice\PhpSpreadsheet\Cell\Cell::getFormattedValue()
Related
I am working on creating an upload function for Excel Sheets in PHP using the extension PHPExcel. I have an array called standard, in which all allowed sheet names within an Excel document are saved. I need to loop through this array using a foreach loop and retrieve a sheet, should one of the worksheets be named the same as an entry in the array standard.
I have implemented it as follows:
foreach (array_keys($this->standard) as $k => $klasse) {
$sheet = $this->xlsx->getSheetByName($klasse);
die($klasse.' - '.print_r($sheet,true));
The die at the end is to check if the process even returns any data. Unfortunately, I do not get any sheet returned, only the variable klasse. I am assuming this is due to the wrong use of the function getSheetByName from PHPExcel. I'd be grateful for any help.
I am using PHPExcel to validate csv files before parsing them and storing in my database and server. I am trying to use the file properties to determine if the file has been modified or if it is the original file. I have used the following for .xls, .xlsx with great results (using the appropriate reader);
$file = $_FILES['file']['tmp_name'];
$reader = new PHPExcel_Reader_CSV();
if($reader->canRead($file)){
$object = $reader->load($file);
$created = $object->getProperties()->getCreated();
$modified = $object->getProperties()->getModified();
if(!$created===$modified){
//File has been edited and cannot be used
}else{
//File is good, continue processing
}
}
However, when using CSV files, NOTHING is working as expected. I renamed an MS-Word doc to .csv->passed, edited a csv->passed, even used a .jpg->passed. What on earth am I missing?? Any help would greatly appreciated! Edit->I should note that $created and $modifed are an exact match when var_dump($object) despite having edited the file and confirming the changes within the document properties.
The properties values accessible from PHPExcel are those stored within the file itself, not within the directory entries for that file.
CSV files don't have any inherent properties of their own; CSV is purely a raw data file format These property methods are for accessing the properties that do exist in other spreadsheet formats such as BIFF (xls) and OfficeOpenXML (xlsx) which do support them. Loading a CSV (or other format that doesn't support properties) into PHPExcel will provide default value for those properties (so that calls like you're making won't trigger fatal errors), but it cannot provide actual values for something that doesn't natively exist in the format being loaded.
It seams that http://www.maatwebsite.nl/laravel-excel/docs can't figure out how to handle simple merged cells.
Importing excel file like this:
Excel::load($tmp_path, function($reader) {
$reader->dd();
})->get();
See image bellow where is problem:
You'll have to read this file out differently, you can't use the simple import methods. You have to read it out per coordinate, example of the native PHPExcel methods:
http://docs.typo3.org/typo3cms/extensions/phpexcel_library/1.7.4/manual.html#_Toc237519900
Also you can check the link: https://github.com/Maatwebsite/Laravel-Excel/issues/442
Hope this helps.
Just checked configuration and we can set 'slugged_with_count' in heading, so results become like picture bellow. Or disable heading and rely on columns order.
I'm trying to set up the print options for an excel file in PHPExcel. I'm working from the packaged API documents (version 1.7.7) but can't find any methods to do this. Am I missing a setting somewhere?
This setting is found in MS Excel under Print > Page Setup > Sheet.
$objPHPExcel->getActiveSheet()->setPrintGridlines(TRUE);
EDIT
setPrintGridlines() identifies whether gridlines around cells should be displayed when printing a worksheet from MS Excel; the default is FALSE. setShowGridlines() identifies whether gridlines around cells should be displayed when the spreadsheet is shown in the MS EXcel GUI; the default is TRUE;
I've found the one I was looking for. It was under the Worksheet class and not the page setup class as I'd previously thought.
For anyone looking in future, this is the method you need:
PHPExcel_Worksheet setPrintGridlines( [boolean $pValue = false])
There are two functions very similar though: setPrintGridlines and setShowGridlines. I'm not quite sure of the difference. Can anyone enlighten me?
I tried to search for some plugins to import Excel file into MySQL database, one of them is http://code.google.com/p/php-excel-reader/
The tool is so powerful that it displays the entire excel content into html.
However, I think I just need to read the Excel file and extract the contents, for example, into an array, and then write a SQL statement for entering into the database.
Would there be any good codes and packages? Thanks!
This is best plugin with proper documentation and examples
https://github.com/PHPOffice/PHPExcel
Plus point: you can ask for help in its discussion forum and you will get response within a day from the author itself, really impressive.
Sometimes I need to import large xlsx files into database, so I use spreadsheet-reader as it can read file per-row. It is very memory-efficient way to import.
<?php
// If you need to parse XLS files, include php-excel-reader
require('php-excel-reader/excel_reader2.php');
require('SpreadsheetReader.php');
$Reader = new SpreadsheetReader('example.xlsx');
// insert every row just after reading it
foreach ($Reader as $row)
{
$db->insert($row);
}
?>
https://github.com/nuovo/spreadsheet-reader
If you can convert .xls to .csv before processing, you can use the query below to import the csv to the database:
load data local infile 'FILE.CSV' into table TABLENAME fields terminated by ',' enclosed by '"' lines terminated by '\n' (FIELD1,FIELD2,FIELD3)
If you save the excel file as a CSV file then you can import it into a mysql database using tools such as PHPMyAdmin
Im not sure if this would help in your situation, but a csv file either manually or programatically would be a lot easier to parse into a database than an excel file I would have thought.
EDIT: I would however suggest looking at the other answers rather than mine since #diEcho answer seems more appropriate.
I wrote an inherited class:
<?php
class ExcelReader extends Spreadsheet_Excel_Reader {
function GetInArray($sheet=0) {
$result = array();
for($row=1; $row<=$this->rowcount($sheet); $row++) {
for($col=1;$col<=$this->colcount($sheet);$col++) {
if(!$this->sheets[$sheet]['cellsInfo'][$row][$col]['dontprint']) {
$val = $this->val($row,$col,$sheet);
$result[$row][$col] = $val;
}
}
}
return $result;
}
}
?>
So I can do this:
<?php
$data = new ExcelReader("any_excel_file.xls");
print_r($data->GetInArray());
?>
You can use PHPOFFICE as "diEcho" said. And then use bin2hex() function to insert data to database (SQL SERVER or MySQL) as a BLOB data type.
bin2hex() function helped me alot.