Quite a weird one. I have a spreadsheet that I am trying to read via Phpspreadsheets, but one cell (at least for the moment), is not retrieving any data and I can't figure out why.
The text in B10 (not retrieving) is Mike
The text in B9 (retrieving) is Name
1) I have checked that the formatting on this cell (B10) and cells where data is retrieved is the same
2) I have tried getvalue, getformattedvalue, getcalculated value, all to no avail
Below is my Php code snippet:
$A1 = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 9)-
>getCalculatedValue(); // Returns value
echo $A1;
$A2 = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 10)-
>getValue(); //returns nothing
echo $A2;
There are many other cells with the same problem, but if can get this one, I'm sure that it's the same for the others...
Where can I start looking? What am I missing (something obvious I'm sure)...
I think that I may have found the answer...
I was loading an xlsm file, and not a plain xls or xlsx file. As soon as I changed to either of these formats, it seemed to behave correctly...
Related
I'm trying to copy styles from a range of cells in a template file and paste it to another range of cells in another file and then save the resulting file. I experimented on a small subset of my Excel file before I fully implemented it, but I found a strange result.
Here is my code snippet...
include "../class/PHPExcel.php";
include "../class/PHPExcel/IOFactory.php";
include "../class/PHPExcel/Cell.php";
$reader = PHPExcel_IOFactory::createReader('Excel2007');
$frTemplate = $reader->load("document/Template/FR-001 Template.xlsx");
$frTemplate->setActiveSheetIndex(0);
$frTemplate->getActiveSheet()->setCellValueExplicitByColumnAndRow(3, 6, $year);
//copy style
$cellStyle = $frTemplate->getActiveSheet()->getStyle('E47:E49');
$frTemplate->getActiveSheet()->duplicateStyle($cellStyle, 'E52:E54');
//copy value
$cellValues = $frTemplate->getActiveSheet()->rangeToArray('E47:E49');
$frTemplate->getActiveSheet()->fromArray($cellValues, null,'E52');
$writer = PHPExcel_IOFactory::createWriter($frTemplate, "Excel2007");
$writer->save("document/Template/FR-001 2017.xlsx");
echo "Done";
Here is an image of the copied Excel file...
Copied File-Case 1
And here is an image of the target file...
Result-Case 1
I'm trying to apply the code with another data, and I found that -
"Merge cell" style cannot be copied
When a range of cells is copied, the only style copied is the first cell (upper left)
Sadly, I cannot post the evidence of the above case as my reputation is still very low (It is my first question in Stack Overflow!)
So, can you please tell me what is wrong with my code? Thanks in advance.
That's correct:
"Merge cell" is not an aspect of style; it's part of the structure of the worksheet, so it cannot be "copied" by duplicateStyle. If you want to merge cells, you need to use the mergeCells() method for each merge range that you want to set.
The duplicateStyle() method copies a style to a range of cells; it doesn't copy multiple styles to a new range. The getStyle() method allows a range for the purpose of setting a style against all cells in that range, but only returns the style of the first cell within that range. You need to duplicate each different style to its own range.
I need to store a 24 digits number in a cell with PHPExcel.
I've read some tutorials about that. But the problem still exists...
Here is the code:
$objPHPExcel->getActiveSheet()->getStyle('A2')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
$objPHPExcel->getActiveSheet()->SetCellValue('A2', '680540101947000270348604');
$objPHPExcel->getActiveSheet()->SetCellValue('C2', '100');
It supposed to store entire number without any change. But when I open file it's stored like:
6.80540101947E23
I tried also to save that as an string and that worked. But I have to deliver this file to bank so their system is so stupid and does't accept number as string...They need to calculate I guess.
Any idea?
This worked for me:
// Set the value explicitly as a string
$objPHPExcel->getActiveSheet()->setCellValueExplicit('A2', '680540101947000270348604', PHPExcel_Cell_DataType::TYPE_STRING);
First you need to set cell format as text then it will be possible.
$objPHPExcel
->getActiveSheet()
->getStyle('A')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
I have problem with PHPExcel. I already read that question:
How to modify excel file using PHPExcel in symfony2
...because on the begining that was my problem. But I already done everything which Abhishek Jaiswal said and... Ive got another problem. Probably Ive done something bad, because that answer is quite reasonable.
Ok, about code:
$excelReader = \PHPExcel_IOFactory::createReader('Excel2007');
$excelObj = $excelReader->load($path.$filename);
for ($i = 3; $i <= 30; $i++) {
$excelObj->setActiveSheetIndex(0)->setCellValue('O'.$i, 'tytyty')->
setCellValue('P'.$i, $company->getFullName())->
setCellValue('Q'.$i, $company->getAddress())->
setCellValue('S'.$i, $company->getAddressAddition())->
setCellValue('T'.$i, $company->getPostalCode())->
setCellValue('U'.$i, $company->getCity())->
setCellValue('V'.$i, $entity->__toString())->
setCellValue('W'.$i, $entity->getPhone())->
setCellValue('X'.$i, $entity->getInternalPhoneNumber())->
setCellValue('Y'.$i, $entity->getEmail());
}
$excelObj->setActiveSheetIndex(0);
$excelWriter = \PHPExcel_IOFactory::createWriter($excelObj, 'Excel2007');
$excelWriter->save($path . $new_filename);
And when I look on saved file, on edited worksheet i got worksheet that start from 31 row (which is first not edited row, and first without any formating in that worksheet). In fact, it also looks broken, because when I opening it in LibreOffice I dont see rows, just numbers on the right.
And Im pretty sure, that xlsx, which Im loading, is good because I tried to dump data from it, and I got, what I expected.
Ok... I found it. I repeated test, that i've done above, but with one difference. I took ,to create file, Microsoft Office. It appear, that Libre Office have some problems with 2007/2010 format.
Everything works great with MO file.
I am building a tool that will accept a CSV or tab-delimited file, which will then be parsed and the data databased.
The uploaded file can be CSV or tab-delimited.
I came up with a workable solution (below) for detecting what format the file might be in and would like to know if there is a better way to solve this and/or how any of you out there have solved the same problem.
Thanks
<?php
$csv_comma='Fruit,Color
Apple,"Red,Green"
Tomato,"Red,Green"
Banana,Yellow
Tangerine,Orange
';
$csv_semi_colon='Fruit;Color
Apple;"Red,Green"
Tomato;"Red,Green"
Banana;Yellow
Tangerine;Orange
';
$tab_delimited='Fruit Color
Apple Red,Green
Tomato Red,Green
Banana Yellow
Tangerine Orange';
$fileArr = array($csv_comma,$csv_semi_colon,$tab_delimited);
foreach($fileArr as $file){
if(preg_match('/^(.+),(.+)/',trim($file))){
echo "CSV with comma separator";
}
if(preg_match('/^(.+);(.+)/',trim($file))){
echo "CSV with semi colon separator";
}
if(preg_match('/^(.+)\t(.+)/',trim($file))){
echo "Tab delimited";
}
}
Well csv has this prety much implemented.
Default for csv is , but with sep= you could specify an other seperator.
You could just implement this as csv. So you have a default of , but if the sep is defined you use that.
You file could look like:
apple, orange, tomato
or
sep=;
apple; orange; tomato
So if the first line starts with sep, it is an "option" line otherwise there are values. For tab you do sep=\t
Now users can define there own seperator and no guessing any more
After some comments of CBroe of easy to use for the user there could be some changes. csv only accepts one charachter as septerator so that system could be use like the above. cvs editor (like excel) will handle that for the user
If the user uses the tab it won't be a csv file but a .txt (for example). So you could change the default according to the file given.
Also I want to add, already pointed out in the comments, if you want to guess you will hit a point where it will occure it is wrong.
I don't know the setup of the files but csv lines need to be the same length (according to my memory). So what you could do is read out the first x lines. and use every seperator.
After that you check which lines lengths are the same, most likely that is your seperator (again guessing)
You can use this kind of pattern to check the csv structure and determine the separator:
if (preg_match('^(?:("[^"]++"|[^,;\t\n]++)(?<sep>[,\t;])(?1)(?:\n|$))++$', $csv_comma, $match))
print_r($match['sep']);
I am using xampp/wamp on windows and looking to convert an excel workbook to a html file.
I am not asking this question right away, i did a lot of research and finally managed to get to a point and got stuck here.
Am using php's COM library to open excel, then read a workbook and try to save it as html, how ever i am having issues with it
This is my code
$excel = new COM("Excel.Application",NULL,CP_UTF8) or die("Unable to instantiate Excel");
$excel->Application->Visible=1;
$excel->DisplayAlerts="False";
$workBook=$excel->Workbooks->Open(realpath("./example-03e-02.xlsx"));
$workBook->PublishObjects->Add(xlSourceSheet, "c:\\temp\\x.htm", "Sheet1", "", xlHtmlStatic, "test_27778", "");
$workBook->Publish (True);
$workBook->AutoRepublish(0);
$excel->Workbooks->Close();
$excel->Application->Quit();
$excel = null;
$workBook=null;
The PUlishObjects method keeps telling me that xlSourceSheet is not defined, i tried to pass it as a string "xlSourceSheet" but it keeps saying parameter type mismatch in one or the other. IN the above case, it says parameter 6 type mismatch;
if i remove the optional parameters like divid and title (the last 2) it shows a type mismatch on source range which empty obviously since am exporting a sheet.
Any body can shed some light on this and tell me what i am doing wrong.
Thanks
Anywyas, i managed to sort out the problem with some further digging into excel developer manual.
I just had to replace xlSourceSheet and xlHtmlstatic with their respective numbers that i found in the docs. xlSourceSheet is 1 and xlHtmlStatic is 0.
If anybody is looking for these codes, here they are
xlSourceAutoFilter 3 An AutoFilter range
xlSourcePivotTable 6 A PivotTable report
xlSourcePrintArea 2 A range of cells selected for printing
xlSourceQuery 7 A query table (external data range)
xlSourceRange 4 A range of cells
xlSourceSheet 1 An entire worksheet
xlSourceWorkbook 0 A workbook
xlHtmlCalc 1 Use the Spreadsheet component. Deprecated in Excel 2007.
xlHtmlChart 3 Use the Chart component. Deprecated in Excel 2007.
xlHtmlList 2 Use the PivotTable component. Deprecated in Excel 2007.
xlHtmlStatic 0 Use static (noninteractive) HTML for viewing only.
$object=$excel->ActiveWorkbook->PublishObjects->Add(1,"c:\\temp\\x.htm","Sheet1",0)->Publish(1);
Thanks cweiske :)