Phpexcel Generating Excel files and exporting Excel files

Source: Internet
Author: User

<?phpheader ("Content-type:text/html;charset=utf-8"); Error_reporting (E_all & ~e_notice); require_once (esf_dir. '/zjadmin/phpexcel/classes/phpexcel.php '); $db Query ("Set names UTF8"), $Info _area=array (2=> "x Mountain",1=> "x-river",3=> "x-Yang",4=> "X-zone"); $m = Isset ($_get[' m '])? $ _get[' m ']:0;if ($m = = 1) {$reader = Phpexcel_iofactory::createreader (' Excel5 ');//Read the Excel file way this method is read excel2007 before the version exce L2007 to read the version after 2007 can also check the \classes\phpexcel\reader folder of the class (for all read classes, which need to fill in which one of the line) $PHPExcel = $reader->load ("Hf_lp.xls"); File name $sheet = $PHPExcel->getsheet (0); Read the first sheet read from 0 $highestrow = $sheet->gethighestrow (); Total number of rows $highestcolumn = $sheet->gethighestcolumn (); Total number of columns obtained $arr = array (1=> ' A ',2=> ' B ',3=> ' C '); for ($row = 2; $row <= $highestRow; $row + +) {for ($column = 0; $column <count ($arr); $column + +) {$val = $        Sheet->getcellbycolumnandrow ($column, $row)->getvalue ();    $list [$row] = $val;     } $str = '; foreach ($list [$row] as $key = $Value) {if ($key = = = 0) $str. = '. Array_search ($value, $Info _area). ', '; else $str. = ' \ '. $value. '     \‘,‘;     } $field = RTrim ($str, ', ');     Echo $field;     mysql_query ("INSERT into temp_table (area,name,address) VALUES (". $field. ")");     echo " 
Include_once (esf_dir. '/zjadmin/phpexcel/classes/phpexcel/writer/excel2007.php '); include_once (ESF_DIR. '/zjadmin /phpexcel/classes/phpexcel/writer/excel5.php '); include_once (Esf_dir. '/zjadmin/phpexcel/classes/phpexcel/ Iofactory.php '); $fileName = "Test_excel"; $headArr = Array ("area", "name", "Address"), Getexcel ($fileName, $HEADARR, $im);}    function Getexcel ($fileName, $HEADARR, $data) {if (empty ($data) | |!is_array ($data)) {die ("data must is a array");    } if (empty ($fileName)) {exit;    } $date = Date ("Y_m_d", Time ());     $fileName. = "_{$date}.xls";    Create a new Phpexcel object $objPHPExcel = new Phpexcel ();         $objProps = $objPHPExcel->getproperties ();    Set table Header $key = Ord ("A");        foreach ($headArr as $v) {$colum = Chr ($key); $objPHPExcel->setactivesheetindex (0)->setcellvalue ($colum. '        1 ', $v);    $key + = 1;    } $column = 2;    $objActSheet = $objPHPExcel->getactivesheet (); foreach ($data as $key = + $rows) {//Line write $span = Ord ("A");            foreach ($rows as $keyName = + $value) {//column write $j = Chr ($span);            $objActSheet->setcellvalue ($j. $column, $value);        $span + +;    } $column + +;    } $fileName = Iconv ("Utf-8", "gb2312", $fileName);    $objPHPExcel->getactivesheet ()->settitle (' simple ');       $objPHPExcel->setactivesheetindex (0);       Header (' Content-type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet '); Header ("content-disposition:attachment;       Filename=\ "$fileName \" ");       Header (' cache-control:max-age=0 ');       $objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 '); $objWriter->save (' php://output '); File download exit via browser;}

Export Source: http://www.oschina.net/code/snippet_112754_15765



Phpexcel Generating Excel files and exporting Excel files

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.