PHP to read and write Excel and CSV files

    PHPExcel open source PHP framework used to read and write Excel and CSV files as used herein, is the whole function of the framework. You can read and write xls, xlsx, csv, ods, html, and even pdf. Very powerful, source code, see
Documentation is also very detailed:
https://github.com/PHPOffice/PHPExcel/blob/1.8/Documentation/PHPExcel%20developer%20documentation.doc

The main function of this paper is completed by PHPExcel read Excel specified in rows and columns, written to the CSV file in rows and columns. The purpose is the output format, then Excel file of data.
The code is relatively straightforward on the code:
<?php

require_once dirname(__FILE__) . '/Classes/PHPExcel/IOFactory.php';

//input output and template file name
$inputFile = "test.xlsx";
$templateFile = "templet.csv";
$outputFile = "templet_out.csv";

//deal with php arguments
for($i = 1;$i< $argc;$i++) {
    switch($argv[$i]) {
        case "-i": $inputfile = $argv[$i+1];break;
        case "-o": $outputfile = $argv[$i+1];break;
        default:break;
    }
}
echo "\$inputfile = $inputFile , \$outputfile = $outputFile \n";

//load input excel file.
$objPHPExcel = PHPExcel_IOFactory::load($inputFile);

//load csv file as excel object
$objReader = PHPExcel_IOFactory::createReader('CSV')->setDelimiter(',')
                                                    ->setEnclosure('"')
                                                    ->setSheetIndex(0);
$objPHPExcelFromCSV = $objReader->load($templateFile);

//read the excel and write csv file.
$index = 2;
while(1) {
    $id = $objPHPExcel->getActiveSheet()->getCell('A'.$index)->getValue();
    $des = $objPHPExcel->getActiveSheet()->getCell('C'.$index)->getValue();
    if($id != "") {
        $objPHPExcelFromCSV->getActiveSheet()->getCell('B'.$index)->setValue($id);
        $objPHPExcelFromCSV->getActiveSheet()->getCell('C'.$index)->setValue($des);
        $objPHPExcelFromCSV->getActiveSheet()->getCell('A'.$index)->setValue("/Test(#7)");
        $objPHPExcelFromCSV->getActiveSheet()->getCell('F'.$index)->setValue("功能测试");
        echo $id."\n";
    } else {
        break;
    }
    $index++;
}

echo "convert success count = ".($index-2)."\n";

//save output file .
$objWriterCSV = PHPExcel_IOFactory::createWriter($objPHPExcelFromCSV, 'CSV');
$objWriterCSV->setUseBOM(true);
$objWriterCSV->save($outputFile);

    php file of this paper is to use the following script php xxx.php performed in linux, you can use http to resolve. Classes need to rely on under this PHPExcel and directory to directory before executing the next php with php file.

    More and methods of use, I recommend watching English document PHPExcel provided, if there is no confidence in their English can look at:

   http://blog.csdn.net/beyond__devil/article/details/53457849

Which explains several examples of official documents, you can look at.

Published 35 original articles · won praise 9 · views 30000 +

Guess you like

Origin blog.csdn.net/ZHOUYONGXYZ/article/details/72901926