2015-08-07 22 views
5

Sto provando a convertire un file Excel in CSV quando l'utente fa clic sul pulsante di aggiornamento.Converti (xls, xlsx) in CSV prima di caricare usando PHP

Funziona quando carico un file Excel in una cartella, poi ottengo quel file usando PHPExcel e leggo tutti i dati e li converto in CSV.

Ecco il codice.

<html> 
    <head> 
     <meta charset="UTF-8"> 
    </head> 
    <?php 
    ini_set("display_errors", "1"); 
    ini_set("memory_limit", "-1"); 
    ini_set("max_execution_time", "-1"); 
    error_reporting(1); 

    require_once "PHPExcel.php"; 
    $dir = "../excel2csv/";     // Main Directory Name 
    $file_arr = array(); 
    $file_ext_arr = array('xls','xlsx');   // Valid Extensions of Excel File 

    // From Directory get only Excel Files in Array 
    if(is_dir($dir)) 
    { 
     if($dh = opendir($dir)) 
     { 
      while(($file = readdir($dh)) !== false) 
      { 
       $info = new SplFileInfo($file); 
       $ext = $info->getExtension();   // Get Extension of Current File 
       if(in_array($ext,$file_ext_arr)) 
       { 
        array_push($file_arr, $file); 
       } 
      } 
      closedir($dh); 
     } 
    } 

    // Make CSV File 
    $fp = fopen('file.csv', 'a'); 
    $list = array(); 

    foreach($file_arr as $val) 
    { 
     $arr_data = array(); 
     $objPHPExcel = PHPExcel_IOFactory::load($dir . $val); 
     $cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection(); 
     foreach($cell_collection as $cell) 
     { 
      $column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn(); 
      $row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow(); 
      $data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue(); 
      //header will/should be in row 1 only. of course this can be modified to suit your need. 
      // Skip Rows From Top if you have header in Excel then Change 0 to 1 
      if($row == 0) 
      { 
       $header[$row][$column] = $data_value; 
      } 
      else 
      { 
       $arr_data[$row]['row'] = $row; 
       $arr_data[$row][$column] = $data_value; 
      } 
     } 
     $data = $arr_data; 
     foreach($data as $val1) 
     { 
      $num_col = sizeof($val1) - 1; // get number of columns in Excel 
      break; 
     } 
     $lwrcol=array();  
     foreach($data as $val2) 
     { 
      $alphaArr = range('A','Z'); 
      $colArr = range('A',$alphaArr[$num_col - 1]); 

      foreach($colArr as $col) 
      { 
       $lwrcol[$col] = isset($val2[$col]) ? utf8_decode($val2[$col]) : ""; 
       fwrite($fp,$lwrcol[$col].","); 
      } 
      fwrite($fp,"\n"); 
     } 
     chmod(getcwd()."/file.csv", 0777); 
    } 
    fclose($fp); 
    ?> 
</html> 

Nel codice prima di tutto al di sopra mi sto trovando tutte excel file dalla cartella e fare uno file.csv file.

Quello che voglio fare è quando l'utente seleziona un file in <input type="file" name="upload"/> e cliccare sul tasto UPLOAD dopo allora nel backend primo processo è convertire file di Excel CSV PRIMA muoversi per caricare la cartella.

+0

Quindi, come posso vedere il codice sta lavorando per la conversione 'Excel' a' CSV' ma non hai scritto il codice per il caricamento prima dal 'input'. Corretta? – Manwal

+0

tutto questo codice è in '

' –

risposta

1

Come posso vedere il codice funziona per il salvataggio dei dati di Excel da CSV. Ma prima devi caricare prima da FORM quindi convertire. Quindi questo è quello che puoi fare è:

prima devi HTML per Form e caricare prima il file se il file POST è lì. Date un'occhiata nel seguente frammento:

<html> 
    <head> 
     <meta charset="UTF-8"> 
    </head> 
    <?php 
    ini_set("display_errors", "1"); 
    ini_set("memory_limit", "-1"); 
    ini_set("max_execution_time", "-1"); 
    error_reporting(1); 


    require_once "PHPExcel.php"; 
    $dir = "../excel2csv/";     // Main Directory Name 
    $file_arr = array(); 
    $file_ext_arr = array('xls','xlsx');   // Valid Extensions of Excel File 



    if(!empty($_FILES)) { 

     //uploading file first 
     $info = pathinfo($_FILES['userFile']['name']); 
     $ext = $info['extension']; // get the extension of the file 
     $newname = "excelfile.".$ext; 

     $target = $dir.$newname; 
     move_uploaded_file($_FILES['userFile']['tmp_name'], $target); 

     //upload finish wiring csv file now 
     writecsv(); 

    } 

    function wirtecsv(){ 
     // From Directory get only Excel Files in Array 
     if(is_dir($dir)) 
     { 
      if($dh = opendir($dir)) 
      { 
       while(($file = readdir($dh)) !== false) 
       { 
        $info = new SplFileInfo($file); 
        $ext = $info->getExtension();   // Get Extension of Current File 
        if(in_array($ext,$file_ext_arr)) 
        { 
         array_push($file_arr, $file); 
        } 
       } 
       closedir($dh); 
      } 
     } 

     // Make CSV File 
     $fp = fopen('file.csv', 'a'); 
     $list = array(); 

     foreach($file_arr as $val) 
     { 
      $arr_data = array(); 
      $objPHPExcel = PHPExcel_IOFactory::load($dir . $val); 
      $cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection(); 
      foreach($cell_collection as $cell) 
      { 
       $column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn(); 
       $row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow(); 
       $data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue(); 
       //header will/should be in row 1 only. of course this can be modified to suit your need. 
       // Skip Rows From Top if you have header in Excel then Change 0 to 1 
       if($row == 0) 
       { 
        $header[$row][$column] = $data_value; 
       } 
       else 
       { 
        $arr_data[$row]['row'] = $row; 
        $arr_data[$row][$column] = $data_value; 
       } 
      } 
      $data = $arr_data; 
      foreach($data as $val1) 
      { 
       $num_col = sizeof($val1) - 1; // get number of columns in Excel 
       break; 
      } 
      $lwrcol=array();  
      foreach($data as $val2) 
      { 
       $alphaArr = range('A','Z'); 
       $colArr = range('A',$alphaArr[$num_col - 1]); 

       foreach($colArr as $col) 
       { 
        $lwrcol[$col] = isset($val2[$col]) ? utf8_decode($val2[$col]) : ""; 
        fwrite($fp,$lwrcol[$col].","); 
       } 
       fwrite($fp,"\n"); 
      } 
      chmod(getcwd()."/file.csv", 0777); 
     } 
     fclose($fp); 
    } 
    ?> 


    <form action='' method='POST' enctype='multipart/form-data'> 
    <input type='file' name='userFile'><br> 
    <input type='submit' name='upload_btn' value='upload'> 
    </form> 
</html> 
+0

Non voglio spostare file in nessuna cartella prima. Voglio convertire il file Excel in CSV prima del suo caricamento sul server. Quando l'utente seleziona un qualsiasi excel e fa clic sul pulsante UPLOAD in quel momento, il primo processo è Convertire il file Excel nel file CSV e dopo la conversione il file viene caricato nella cartella UPLOAD per usarlo in un altro processo. –

+0

sta visualizzando la pagina vuota –

+0

@SriP non so in questo momento vera causa della pagina vuota. Per questo è necessario eseguire il debug del codice passo dopo passo. – Manwal