PHP 5.6 Read and Write excel Table File Operation Example

  • 2021-11-29 06:13:06
  • OfStack

In this paper, an example is given to describe the operation of reading and writing excel table files by PHP 5.6. Share it for your reference, as follows:

Test environment: php 5.6. 24. There is no compatibility problem with this one.

For more chestnuts, please see examples of PHPExcel. It's quite powerful.

Read the excel file:

Step 1: Download the open source class library file of PHPExcel. The official website is http://www.codeplex.com/PHPExcel. There are also many sample packages in it.

Or download it from this site: https://www.ofstack.com/codes/194070. html

Step 2, read the basic code example:


<?php
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';
require_once 'Classes/PHPExcel/Reader/Excel5.php';
$file_url = './excel/phpLv.xls';
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load($file_url);
// Set the currently active worksheet 
$objPHPExcel->setActiveSheetIndex(1);
// Get the current active table. This will be used for future operations. Labor and capital don't like chain operation. It's too ugly, okay? 
$activeSheet = $objPHPExcel->getActiveSheet();
// Maximum number of rows in the current table 
$highestRow = $activeSheet->getHighestRow();
// Maximum number of columns in the current table 
$highestColumn = $activeSheet->getHighestColumn();
echo " Maximum column: $highestColumn";
echo " Maximum row: $highestRow";
echo '<hr/>';
$cell = function ($cell) use ($activeSheet) {
  return $activeSheet->getCell("$cell")->getValue();
};
$str1 = $cell('A13');
echo $str1;
exit;

Export an excel table file:

The first step, as above, is to download the PHPExcel class library file first.

Step 2, export the sample code for the excel file:


//-------------------------------- Export excel Documents --------------------------------
require_once './Classes/PHPExcel.php';
$objPHPExcel=new PHPExcel();
//1 Something about excel Description of the file. In Classes/PHPExcel/DocumentProperties.php There are more options in 
$prop = $objPHPExcel->getProperties();
$prop->setCreator('sweat_xiaoMa');
$prop->setLastModifiedBy('xiaoma');
$prop->setTitle('Office 2007 XLSX Document');
$prop->setSubject('Office 2007 XLSX Document');
$prop->setDescription('Document for Office 2007 XLSX, generated using PHP classes.');
$prop->setKeywords('office 2007 openxml php');
$prop->setCategory('Result file');
// Set the index of the current worksheet to use 
$objPHPExcel->setActiveSheetIndex(0);
// You can then set the contents of the cell. 
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->setCellValue('A1',' Student number ');
$activeSheet->setCellValue('B1',' Grade ');
$activeSheet->setCellValue('C1',' Class ');
$activeSheet->setCellValue('D1',' Name ');
$activeSheet->setCellValue('E1',' Gender ');
// Sets the title for the worksheet currently in use. 
$activeSheet->setTitle(' Worksheet 1 La la la la ');
// File name. Below header Used in. 
$filename = ' Statistical table of student information _'.date('Y-m-dHis');
/*
* Generate xlsx Documents 
*/
// header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
// header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
// header('Cache-Control: max-age=0');
// $objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
/*
* Generate xls Documents 
*/
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

For more readers interested in PHP related contents, please check the special topics of this site: "Summary of php Operation office Document Skills (including word, excel, access, ppt)", "Encyclopedia of PHP Array (Array) Operation Skills", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "PHP Mathematical Operation Skills Summary", "php Regular Expression Usage Summary", "php String (string) Usage Summary" and "php Common Database Operation Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: