Excel File parsing issue using PHPExcelReader - php

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

Related

I am getting issues when importing data using .csv into PostgreSQL ( greater than 50K lines in csv)

ERROR i'm getting :
This page isn’t working didn’t send any data.
ERR_EMPTY_RESPONSE
I am using PHP language for reading the csv file .
My PHP approach is look like for procession the csv data :
$csvAsArray = array_map('str_getcsv', file($tmpName));
I am sure that the above code creating problem afterwords the code is not getting executing .
How i can import more that greater than at least 1 million data at a time ?
Can anyone help me out , which approach should i choose ?
It looks like you're trying to grab the entire contents of the file in one gulp. Don't do that :) PHP array_map() isn't scalable to 1000's ... or millions of lines.
SUGGESTION:
Read your data into a temp file (as you appear to be doing now).
Do a Postgresql COPY
EXAMPLE:
COPY my_table(my_columns, ...)
FROM 'my_file.csv' DELIMITER ',' CSV HEADER;
I would suggest using a league/csv package for CSV parsing and importing. #paulsm4 is correct that it's never needed to put the whole file into memory and then work with it, one should rather read line-by-line. But this package is well-maintained and does this all under the hood and quite effectively. And it is much more flexible than COPY postgres command to my mind. One can filter the contents, map callbacks to fields/rows and all this on PHP side.

Download .csv file format in SAS from the web

I wonder how to download some .csv file by using SAS.
Browsing on the web, I found it is possible to do that by running the following script:
filename NAME url "http://.../NAME_OF_THE_FILE.csv"
Particularly, I want to understand how such statement works and in which case I cannot use that.
For instance, let's assume one has to download a .csv file that is uploaded on a web page, as, for example, in the web site, where one can find football match data available.
In such case case, by using the following script to download the file:
filename csv url "http://www.football-data.co.uk/mmz4281/1617/E0.csv";
and the following one to import data in SAS:
proc import file = csv
out = junk_00
dbms = csv replace;
delimiter = ",";
run;
everything works fine. This file corresponds to the season 2016/2017 and contains the Premier League data, one can find on the first link.
Instead, in the case of the championship data for the 2016/2017 season, by using the same script as follows:
filename csv url "http://www.football-data.co.uk/mmz4281/1617/E1.csv";
proc import file = csv
out = junk_00
dbms = csv replace;
delimiter = ",";
run;
you get the following error:
Import unsuccessful. See SAS Log for details.
Browsing at the LOG window you can see among the LOG lines the following note/warning:
Invalid data for Date, even if the file is formatted correctly.
I don't understand the reason sometimes the script works and sometimes not, since this happened with other file, although the file are not corrupted and formatted correctly and in the same way.
What's wrong?
Can someone help me to understand why this happens?
Thanks all in advance!
Proc Import has to guess at data types. For some reason it thinks the date field is formatted as MMDDYY, but it's actually DDMMYY. Or maybe it's used inconsistently, I didn't check all, but could see the source of the error immediately.
The solution is to not use PROC IMPORT but to use a data step. If all the files are structured the same, then this works as a solution, but if each file is different then it's not a feasible solution.
Another possible workaround, is to download the data and then set GUESSINGROWS to a large number and then read the files. It will read all the values before guessing at the types so it can be better. This solution does not appear to work when using filename URL, but I don't know why.
I don't think this is a full answer, but it should shed some light on what's happening for you.

find out when a file's contents were last modified

I wrote a CMS script made of many folders and files and I want to find a way to track when I last modified any of the files. I wrote a recursive directory/file check that finds the latest modified file and gives me the date and time however my issue is this: every time that I as much as copy a file to the server, or rename a file, even if I didn't make any modifications at all to any of the files, the newly copied file or renamed file now has today's date and therefore my script shows that there was a modification made today even if I haven't made changes in weeks.
How can I circumvent that?
I am using filemtime()
Is there a way with PHP to know when the file was ACTUALLY last modified (ie when the code in a file was worked on the last time)?
Thanks
I found a way to do it and wanted to post the answer:
$test = new SplFileInfo('path/to/file');
echo $test->getMTime();
echo date('Y-m-d',$test->getMTime());
The SplFileInfo::getMTime will actually return the last time a file's contents were modified as opposed to the last modification date of the file

Pull data from incrementing XML files

So heres the scenario.
I need to pull data from XML files located on the web (individually this is easy enough), but my problem is that a new file with an incremented filename is added from from time to time.
So for example, one day a filename may be www.website.com/00001.xml and the next day, a new file is added as www.website.com/00002.xml
I need to create a script, ideally in PHP, that will automatically refresh every couple of hours and pull the data from the new XML file, if the xml file is not present yet then it will retry that same number again. Hopefully all this can then just be put into rows in an excel file.
The question is basically, what is the best way to go about this? if anyone could point me to something similar thats already out there, then that would be great.
Thanks in advance

Best practice - exporting CSV

I am looking for the best way to export a CSV file. With MySQL and PHP.
Currently Im generating an CSV with INTO OUTFILE, it works that way but I don't think it's the good way.
Isn't there a better option to make a CSV export download button for every moment a user clicks the download button?
A INTO OUTFILE export is only possible for one instance and is not overwritable.
I have to generate a timestamp and save the file, and then get the latest file from my directory.
This method looks a bit messy for downloading a CSV file from a server...
Has anyone got better solutions?
Thanks!
I think you are well off with exporting via INTO OUTFILE. The reason is that sending the content to the CSV file is done by the MySQL server. Doing it with the PHP Script would be slower (first of all because it is a script, second of all because the data from the SQL server need to be passed to the script) and cost you more resources.
If the CSV file(s) become large you should keep in mind that your Script still may expire. You can encounter this issue by either setting an higher value for the maximum running time of a script in the configuration or have the CSV file being created by an external process/script
Maybe something like this:
`echo $query | mysql > $unique`;
$contents = file($unique);

Categories