Visualizzazione dei risultati da 1 a 2 su 2

Discussione: Excel To CSV

  1. #1

    Excel To CSV

    Salve a tutti
    In poche parole io dovrei prendere un file .XLS aprirlo cancellare le colonne B e C, ed tutte quelle da E in poi, per salvarlo infine in un file CSV.
    Ho scelto di optare per le classi PhpExcel di Codeplex.
    Ma ho un piccolo problema in quanto nel mio codice apro prima il file Xls cancello B e C, e lo salvo in un file. Poi riapro questo file, cancello le altre colonne e salvo ancora.
    Il problema e' che nel CSV finale mi ritrovo con l'ultima riga senza le colonne eliminate, e tutte le precedenti righe quindi con tutti i valori null.
    Sto impazzendo aiutatemi, ecco il mio codice:
    Codice PHP:
    <?php error_reporting(E_ALL); set_time_limit(0); date_default_timezone_set('Europe/London'); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>PHPExcel Reader Raffa </title> </head> <body> <h1>PHPExcel Reader Raffa </h1> <h2>Simple File Reader Using a Configurable Read Filter</h2> <?php /** Include path **/ set_include_path(get_include_path() . PATH_SEPARATOR '../../../Classes/'); /** PHPExcel_IOFactory */ include 'PHPExcel/IOFactory.php'$inputFileType 'Excel5'// $inputFileType = 'Excel2007'; // $inputFileType = 'Excel2003XML'; // $inputFileType = 'OOCalc'; // $inputFileType = 'Gnumeric'; $inputFileName = './sampleData/prodotti.xls'; $sheetname = 'Foglio 01'; $objReader = PHPExcel_IOFactory::createReader('Excel5'); $objReader->setReadDataOnly(true); $objPHPExcel = $objReader->load($inputFileName); $objPHPExcel->getActiveSheet()->removeColumn( 'B',2); $objWorksheet = $objPHPExcel->setActiveSheetIndex(0); $highestRow = $objWorksheet->getHighestRow(); $highestColumn = $objWorksheet->getHighestColumn(); $headingsArray = $objWorksheet->rangeToArray('A1:'.$highestColumn.'1',null, true, true, true); $headingsArray = $headingsArray[1]; $r = -1; $namedDataArray = array(); for ($row = 2; $row <= $highestRow; ++$row) { $dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, true); if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) { ++$r; foreach($headingsArray as $columnKey => $columnHeading) { $namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey]; } } } $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); $objWriter->save('./sampleData/cazzone.xls'); $objWriter = new PHPExcel_Writer_CSV($objPHPExcel); $objWriter->save('./sampleData/cazzone.csv'); echo '<pre>'; var_dump($namedDataArray); echo '</pre><hr />'; $inputFileType = 'Excel5'; $inputFileName1 = './sampleData/cazzone.xls'; $sheetname1 = 'Foglio 01'; $objReader1 = PHPExcel_IOFactory::createReader('Excel5'); $objReader1->setReadDataOnly(true); $objPHPExcel1 = $objReader1->load($inputFileName1); $objWorksheet1 = $objPHPExcel1->setActiveSheetIndex(0); $highestRow1 = $objWorksheet1->getHighestRow(); $highestColumn1 = $objWorksheet1->getHighestColumn(); $headingsArray1 = $objWorksheet1->rangeToArray('A1:'.$highestColumn1.'1',null, true, true, true); $headingsArray1 = $headingsArray1[1]; $objPHPExcel1->getActiveSheet()->removeColumn( 'C',20); $r = -1; $namedDataArray1 = array(); for ($row = 2; $row <= $highestRow1; ++$row) { $dataRow1 = $objWorksheet1->rangeToArray('A'.$row.':'.$highestColumn1.$row,null, true, true, true); if ((isset($dataRow1[$row]['A'])) && ($dataRow1[$row]['A'] > '')) { ++$r; foreach($headingsArray1 as $columnKey => $columnHeading) { $namedDataArray1[$r][$columnHeading] = $dataRow1[$row][$columnKey]; } } } $objWriter1 = new PHPExcel_Writer_CSV($objPHPExcel1); $objWriter1->save('./sampleData/cazzone1.csv'); echo '<pre>'; var_dump($namedDataArray1); echo '</pre><hr />';

  2. #2
    Scusate ecco il codice:
    codice:
     
    
    Codice PHP:
    <?php error_reporting(E_ALL); set_time_limit(0); date_default_timezone_set('Europe/London'); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>PHPExcel Reader Raffa </title> </head> <body> <h1>PHPExcel Reader Raffa </h1> <h2>Simple File Reader Using a Configurable Read Filter</h2> <?php /** Include path **/ set_include_path(get_include_path() . PATH_SEPARATOR '../../../Classes/'); /** PHPExcel_IOFactory */ include 'PHPExcel/IOFactory.php'; $inputFileType 'Excel5'; //    $inputFileType = 'Excel2007'; //    $inputFileType = 'Excel2003XML'; //    $inputFileType = 'OOCalc'; //    $inputFileType = 'Gnumeric'; $inputFileName './sampleData/prodotti.xls'; $sheetname 'Foglio 01'; $objReader PHPExcel_IOFactory::createReader('Excel5'); $objReader->setReadDataOnly(true); $objPHPExcel $objReader->load($inputFileName); $objPHPExcel->getActiveSheet()->removeColumn'B',2); $objWorksheet $objPHPExcel->setActiveSheetIndex(0); $highestRow $objWorksheet->getHighestRow(); $highestColumn $objWorksheet->getHighestColumn(); $headingsArray $objWorksheet->rangeToArray('A1:'.$highestColumn.'1',nulltruetruetrue); $headingsArray $headingsArray[1]; $r = -1; $namedDataArray = array(); for ($row 2$row <= $highestRow; ++$row) {     $dataRow $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,nulltruetruetrue);     if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {         ++$r;         foreach($headingsArray as $columnKey => $columnHeading) {             $namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];         }     } } $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); $objWriter->save('./sampleData/cazzone.xls'); $objWriter = new PHPExcel_Writer_CSV($objPHPExcel); $objWriter->save('./sampleData/cazzone.csv'); echo '<pre>'; var_dump($namedDataArray); echo '</pre><hr />'; $inputFileType 'Excel5'; $inputFileName1 './sampleData/cazzone.xls'; $sheetname1 'Foglio 01'; $objReader1 PHPExcel_IOFactory::createReader('Excel5'); $objReader1->setReadDataOnly(true); $objPHPExcel1 $objReader1->load($inputFileName1); $objWorksheet1 $objPHPExcel1->setActiveSheetIndex(0); $highestRow1 $objWorksheet1->getHighestRow(); $highestColumn1 $objWorksheet1->getHighestColumn(); $headingsArray1 $objWorksheet1->rangeToArray('A1:'.$highestColumn1.'1',nulltruetruetrue); $headingsArray1 $headingsArray1[1]; $objPHPExcel1->getActiveSheet()->removeColumn'C',20); $r = -1; $namedDataArray1 = array(); for ($row 2$row <= $highestRow1; ++$row) {     $dataRow1 $objWorksheet1->rangeToArray('A'.$row.':'.$highestColumn1.$row,nulltruetruetrue);     if ((isset($dataRow1[$row]['A'])) && ($dataRow1[$row]['A'] > '')) {         ++$r;         foreach($headingsArray1 as $columnKey => $columnHeading) {             $namedDataArray1[$r][$columnHeading] = $dataRow1[$row][$columnKey];         }     } } $objWriter1 = new PHPExcel_Writer_CSV($objPHPExcel1); $objWriter1->save('./sampleData/cazzone1.csv'); echo '<pre>'; var_dump($namedDataArray1); echo '</pre><hr />';

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.