Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.40 KB | None | 0 0
  1.  
  2. Quando o script le o sql o formato da data acaba não sendo aceito ('FriFri/NovNov/2010201020102010') conforme o retorno abaixo:
  3.  
  4.  
  5.  
  6. FriFri/NovNov/2010201020102010 -
  7. Warning: pg_query() expects parameter 1 to be resource, boolean given in
  8. D:\xampp\htdocs\leitorxls2\example.php on line 99
  9. INSERT INTO pedidos_venda2 ( product_hierarchy, validade, material, material_description )
  10. VALUES ( 'BRTM', 'FriFri/NovNov/2010201020102010', 'P61BEB5FH',
  11. '09 25 15 5S 0,05B 0,05Cu 0,1Mn 0,1Zn' )
  12.  
  13. ______________________________________________________________________
  14. <?php
  15. // Test CVS
  16.  
  17. require_once 'Excel/reader.php';
  18.  
  19. function conecta_logistica()
  20. {
  21.  
  22.     $BANCO = "host=localhost port=5432 dbname=teste user=postgres password=102030";
  23.  
  24.     return pg_connect ( $BANCO );
  25.  
  26. }
  27. // ExcelFile($filename, $encoding);
  28. $data = new Spreadsheet_Excel_Reader();
  29.  
  30.  
  31. // Set output Encoding.
  32. $data->setOutputEncoding('CP1251');
  33.  
  34.  
  35. /***
  36. * if you want you can change 'iconv' to mb_convert_encoding:
  37. * $data->setUTFEncoder('mb');
  38. *
  39. **/
  40.  
  41. /***
  42. * By default rows & cols indeces start with 1
  43. * For change initial index use:
  44. * $data->setRowColOffset(0);
  45. *
  46. Exibindo Uma Planilha
  47.  
  48. Caso seu desejo seja apenas apresentar o conteúdo de um arquivo excel no navegador então faça o seguinte:
  49.  
  50. $data = new Spreadsheet_Excel_Reader("test.xls");
  51. $data->dump();
  52.  
  53. Convertendo uma Planilha em Array
  54.  
  55. Não existe tal função na ferramenta, sendo assim eu fiz a implementação desta funcionalidade
  56.  
  57. $data = new Spreadsheet_Excel_Reader("test.xls")
  58. $totalLinhas = $data->rowcount();
  59. $totalColunas= $data->colcount();
  60.  
  61. for($i = 1; $i <= $totalLinhas; $i++){
  62.     for($j = 1; $j <= $totalColunas; $j++){
  63.         $a[$i][$j] = $data->val($i,$j);
  64.     }
  65. }
  66. return $a;
  67.  
  68.  
  69. *  Some function for formatting output.
  70. * $data->setDefaultFormat('%.2f');
  71. * setDefaultFormat - set format for columns with unknown formatting
  72. *
  73. * $data->setColumnFormat(4, '%.3f');
  74. * setColumnFormat - set format for column (apply only to number fields)
  75. *
  76. **/
  77. $data->read('fotosemana1.xls');
  78. /*
  79.  $data->sheets[0]['numRows'] - count rows
  80.  $data->sheets[0]['numCols'] - count columns
  81.  $data->sheets[0]['cells'][$i][$j] - data from $i-row $j-column
  82.  
  83.  $data->sheets[0]['cellsInfo'][$i][$j] - extended info about cell
  84.    
  85.     $data->sheets[0]['cellsInfo'][$i][$j]['type'] = "date" | "number" | "unknown"
  86.         if 'type' == "unknown" - use 'raw' value, because  cell contain value with format '0.00';
  87.     $data->sheets[0]['cellsInfo'][$i][$j]['raw'] = value if cell without format
  88.     $data->sheets[0]['cellsInfo'][$i][$j]['colspan']
  89.     $data->sheets[0]['cellsInfo'][$i][$j]['rowspan']
  90. */
  91. error_reporting(E_ALL ^ E_NOTICE);
  92.  
  93. $CON = conecta_logistica();
  94.  
  95. pg_query( 'BEGIN' );
  96.  
  97. for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++)
  98. {
  99.         if($i != 1)
  100.         {  
  101.               echo $teste = $data->sheets[0]['cells'][$i][16]." - ";
  102.              
  103.             if( $data->sheets[0]['cells'][$i][1] != '' )
  104.             {
  105.                 $SQL = "
  106.                     INSERT INTO pedidos_venda2                 
  107.                     ( product_hierarchy, validade, material, material_description )
  108.                     VALUES
  109.                     ( '" . $data->sheets[0]['cells'][$i][1] . "', '" . $data->sheets[0]['cells'][$i][16] . "', '" . $data->sheets[0]['cells'][$i][3] . "', '" . $data->sheets[0]['cells'][$i][4] . "' )
  110.            
  111.             ";
  112.                 pg_query ( $CON, $SQL );
  113.                
  114.                 echo $SQL;
  115.            
  116.             }
  117.         }
  118.     echo "<br><br>";
  119. }
  120.  
  121. pg_query( 'COMMIT' );
  122. //print_r($data);
  123. //print_r($data->formatRecords);
  124. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement