PHP-specific application for reading Excel files-PHP Tutorial

Source: Internet
Author: User
Tags php excel
Use PHP to read Excel files. In this article, we will introduce in detail some examples of PHP Excel file reading code :? Phprequire_once... Excelreader. php; ExcelFile ($ filename, $ encoding); $ data we will introduce you in detail in this article

PHP code example for reading Excel files:

  1. <? Php
  2. Require_once '../Excel/reader. php ';
  3. // ExcelFile ($ filename, $ encoding );
  4. $ Data = new Spreadsheet_Excel_Reader ();
  5. // Set output Encoding. specify the Chinese code
  6. $ Data->SetOutputEncoding ('gb2312 ');
  7. /***
  8. * If you want you can change 'iconv'
    To mb_convert_encoding:
  9. * $ Data->SetUTFEncoder ('mb ');
  10. *
  11. **/
  12. /***
  13. * By default rows & cols indeces start with 1
  14. * For change initial index use:
  15. * $ Data->SetRowColOffset (0 );
  16. *
  17. **/
  18. /***
  19. * Some function for formatting output.
  20. * $ Data->SetDefaultFormat ('%. 2f ');
  21. * SetDefaultFormat-set format
    Columns with unknown formatting
  22. *
  23. * $ Data->SetColumnFormat (4, '%. 3f ');
  24. * SetColumnFormat-set format
    Column (apply only to number fields)
  25. *
  26. **/
  27. // Specify the excel file to be read
  28. $ Data->Read ('../uploads/jsxz01.xls ');
  29. /*
  30. $ Data->Sheets [0] ['numrows ']-count rows
  31. $ Data->Sheets [0] ['numcols']-count columns
  32. $ Data->Sheets [0] ['cells '] [$ I] [$ j]-
    Data from $ I-row $ j-column
  33. $ Data->Sheets [0] ['cellsinfo'] [$ I] [$ j]-
    Extended info about cell
  34. $ Data->Sheets [0] ['cellsinfo'] [$ I] [$ j]
    ['Type'] = "date" | "number" | "unknown"
  35. If 'type' = "unknown"-use 'raw'
    Value, because cell contain value
    Format '0. 00 ';
  36. $ Data->Sheets [0] ['cellsinfo'] [$ I] [$ j]
    ['Raw'] = value if cell without format
  37. $ Data->Sheets [0] ['cellsinfo'] [$ I] [$ j]
    ['Colspan ']
  38. $ Data->Sheets [0] ['cellsinfo'] [$ I] [$ j]
    ['Rowspan ']
  39. */
  40. Error_reporting (E_ALL ^ E_NOTICE );
  41. // Read each unit value cyclically
  42. For ($ I = 1; $ I<= $ Data->Sheets [0]
    ['Numrows ']; $ I ++ ){
  43. For ($ j = 1; $ j<= $ Data->Sheets [0]
    ['Numcols']; $ j ++ ){
  44. Echo ". $ data->Sheets [0] ['cells]
    [$ I] [$ j]. "", ";
  45. }
  46. Echo"
    ";
  47. }
  48. // Print_r ($ data );
  49. // Print_r ($ data->FormatRecords );
  50. ?>

The above is the full usage of PHP to read Excel files as a reference object.


Example of PHP code for reading Excel files :? Php require_once '../Excel/reader. php'; // ExcelFile ($ filename, $ encoding); $ data...

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.