Setting Active Cell for excel generated by PHPSpreadsheet - php

I am using PHPSpreadsheet
I am using freezePane('A9') in every excel generated by our web application.
At end, I am also adding setSelectedCell('A9');.
But when I open excel file it gets open with "A10" as the active cell. And also "A9" is scrolled down. So one has to scroll up to see row "9".
Check here an image of the issue.

This sounds a little like your issue, https://github.com/PHPOffice/PhpSpreadsheet/issues/389
Their response of a sorts of fix is this:
can be corrected by explicitly providing 'topLeftCell' argument:
$spreadsheet->getActiveSheet()->freezePane('A2','A2');

Related

PHPExcel: finding page breaks after when creating worksheet on the fly

for development, i use PHPExcel (https://github.com/PHPOffice/PHPExcel) for creating a Excel (xlsx) document filled with data from a mysql database. So far this works good.
I want to print this Excel document too, as hardcopy to spread around for people reading it from paper. Printing wont be a problem.
The problem lies in: it's a long document, far more rows then will fit on 1 or 2 pages. The worksheet consists of blocks which i want to keep together, when printed, to be on the same page.
I can use a method to set breaks on a row. That i will be able to get to work. But after finishing putting the data on the sheet, i don't know where the automatically placed page breaks are. There is a function (getBreaks()) on the worksheet, which should provide an array with the breaks, but the returned array remains empty, so i can't find out which blocks go over a break.
Can someone help me?
Should i first save the Excel sheet, then open it again and do my job then?
Excel document is created on a shared hosting webserver. No printing options there i guess, at least not where i can manage them.
Other solution is: put a macro's in that Excel document, and let the macro run (to put the breaks on the right places) and print the Excel sheet. (not my favorite solution as i am honest).

Set the zoom level for the html file generated using PHPExcel?

I am trying to generate a HTML file using PHPExcel. I have more than 30 columns and would like to zoom the page.
I've tried using the code below, but hasn't worked.
$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToPage(true);
$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToWidth(2);
I've also tried using this, but hasn't worked either.
$objPHPExcel->getActiveSheet()->getSheetView()->setZoomScale(250);
Both options don't work in the case of HTML page, but the zoom works if it is an Excel file. Where am I going wrong?
As you may see from the chaining of commands, the setZoomScale() method is part of the Worksheet class and it will only have an impact in case a worksheet is written, not when it is read.
The PHPDoc for PHPExcel lists the internal command _writeSheetViews() for Excel2007 writer and _storeZoom for old Excel versions whereas PHPExcel_Writer_HTML doesn't offer similar behavior.
What you may try is adding custom CSS styling to the created HTML file that may use smaller font-sizes for the table. AFAIK you won't be able to change the zoom level of the browser programmatically.

Excel File parsing issue using PHPExcelReader

I am facing a peculiar problem in parsing an excel file (.xls) using PHPExcelReader which actually Spreadsheet_Excel_Reader class. I have used it so many times in different applications and every time I was fine. I am working for an app where there is a ftp folder and an excel file is remotely put there every day. I have a scheduler task that runs every day and read the excel file and update the database.
It was working very fine for couple of months. Now they are adding some new columns in the files and the Spreadsheet_Excel_Reader is unable to read the numeric and date values. But if I just open the file and hit CTRL+S without doing anything, svn says that the file has been modified although I don't see anything changed from 'SVN: Diff with previous version'. However it is doing the magic as I see that the saved file is parsed correctly.
Bellow is the result I see when I try to run the script without touching the file. Please look at index 5 to 9.
Now look at the parse result when I run the script after opening the file and hit CTRL+S. Now entirely sure what is happening. I contacted to them and they said they are not doing anything new.
Any idea about this problem?
Sharing the idea here is much appreciated.
How are you looping through and grabbing the cell values? Have you tried using $cell->val(12,6)? An alternative could be (Thanks to Mark Baker):
$range = 'F'.$row.':'.'J'.$row; # assuming F-J cols are your numeric cols
$objPHPExcel->getActiveSheet()
->getStyle($range)
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

Extracting dynamically changing data in excel via php

I have an open excel sheet that's constantly being updated by another program via DDE. I wish to have a php script that accesses some of the data in this excel sheet. I have tried using PHPExcel and it seems that I cannot have the changes I make (e.g. via setCellValue) being immediately reflected in the open excel sheet. Similarly, if I change value of a cell (without saving sheet to the file system) the new value of the cell is not available via getValue().
Is this functionality supported by phpExcel? If so, could someone please point me to documentation that shows how this can be done? Alternatively, is there another way (not using phpExcel, for example) to do this?
Thanks.
I was able to do this using the method shown at the rarified blog webpage
This worked for me, both for "pushing" cell values from php to excel, as well as getting modified values (without saving the file) from excel to perl. This site also has a nifty ajax-based function that keeps auto-refreshing my webpage with the latest values in excel.
Many thanks to the author of the blog.
It's not supported by PHPExcel.... PHPExcel loads the workbook into memory at the point in time when you issue the load() call , and at that point it can't "autorefresh" whenever the workbook is changed by your DDE because the DDE update is to the workbook on disk, not the PHPEXcel copy that's in PHP memory.
You'd need to be constantly loading and reloading to pick up changes to the underlying file.
Likewise, if you change the workbook in PHPExcel, it doesn't write that change back to the file on disk unless you explicitly save(), so the change will not be visible to your DDE program.
I'm not aware whether you can even do this with MS Excel itself... if you load a workbook using MS Excel itself, you're loading from disk into memory, and if anything else is accessing that workbook at the time, you find that you've loaded it in read-only mode, and (as far as I'm aware) it won't automatically refresh whenever the DDE program updates the original version. If anything can work with this the way you need, it's likely to be COM, but I wouldn't build up your hopes too much.

CSV files and multi line text cells

I am generating a simple csv file using php. The file contains some user's personal data.
When I open the generated file in office, the addresses are not displayed in full height. I have to double click on the cell for the address to be shown fully (in full width and height) otherwise I can only see the first word/number of the address.
Also, I have date of births displayed as ######, I have to expand the whole column to see them fully.
This doesn't happen in open office.
Is there any way to force MS Office to show all fields in full? Because otherwise it'll be to confusing for the people who will use (Hey where are all the details!:)
Thanks :)
I don't think you can "format" your sheets with CSV. You will have to produce some other file format that Excel understands. I would suggest XML which is really easy to generate.
Just make a sample sheet with the data you want, save it as XML and you'll see how your file should be generated.
Or you could use some ready-made PHP solution for writing excel files if you can't be bothered with analysing the XML file.
you could try the auto-size columns feature.
This is a UI issue with how Excel works, you can't force Excel or anything else how they handle it.
The quickest work around is to perhaps create an XLS file that runs a macro to retrieve the CVS file and format the cells as needed, but there's nothing you can do inside the CSV to affect what Excel is displaying.

Categories