Use PHPExcel to read Excel Data and export data to Excel, phpexcelexcel
PHPExcel is a PHP class library that helps us read and export Excel Data from Excel in a simple and efficient manner. It is also a common application scenario in our daily development. For example, if you have a customer information table and want to export it to colleagues in batches, you can use PHPExcel to quickly implement it. Similarly, if we want to use the SMS Mass Sending interface to send group information, PHPExcel can quickly import customer information to avoid manual information input.
PHP Excel Tutorial:
First download PHPExcel
To begin.
Download the file and decompress it to obtain the following file:
PHPExcel
Now we can use PHPExcel to read and create tables happily!
PHPExcel DEMO1: Use PHPExcel to read excel information:
Create a file named read.php in the root directory to read the document and create a file named test.xlsx. The information written in the file is as follows:
Then write the following code in read. php:
<? Phpinclude '. /Classes/PHPExcel/IOFactory. php '; $ inputFileName = '. /test.xls '; date_default_timezone_set ('prc'); // read the excel file try {$ inputFileType = PHPExcel_IOFactory: identify ($ inputFileName); $ objReader = PHPExcel_IOFactory :: createReader ($ inputFileType); $ objPHPExcel = $ objReader-> load ($ inputFileName);} catch (Exception $ e) {die ('file loading error :"'. pathinfo ($ inputFileName, PATHINFO_BASENAME ). '":'. $ e-> getMessage ();} // determine the sheet to read and what is sheet. Check the bottom right corner of excel, I really don't know about Baidu. $ sheet = $ objPHPExcel-> getSheet (0); $ highestRow = $ sheet-> getHighestRow (); $ highestColumn = $ sheet-> getHighestColumn (); // get the data of a row for ($ row = 1; $ row <= $ highestRow; $ row ++) {// Read a row of data into an array $ rowData = $ sheet-> rangeToArray ('A '. $ row. ':'. $ highestColumn. $ row, NULL, TRUE, FALSE); // The rowData obtained here is a row of data, which is processed by yourself after the data is obtained. Here we only play it to see the effect of var_dump ($ rowData ); echo "<br> ";}
Data obtained:
PHPexcel File Reading
PHPExcel DEMO2: Use PHPExcel to export information to excel:
<? Php/** Error reporting */error_reporting (E_ALL); ini_set ('display _ errors ', TRUE); ini_set ('display _ startup_errors', TRUE ); date_default_timezone_set ('prc');/** introduce PHPExcel */require_once dirname (_ FILE __). '. /Classes/PHPExcel. php '; // create an Excel file object $ objPHPExcel = new PHPExcel (); // set the document information. In windows, right-click the file attribute to view $ objPHPExcel-> getProperties () -> setCreator ("")-> setLastModifiedBy ("")-> setTitle ("document title")-> setSubject ("document topic ") -> setDescription ("document description")-> setKeywords ("set document keywords")-> setCategory ("set document category"); // Based on excel coordinates, add data $ objPHPExcel-> setActiveSheetIndex (0)-> setCellValue ('a1', '')-> setCellValue ('b2', 'World ') -> setCellValue ('c1 ', '')-> setCellValue ('d2 ', 'World'); // contains various symbols, encoded as UTF-8 $ objPHPExcel-> setActiveSheetIndex (0)-> setCellValue ('a4 ', 'miscellaneous glyphs')-> setCellValue ('a5 ', ''' *************äöüç'); $ objPHPExcel-> getActiveSheet ()-> setCellValue ('a8 ', "Hello World"); $ objPHPExcel-> getActiveSheet ()-> getRowDimension (8)-> setRowHeight (-1); $ objPHPExcel-> getActiveSheet () -> getStyle ('a8 ')-> getAlignment ()-> setWrapText (true); $ value = "-ValueA \ n-Value B \ n-Value C "; $ objPHPExcel-> getActiveSheet ()-> setCellValue ('a10', $ value); $ objPHPExcel-> getActiveSheet ()-> getRowDimension (10)-> setRowHeight (-1 ); $ objPHPExcel-> getActiveSheet ()-> getStyle ('a10')-> getAlignment ()-> setWrapText (true); $ objPHPExcel-> getActiveSheet () -> getStyle ('a10')-> setQuotePrefix (true); // rename the Work sheet $ objPHPExcel-> getActiveSheet ()-> setTitle ('first sheet '); // set the first sheet as the working sheet $ objPHPExcel-> setActiveSheetIndex (0); // Save the Excel worksheet $ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel2007 '); $ objWriter-> save ('export.xlsx'); // save the Excel 95 format file. The save path is the current path. $ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5'); $ objWriter-> save('export.xls ');
After the execution, we can get export.xlsand export.xlsx.
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!