Using PHP to generate Excel files to the specified directory

Source: Internet
Author: User
Tags foreach header strlen

This article mainly introduces the use of PHP to generate Excel files to the specified directory of relevant information, the need for friends can refer to the

Recently the company wants to generate reports and generate them in PHP.

Header ("Content-type:application/vnd.ms-excel");

Header ("Content-disposition:attachment;filename=test_data.xls");

I Baidu, seemingly this can be achieved soon, but this file is generated in the browser to download the place,

I want to generate the generated files to the specified directory, so can I implement it?

Also, can you insert a picture inside?

Phpexcel is in English, read for a long time did not read. Is there a direct-generated example to look at?

Here we enclose an example:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 /** +----------------------------------------------------------* Import required class library with Java Import * This function has caching function +------------------ ----------------------------------------* @param string $class class Library namespace String * @param string $baseUrl start path * @param string $e XT imported file name extension +----------------------------------------------------------* @return Boolen +-------------------------- --------------------------------/Function Import ($class, $baseUrl = ', $ext = '. class.php ') {static $_file = array (); $ class = Str_replace (Array ('. ', ' # '), Array ('/', '. '), $class); if (' = = = $baseUrl && false = = Strpos ($class, '/')} {//Check alias import return Alias_import ($class);} if (Isset $_file[ $class. $BASEURL])) return true; else $_file[$class. $baseUrl] = true; $class _strut = explode ('/', $class); if (empty ($BASEURL)) {if (' @ ' = = $class _strut[0] | | App_name = = $class _strut[0]) {//Load current Project Application class Library $BASEURL = DirName (lib_path); $class = Substr_replace ($class, basename (LIB_PA TH). ' /', 0, strlen ($class _strut[0]) + 1); }elseif (' tHink ' = = Strtolower ($class _strut[0])) {//The official base Class library $baseUrl = Core_path; $class = substr ($class, 6);} ElseIf (Strtolower ($class _strut[0]), array (' org ', ' com ')) {/Org Third party common class Library COM enterprise Common class library $BASEURL = Library_path;} else {//Load other Project Application class Library $class = Substr_replace ($class, ', 0, strlen ($class _strut[0]) + 1); $baseUrl = App_path. '.. /' . $class _strut[0]. '/'. basename (Lib_path). ' /'; } if (substr ($BASEURL,-1)!= '/') $baseUrl. = '/'; $classfile = $baseUrl. $class. $ext; if (!class_exists (basename ($class), false) {//If the class does not exist, import the class library file return Require_cache ($classfile);}  /** * Export Excel Table * @param array $data data, two-dimensional array, each data one record * @param array $title The field name of each column data, an array must be consistent with the data order (can be omitted) * @param s Tring $filename Excel Name * @param array $field You need to specify the exported data field, and the sort must be consistent with the title, which is the array key value of the detected data/function exportexcel ($data = ', $ Title= ', $filename = ' Excel ', $field =array ()) {if (! $data | | | |!is_array ($DATA)) return false; if ($filename = = ") $filename = ' Excel '; if ($field && Is_array ($field)) {//As long as the export refersSet the field and export $dateNew =array () in this order; foreach ($data as $k => $v) {foreach ($field as $fkey) {$dateNew [$k] [$fkey]= $v [$fkey];}} $data = $dateNew; }   Import ("@.org.util.excelxml")//Call export Excel class $xls = new Excelxml (' UTF-8 ', false, ' Sheet1 '); $xls->addarray ($data, $title); $xls->generatexml ($filename); }

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.