Home > Article > Backend Development > Use PHP to generate excel files to the specified directory_PHP tutorial
This article mainly introduces the relevant information about using PHP to generate excel files to the specified directory. Friends in need can refer to it
Recently, the company needs to generate reports using PHP.
Header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=test_data.xls");
I searched on Baidu, and it seems that this can be implemented soon, but this file is generated where the browser downloads it,
I want to generate the generated files to the specified directory, can this be achieved?
Also, can you insert pictures into it?
PHPExcel is in English, but I didn’t understand it after reading it for a long time. Is there any example of direct generation?
We attach an example below:
?
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 43 44 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 the required class library, the same as java's Import * This function has caching function ----------------------------------------------- ---------- * @param string $class class library namespace string * @param string $baseUrl starting path * @param string $ext imported file 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 the current project application class library $baseUrl = dirname(LIB_PATH); $class = substr_replace($class, basename(LIB_PATH).'/', 0, strlen($class_strut[0]) 1); }elseif ('think' == strtolower($class_strut[0])){ // think official base class library $baseUrl = CORE_PATH; $class = substr($class,6); }elseif (in_array(strtolower($class_strut[0]), array('org', 'com'))) { // org third-party public class library com enterprise public class library $baseUrl = LIBRARY_PATH; }else { // Load other project application libraries $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, one record for each data * @param array $title The field name of each column of data, a unique array, which must be consistent with the order of the data (can be omitted) * @param string $filename excel name * @param array $field needs to specify the exported data field. The sorting must be consistent with the title, which is the array key value of the retrieved 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 specified fields are exported, export them in this order $dateNew=array(); foreach ($data as $k=>$v){ foreach ($field as $fkey){ $dateNew[$k][$fkey]=$v[$fkey]; } } $data=$dateNew; }
import("@.ORG.Util.ExcelXml");//Call to export excel class $xls = new ExcelXml('UTF-8', false, 'Sheet1'); $xls->addArray($data,$title); $xls->generateXML($filename); } |