Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. <?php
  2. set_time_limit(20000);
  3. ini_set('memory_limit','-1');
  4. require_once './PHPExcel.php';
  5. require_once './PHPExcel/IOFactory.php';
  6. require_once './PHPExcel/Reader/Excel5.php';
  7. $dsn = "mysql:host=localhost;dbname=lab2_crud;";
  8. $user = "fred";
  9. $password = "bRKRAqCbVEQLn9e7";
  10. try{
  11. $dbh = new PDO($dsn,$user,$password);
  12. $dbh->query('set names utf8;');
  13. }catch(PDOException $e){
  14. echo "Failed to connection".$e->getMessage();
  15. }
  16.  
  17.  
  18. $filePath = "STOCK.xlsx";
  19. $PHPReader = new PHPExcel_Reader_Excel2007();
  20. if(!$PHPReader->canRead($filePath)){
  21. $PHPReader = new PHPExcel_Reader_Excel5();
  22. if(!$PHPReader->canRead($filePath)){
  23. echo 'no Excel';
  24. return ;
  25. }
  26. }
  27. $PHPExcel = $PHPReader->load($filePath);
  28. $sheetCount = $PHPExcel->getSheetCount();
  29. for($i = 0; $i< $sheetCount; $i ++){
  30. $currentSheet = $PHPExcel->getSheet($i);
  31. $highestColumn = $currentSheet->getHighestColumn();
  32. $highestRow = $currentSheet->getHighestRow();
  33.  
  34. $stmt = $dbh->prepare("insert into products(Name,IID, Type) values (:Name,:IID,:Type) ");
  35. $stmt->bindParam(":Name", $Name, PDO::PARAM_STR);
  36. $stmt->bindParam(":IID", $IID, PDO::PARAM_STR);
  37. $stmt->bindParam(":Type", $IID, PDO::PARAM_STR);
  38. for ($j = 2; $j <= $highestRow; $j++) {
  39. $IID = $PHPExcel->getActiveSheet()->getCell("A" . $j)->getValue();
  40. $Name = $PHPExcel->getActiveSheet()->getCell("B" . $j)->getValue();
  41. $Type = $i;
  42. if ($IID != "" && $Name != "") {
  43. echo "$IID:$Name : $Type";
  44. echo "<br>";
  45. $stmt->execute();
  46. }
  47. }
  48. }
  49.  
  50. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement