2015-03-18 134 views
0

我用php完成了excel的讀取,但是寫不了,我不知道爲什麼.. 要在文件中讀取,取一列並寫入一個新文件excel並粘貼到那裏是...現在只有.. 感謝所有..phpexcel excel的讀寫

<?php 
    require_once "excel/Classes/PHPExcel.php"; 
    require_once "excel/Classes/PHPExcel/IOFactory.php"; 
    mb_internal_encoding('UTF-8'); 
    mb_http_output('UTF-8'); 
    set_time_limit (0); 
    ini_set('memory_limit', '-1'); 
    error_reporting(E_ALL); 
    ini_set('display_errors', TRUE); 
    ini_set('display_startup_errors', TRUE); 

    ?> 
    <form action="" method="post" enctype="multipart/form-data"> 
     Desea comenzar el proceso ? 

     <input type="submit" value="Aceptar" name="submit"> 
    </form> 



    <?php 



    if(isset($_POST['submit'])){ 

     $objPHPExcel_load = new PHPExcel(); 
     $PHPExcel_save = new PHPExcel(); 
     $PHPExcel_save->getProperties()->setCreator("Maarten Balliauw") 
           ->setLastModifiedBy("Maarten Balliauw") 
           ->setTitle("Office 2007 XLSX Test Document") 
           ->setSubject("Office 2007 XLSX Test Document") 
           ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") 
           ->setKeywords("office 2007 openxml php") 
           ->setCategory("Test result file"); 
     $PHPExcel_save->setActiveSheetIndex(0) 
       ->setCellValue('A1', 'Cedula'); 
     $PHPExcel_save->getActiveSheet()->setTitle('Simple'); 
     $PHPExcel_save->setActiveSheetIndex(0); 
     $inputFileName = 'discussdesk.xlsx'; 
     $objPHPExcel_load = PHPExcel_IOFactory::load($inputFileName); 
     $objWorksheet = $objPHPExcel_load->getActiveSheet(); 
     $cantidad = $objWorksheet->getHighestRow();// tamaño de los registros 
     $objeto_pegar = $PHPExcel_save->getActiveSheet(0); 
     echo "cantidad : ".$cantidad; 
     $prueba = true; 
     $row = 2; 
     $column = 1; 
     for($i = 1;$i <= $cantidad;$i++){ 
      $celda = trim($objWorksheet->getCellByColumnAndRow(7, $row)->getValue()); 
      $PHPExcel_save->setActiveSheetIndex(0)->setCellValueByColumnAndRow(1, $row, $celda); 

      echo $celda.'<br>'; 
      if(is_null($celda)){ 
       break; 
      } 
      $row +=1; 
     } 
     $PHPExcel_save->setActiveSheetIndex(0); 

     header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 
     header('Content-Disposition: attachment;filename="01simple.xlsx"'); 
     header('Cache-Control: max-age=0'); 
    // If you're serving to IE 9, then the following may be needed 
     header('Cache-Control: max-age=1'); 

     // If you're serving to IE over SSL, then the following may be needed 
     header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past 
     header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified 
     header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 
     header ('Pragma: public'); // HTTP/1.0 

     $objWriter = PHPExcel_IOFactory::createWriter($PHPExcel_save, 'Excel2007'); 
     $objWriter->save('php://output'); 


    } 


    ?> 

回答

0
$PHPExcel_writer = new PHPExcel(); 
    $objReader = PHPExcel_IOFactory::createReader('Excel2007'); // crea el archivo a leer 
    $PHPExcel_ = $objReader->load('testFile.xlsx'); // load the file 
    $PHPExcel_->setActiveSheetIndex(0) 
      ->setCellValue('A1', 'field1') 
      ->setCellValue('B1', 'field2') 
      ->setCellValue('C1', 'field3') 
      ->setCellValue('D1', 'field4') 
      ->setCellValue('E1', 'field5') 
      ->setCellValue('F1', 'field6'); // write the first row(tittle) 


    //$PHPExcel_writer->setActiveSheet()->setTitle('Simple'); 
    $PHPExcel_->setActiveSheetIndex(0); 
    // 
    $objPHPExcel_load = new PHPExcel(); 
    $inputFileName = 'discussdesk.xlsx'; 
    $objPHPExcel_load = PHPExcel_IOFactory::load($inputFileName); 
    $objWorksheet = $objPHPExcel_load->getActiveSheet(); 
    $cantidad = $objWorksheet->getHighestRow();// size of row*/ 


    //echo "count : ".$cantidad; 

    //$row = 2; 
    //$column = 1; 
    //$PHPExcel_writer->getActiveSheet()->setTitle('Simple'); 
    //$PHPExcel_writer->setActiveSheetIndex(0)->setCellValueByColumnAndRow(1, 1, "prueba"); 

    for($i = 1;$i <= 1000;$i++){ ?> 
     <script> envia_barra(<? echo $i; ?>); 

       </script> 
     <?php 
     /*$celda = trim($objWorksheet->getCellByColumnAndRow(12, $row+3)->getValue()); 
     $data = file_get_contents("youweb?param=".$celda); 
     set_time_limit(5); 
     $PHPExcel_->getActiveSheetIndex(0); 
     if(preg_match('|<td width="180" class="tblbgcolort"><strong>Departamento:</strong></td>\s+<td width="400" class="tblbgcolort">(.*?)</td>|is' , $data , $dep)){ 
      preg_match('|<td width="180" class="tblbgcolort"><strong>Departamento:</strong></td>\s+<td width="400" class="tblbgcolort">(.*?)</td>|is' , $data , $dep); 
      preg_match('|<td class="tblbgcolor"><strong>Municipio:</strong></td>\s+<td class="tblbgcolor">(.*?)</td>|is' , $data , $mun); 
      preg_match('%<strong>Puesto:</strong>.+?<td>(.+?)</td>%si',$data,$puesto); 
      preg_match("%<div style='float:left;'>(.*?)<\/div>%si", $data , $dir); 
      preg_match('|<td class="tblbgcolor"><strong>Mesa</strong></td>\s+<td class="tblbgcolor">(.*?)</td>|is' , $data , $mes); 
      $PHPExcel_->setActiveSheetIndex(0)->setCellValueByColumnAndRow(0,$row, $celda); 
      $PHPExcel_->setActiveSheetIndex(0)->setCellValueByColumnAndRow(1,$row,$dep[1]); 
      $PHPExcel_->setActiveSheetIndex(0)->setCellValueByColumnAndRow(2,$row,$mun[1]); 
      $PHPExcel_->setActiveSheetIndex(0)->setCellValueByColumnAndRow(3,$row,$puesto[1]); 
      $PHPExcel_->setActiveSheetIndex(0)->setCellValueByColumnAndRow(4,$row,$dir[1]); 
      $PHPExcel_->setActiveSheetIndex(0)->setCellValueByColumnAndRow(5,$row,$mes[1]); 

     }else{ 
      $PHPExcel_->getActiveSheet()->setCellValueByColumnAndRow(0,$row,$celda); 
      $PHPExcel_->getActiveSheet()->setCellValueByColumnAndRow(1,$row ,"No hay registro"); 
     } 
     $row++; 
     if(is_null($celda)){ 
      break; 
     } 
    } 
    //$PHPExcel_writer->setActiveSheetIndex(0); 
    $objWriter = PHPExcel_IOFactory::createWriter($PHPExcel_, 'Excel2007'); 
    $objWriter->save('testFile.xlsx'); 
    //$objWriter->save('php://output'); 
    //header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
    //header("Content-Disposition: attachment;filename=\"discussdesk.xlsx\""); 
    //header("Cache-Control: max-age=0"); 
    //$objWriter = PHPExcel_IOFactory::createWriter($PHPExcel_writer, 'Excel2007'); 
    //$objWriter->save('php://output'); 
    exit;