xlsx to csv with selected cells

Topics: Developer Forum, Project Management Forum, User Forum
Apr 25, 2012 at 12:09 PM

Hallo,

Can you guys help me with a script that gets selective data from given cells within a xlsx file and makes a csv file from that data.

This is wat i got now.

<?php
include 'Classes/PHPExcel.php';

$inputFileType = 'Excel5'; 
$inputFileName = 'example.xls'; 
$sheetname = 'Data Sheet #1'; 


/**  Define a Read Filter class implementing PHPExcel_Reader_IReadFilter  */ 
class MyReadFilter implements PHPExcel_Reader_IReadFilter 
{ 
    public function readCell($column, $row, $worksheetName = '') { 
        //  Read rows 11 to 17 and columns X to Z only 
        if ($row >= 11 && $row <= 17) { 
            if (in_array($column,range('X','Z'))) { 
                return true; 
            } 
        } 
        return false; 
    } 
} 

/**  Create an Instance of our Read Filter  **/ 
$filterSubset = new MyReadFilter(); 

/**  Create a new Reader of the type defined in $inputFileType  **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType); 
/**  Tell the Reader that we want to use the Read Filter  **/ 
$objReader->setReadFilter($filterSubset); 
/**  Load only the rows and columns that match our filter to PHPExcel  **/ 
$objPHPExcel = $objReader->load($inputFileName);

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client�s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>

This one only selects rows 11 to 17 and cols X to Z, then it makes a xls file from that data.
The script works good but i want to create a csv file comma delimitted and also add other cells like B2, B3, E2, E5 etc.

Can anyone help me with this, i'm a beginner PHP programmer.