Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('default_charset', 'UTF-8');
- include("PHPExcel-1.8/Classes/PHPExcel/IOFactory.php");
- include("PHPExcel-1.8/Classes/PHPExcel.php");
- //database
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbName = "is_verxml";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbName);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- echo "Connected successfully";
- $sql = "SELECT id_doc, fileName, verification FROM xml";
- $result = $conn->query($sql);
- $arrayresultados = array();
- if ($result->num_rows > 0) {
- // output data of each row
- while($row = $result->fetch_assoc()) {
- array_push($arrayresultados, $row["fileName"]);
- }
- } else {
- echo "0 results";
- }
- print_r($arrayresultados);
- echo "<br><br><br>";
- $xmlDoc = new DOMDocument();
- libxml_use_internal_errors(true);
- // File reader
- $x = 0;
- $controlo = 1;
- $dir = 'C:/xampp/htdocs/toXML/DadosMonitorizacao-Eletric-IPVC/DadosAProcessar';
- $files = opendir($dir);
- if($files){
- while(($files_name = readdir($files)) !==FALSE){
- if($files_name != '.' && $files_name != '..'){
- //verifica se está na base de dados
- foreach ($arrayresultados as $nomes) {
- if($files_name == $nomes){
- echo "igual" . "<br>";
- echo $files_name . " - ". $nomes . "<br>";
- $controlo = 1;
- echo $controlo . "<br>";
- }else{
- $controlo = 0;
- echo "diferente" . "<br>";
- echo $files_name . " - ". $nomes . "<br>";
- echo $controlo . "<br>";
- }
- }
- ///////////////////////
- ///////////////////////
- ///////////////////////
- ///////////////////////
- // FIquei aqui!!!! está a adicionar sempre o ultimo..... verificar isso..
- ///////////////////////
- ///////////////////////
- ///////////////////////
- ///////////////////////
- ///////////////////////
- ///////////////////////
- if($controlo == 1){
- echo "nãoo está"."<br><br>";
- //adicionar a base de dados e excel
- $sql = "INSERT INTO xml (fileName) VALUES ('".$files_name."')";
- if ($conn->query($sql) === TRUE) {
- echo "New record created successfully";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- //adicionar ao excel
- //xml object
- $xmlDoc->load('http://localhost:8081/toXML/DadosMonitorizacao-Eletric-IPVC/DadosAProcessar/'.$files_name);
- $x = $xmlDoc->documentElement;
- //excel object
- $objReader = PHPExcel_IOFactory::createReader('Excel2007');
- $objPHPExcel = $objReader->load('lastfile.xlsx');
- $worksheet = $objPHPExcel->getActiveSheet();
- $lastLine = $worksheet->getHighestRow();
- $line = $lastLine+1;
- $lastCol = $worksheet->getHighestColumn();
- $col = 'A';
- foreach($x->childNodes as $k){
- if($k->nodeName!="#text"){
- //titulos
- //$worksheet->setCellValue($col.$line,$k->nodeName);
- $worksheet->setCellValue($col.$line,$k->nodeValue);
- $col++;
- }
- }
- $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'excel2007');
- $objWriter->save("lastfile.xlsx");
- }else{
- echo "está"."<br><br>";
- $controlo = 1;
- }
- }
- }
- }
- echo "<br><br><br>";
- //so entra no excel se...
- $conn->close();
- //header ('Location: index.php')
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement