3
I’m extracting data from a file .xlsx
. converting them into .csv
and showing it on screen. So far so good, however, I want to play this data in a database, using PHP, but for this I need to convert the data, because the way they are, they will not enter the database, see:
10-01-15 SAI 87,059.56 UVA SAFRA 2015 - APB
I need to format the date 10-01-15
and the value 87,059.56
, the rest of the data is quiet. get out - UVA SAFRA 2015 - APB
Would I have to play so even on the bench, and then catch them, convert the date and value and play on another table?
Follows the code:
if (isset($_POST['pega'])) {
include_once("PHPExcel/Classes/PHPExcel.php");
$uploadDir = "uploadFile/";
$uploadfile = $uploadDir . $_FILES['arquivo']['name'];
if(move_uploaded_file($_FILES['arquivo']['tmp_name'], $uploadfile)) {
echo "Arquivo pego com sucesso";
echo "<br><br>";
}else{
echo "Não foi possível pegar arquivo";
echo "<br><br>";
}
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($uploadfile);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$csvFileName = str_replace('.xlsx', '.csv', $uploadfile);
$objWriter->save($csvFileName);
if (($handle = fopen($csvFileName, "r")) !== false) {
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$num = count($data);
for ($c = 0; $c < $num; $c++) {
echo $data[$c]." ";
}
echo "<br />\n";
}
fclose($handle);
}
}
I was able to separate the dice, but I can’t play for the bench, I’m using this:
$pdo = conectar();
$insereDados=$pdo->prepare("INSERT INTO dadosImportados (data, tipo, valor, descricao) VALUES (?,?,?,?)");
$insereDados->bindValue(0, $data[0]);
$insereDados->bindValue(1, $data[1]);
$insereDados->bindValue(2, $data[2]);
$insereDados->bindValue(3, $data[3]);
$insereDados->execute();
But this message appears on the screen: "Fatal error: Uncaught Exception 'Pdoexception' with message 'SQLSTATE[HY093]: Invalid Parameter number: Columns/Parameters are 1-based' in /Applications/MAMP/htdocs/systems/scripts_da_web/php/imports/importFile.php:68 Stack trace: #0 /Applications/MAMP/htdocs/systems/scripts_da_web/php/imports/importArchive.php(68): Pdostatement->bindValue(0, '10-01-15') #1 {main} thrown in /Applications/MAMP/htdocs/systems/scripts_da_web/php/imports/importFile.php on line 68"
Format date for format
YYYY-DD-MM
? What type of column will hold the monetary value?– user28595
Puts the code you use to grab the xlsx and play on the screen. It will help to come up with a suggestion.
– Ricardo
10-01-15 SAI 87,059.56 UVA SAFRA 2015 - APB
Does this come as a csv value? Will that comma after 87 not bother you?– rray
Yes, yyyy-dd-mm, the column will be of the type Date
– GustavoSevero
No, each dice comes in an array value echo $data[$c]." ";
– GustavoSevero
It generates a . csv, but is being displayed on the screen the Becco data.
– GustavoSevero
I deleted the comment from above, now that you posted the code got better the question.
– Bacco
The question itself is basically how to format the date and the monetary value? So, everything else is unnecessary. Or has something more than that?
– Daniel Omine
That’s right Daniel Omine... Actually the other data are not unnecessary, but the date and value, should be adjusted to go to the bank.
– GustavoSevero
Some personal idea?
– GustavoSevero
i meant that in the context of the question it was unnecessary to put so many details. Simply ask how to form date and currency.
– Daniel Omine
Our guy! There are some who say to detail, specify the question better and now you say it is not necessary.
– GustavoSevero
You must have good sense in discerning what "detailing a question" is. Just put what is essential. In your case, for example, you only want to know how to format the date and currency. Therefore, everything else that is not related to this is out of context. But it is not enough to just ask and wait. I also recommend to show what you tried to do to solve because this is not a free technical support center where you play the codes and, in a touch of magic come solutions.
– Daniel Omine
You can choose a correct answer?
– durtto
the answers didn’t help you? if they didn’t help, what’s missing to help?
– durtto