Example of generating and reading excel files in phpexcel

Source: Internet
Author: User
Example of generating and reading excel files in phpexcel

  1. Require_once 'classes/phpexcel/reader/excel2007.php ';
  2. Require_once 'classes/phpexcel/reader/excel5.php ';
  3. Include 'classes/phpexcel/iofactory. php ';
  4. Function arraytoexcel ($ data ){
  5. $ Objphpexcel = new phpexcel ();
  6. $ Objphpexcel-> setactivesheetindex (0 );
  7. $ Objphpexcel-> getactivesheet ()-> settitle ('firstsheet ');
  8. $ Objphpexcel-> getdefaultstyle ()-> getfont ()-> setname ('arial ');
  9. $ Objphpexcel-> getdefaultstyle ()-> getfont ()-> setsize (10 );
  10. // Add data
  11. $ I = 2;
  12. Foreach ($ data as $ line ){
  13. $ Objphpexcel-> getactivesheet ()-> setcellvalue ('A'. $ I, $ line ['from']);
  14. $ Objphpexcel-> getactivesheet ()-> getcell ('A'. $ I)-> setdatatype ('N ');
  15. $ Objphpexcel-> getactivesheet ()-> setcellvalue ('B'. $ I, $ line ['to']);
  16. $ Objphpexcel-> getactivesheet ()-> getcell ('B'. $ I)-> setdatatype ('N ');
  17. $ I ++;
  18. }
  19. $ Objwriter = phpexcel_iofactory: createwriter ($ objphpexcel, 'excel5 ');
  20. $ File = 'excel.xls ';
  21. $ Objwriter-> save ($ file );
  22. }

>>> For more php Tutorial content, please pay attention to the php programming section on this site.

If you do not want to save it on the server and want to download it directly to the client after generation, you can add the following code when outputting the file without using $ objwriter-> save ($ file );

  1. Header ("pragma: public ");
  2. Header ("expires: 0 ");
  3. Header ("cache-control: must-revalidate, post-check = 0, pre-check = 0 ");
  4. Header ("content-type: application/force-download ");
  5. Header ("content-type: application/vnd. ms-execl ");
  6. Header ("content-type: application/octet-stream ");
  7. Header ("content-type: application/download ");
  8. Header ('content-disposition: attachment; filename = "excel.xls "');
  9. Header ("content-transfer-encoding: binary ");
  10. $ Objwriter-> save ('php: // output ');

Here is an example of reading the content of an excel file: the following code function exceltoarray is used to reorganize the content in an excel file and put it in an array.

  1. Require_once 'classes/phpexcel. php ';
  2. Require_once 'classes/phpexcel/iofactory. php ';
  3. Function exceltoarray ($ file ){
  4. $ Objreader = phpexcel_iofactory: createreader ('excel5 ');
  5. $ Objreader-> setreaddataonly (true );
  6. $ Objphpexcel = $ objreader-> load ($ file );
  7. $ Objworksheet = $ objphpexcel-> getactivesheet ();
  8. $ Highestrow = $ objworksheet-> gethighestrow ();
  9. $ Highestcolumn = $ objworksheet-> gethighestcolumn ();
  10. $ Highestcolumnindex = phpexcel_cell: columnindexfromstring ($ highestcolumn );
  11. $ Exceldata = array ();
  12. For ($ row = 2; $ row <= $ highestrow; ++ $ row ){
  13. For ($ col = 0; $ col <= $ highestcolumnindex; ++ $ col ){
  14. $ Exceldata [$ row] [] = $ objworksheet-> getcellbycolumnandrow ($ col, $ row)-> getvalue ();
  15. }
  16. }
  17. Return $ exceldata;
  18. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.