Home  >  Article  >  Backend Development  >  PHPExcel reads Excel files_PHP tutorial

PHPExcel reads Excel files_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:47:38724browse

Use PHPExcel to read Excel 2007 or Excel2003 files

Knowledge points involved: www.2cto.com

PHP loops to read excel files

PHP converts characters into ascii encoding and converts characters into decimal numbers

PHP reads the excel date format and performs display conversion

PHP encodes and converts garbled Chinese characters

View Code

require_once 'PHPExcel.php';

/**Convert date format in excel*/

function GetData($val){

$jd = GregorianToJD(1, 1, 1970);

$gregorian = JDToGregorian($jd+intval($val)-25569);

Return $gregorian;/**The display format is "month/day/year"*/

}

$filePath = 'test.xlsx';

$PHPExcel = new PHPExcel();

/**By default, excel2007 is used to read excel. If the format is incorrect, the previous version will be used to read it.*/

$PHPReader = new PHPExcel_Reader_Excel2007();

if(!$PHPReader->canRead($filePath)){

$PHPReader = new PHPExcel_Reader_Excel5();

If(!$PHPReader->canRead($filePath)){

echo 'no Excel';

         return ;

}

}

$PHPExcel = $PHPReader->load($filePath);

/**Read the first worksheet in excel file*/

$currentSheet = $PHPExcel->getSheet(0);

/**Get the largest column number*/

$allColumn = $currentSheet->getHighestColumn();

/**Get the total number of rows*/

$allRow = $currentSheet->getHighestRow();

/**Start outputting from the second row because the first row in the excel table is the column name*/

for($currentRow = 2;$currentRow <= $allRow;$currentRow++){

/**Start output from column A*/

for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){

$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();/**ord() converts characters into decimal numbers*/

If($currentColumn == 'A')

           {

                   echo GetData($val)."t";

         }else{

                       //echo $val;

               /**If the output Chinese characters are garbled, you need to use the iconv function to convert the output content. As follows, convert gb2312 encoding to utf-8 encoding for output.*/

                 echo iconv('utf-8','gb2312', $val)."t";

}

}

echo "
";

}

echo "n";

?>

Excerpted from Pieces of clouds passing by

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478504.htmlTechArticleUsing PHPExcel to read Excel 2007 or Excel2003 files involves knowledge points: www.2cto.com PHP loops excel files Read PHP and convert the characters into ascii encoding and convert the characters into decimal numbers...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn