How to convert Excel XLS to CSV using PHP

后端 未结 4 1988
面向向阳花
面向向阳花 2020-11-30 08:39

Can anyone guide me how to convert XLS to CSV using PHP?

I have excel spread sheet which contains a list of documents, I want to convert this with CSV format using P

相关标签:
4条回答
  • You can use the php library PHPExcel to read the excel file, and just loop over the rows and cells and just write the data out to a csv file?

    0 讨论(0)
  • 2020-11-30 09:16

    Probably you can start reading a XLS using PHP.

    Then, using the main logic to output what you want (csv in your case).

    Good luck,

    0 讨论(0)
  • 2020-11-30 09:20

    Rewrite the code provided by @Rajat Modi using PhpSpreadsheet library due to PHPExcel is deprecated.

    https://github.com/PHPOffice/PhpSpreadsheet

    https://phpspreadsheet.readthedocs.io/en/develop/

    <?php 
    
    require 'vendor\autoload.php';
    
    use \PhpOffice\PhpSpreadsheet\Reader\Xlsx;
    use \PhpOffice\PhpSpreadsheet\Writer\Csv;
    
    $xls_file = "Example.xlsx";
    
    $reader = new Xlsx();
    $spreadsheet = $reader->load($xls_file);
    
    $loadedSheetNames = $spreadsheet->getSheetNames();
    
    $writer = new Csv($spreadsheet);
    
    foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
        $writer->setSheetIndex($sheetIndex);
        $writer->save($loadedSheetName.'.csv');
    }
    
    0 讨论(0)
  • 2020-11-30 09:23

    This will surely work,

    require_once 'Classes/PHPExcel/IOFactory.php';
    
    $inputFileType = 'Excel5';
    $inputFileName = 'YOUR_EXCEL_FILE_PATH';
    
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcelReader = $objReader->load($inputFileName);
    
    $loadedSheetNames = $objPHPExcelReader->getSheetNames();
    
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
    
    foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
        $objWriter->setSheetIndex($sheetIndex);
        $objWriter->save($loadedSheetName.'.csv');
    }
    

    Hope this helps...

    0 讨论(0)
提交回复
热议问题