insert date in sql din upload excel file

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
sterica
Mesaje: 285

insert date in sql din upload excel file

Salutare,

Am urmatoarea structura a excelului (este o balanta contabila):

Cod: Selectaţi tot

    A         B         C         D         E         F
+------+-----------+----------+-----------+----------+-----------+
| CONT | DESCRIERE | DEBITOR1 | CREDITOR1 | DEBITOR2 | CREDITOR2 |
+------+-----------+----------+-----------+----------+-----------+
| 401  | FURNIZOR1 |   456.00 |    120.00 |  3421.00 |  31124.00 |
| 401  | FURNIZOR2 |   126.00 |    450.00 |  3428.00 |  31124.00 |
| 401  | FURNIZOR3 |   876.00 |    190.00 |  3423.00 |  44444.00 |
| 401  | FURNIZOR4 |   346.00 |   1632.00 |  6544.00 |  31124.00 |
| 401  | FURNIZOR5 |   195.00 |    120.00 |  3421.00 |  76434.00 |
| 401  | FURNIZOR6 |   690.00 |  11323.00 |  2317.00 |  31124.00 |
| 401  | TOTAL1    |   234.00 |    560.00 |  3427.00 |  31124.00 |
+------+-----------+----------+-----------+----------+-----------+
si urmatoarea structura a tabelei sql:

Cod: Selectaţi tot

+----+------+-----------+----------+-----------+
| ID | CONT | DESCRIERE | DEB1     |    CRED2  |
+----+------+-----------+----------+-----------+
|  1 |  401 | FURNIZOR1 |  456.00  |  31124.00 |
|  2 |  401 | FURNIZOR3 |  876.00  |  44444.00 |
|  3 |  401 | FURNIZOR6 |  690.00  |  31124.00 |
+----+------+-----------+----------+-----------+
in tabela nu vreau sa introduc toti furnizorii, doar anumiti furnizori.
Alegerea fisierului vreau sa o fac cu:

Cod: Selectaţi tot

<input type="file" name="datafile" size="40">
De unde pot incepe sa fac acest lucru?

Multumesc!

MarPlo Mesaje: 4343
Salut
Poti sa incepi cu raspunsul de la subiectul similar: "Transfer date din Excel in MySQL cu PHP" de la pagina: transfer-date-excel-mysql-php-t2216.htm
- Sau vezi ce gasesti pe internet la cautare: " insert excel data in mysql ".

sterica Mesaje: 285
Am reusit sa incarc valorile dintr-un fisier excel, multumesc mult de recomandare.
Codul este acesta:

Cod: Selectaţi tot

<?php  
 $connect = mysqli_connect("localhost", "root", "", "financiar");  
 include ("PHPExcel/IOFactory.php");  
 $html="<table border='1'>";  
 $objPHPExcel = PHPExcel_IOFactory::load('401.xlsx');  
 foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)   
 {  
      $highestRow = $worksheet->getHighestRow();  
      for ($row=1; $row<=$highestRow; $row++)
      {  
           $html.="<tr>";  
           $furnizor = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(5, $row)->getValue());  
           $rc_debitor = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(14, $row)->getValue());
           $sf_creditor = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(23, $row)->getValue());  
           $sql = "INSERT INTO cont_401(FURNIZOR, RC_DEBITOR, SF_CREDITOR) VALUES ('".$furnizor."', '".$rc_debitor."', '".$sf_creditor."')";  
           mysqli_query($connect, $sql);  
           $html.= '<td>'.$furnizor.'</td>';  
           $html .= '<td>'.$rc_debitor.'</td>';
          $html .= '<td>'.$sf_creditor.'</td>';
           $html .= "</tr>";  
      }  
 }  
 $html .= '</table>';  
 echo $html;  
 echo '<br />Data Inserted';  
 ?>
Insa cum pot aduce numele fisierului pe care doresc sa il incarc intr-o variabila. Am acest input de unde vreau sa incarc fisierle:

Cod: Selectaţi tot

<input type="file" name="datafile" size="40">
.

Multumesc!

MarPlo Mesaje: 4343
Faci intai upload, cu un cod php in acelasi fisier si retii cale si numele unde e salvat, apoi se foloseste PHPExcel; cum e in acest cod:

Cod: Selectaţi tot

//Here add php code that uploads the excel file
$fxls ='dir_upload/'. $_FILES['datafile']['name']; //store path and file name

$connect = mysqli_connect("localhost", "root", "", "financiar"); 
include ("PHPExcel/IOFactory.php"); 
$html="<table border='1'>"; 
$objPHPExcel = PHPExcel_IOFactory::load($fxls);
//...

sterica Mesaje: 285
functioneaza, multumesc mult de ajutor!

Subiecte similare