Export an excel table from a new PHP excel database

Source: Internet
Author: User
Tags php excel

First, download the latest class files from the official phpExcel website. The following is a small example of Chinese garbled characters.

/** PHPExcel */require_once '.. /Classes/PHPExcel. php '; // Create new PHPExcel object $ objPHPExcel = new PHPExcel (); // connect to the database to obtain data $ conn = mysql_connect ('localhost', 'root ',''); if (! $ Conn) die ('database connection failed'); mysql_select_db ('test'); mysql_set_charset ('utf8'); $ SQL = "SELECT * FROM orders "; $ query = mysql_query ($ SQL); $ objPHPExcel-> setActiveSheetIndex (0)-> setCellValue ('a1', mb_convert_encoding ('sequence number ', 'utf-8 ', 'gbk')-> setCellValue ('b1 ', mb_convert_encoding ('real name', 'utf-8', 'gbk')-> setCellValue ('c1 ', mb_convert_encoding ('money', 'utf-8', 'gbk'); $ num = 2; while ($ row = mysql_fetch_array ($ query )) {$ objPHPExcel-> setActiveSheetIndex (0)-> setCellValue ('A '. $ num, $ row ['id'])-> setCellValue ('B '. $ num, $ row ['realname'])-> setCellValue ('C '. $ num, $ row ['momeny']); $ num ++;} // Rename sheet $ objPHPExcel-> getActiveSheet ()-> setTitle ('simple '); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $ objPHPExcel-> setActiveSheetIndex (0 ); // Redirect output to a client's web browser (Excel5) header ('content-Type: application/vnd. ms-excel '); header ('content-Disposition: attachment; filename = "01simple.xls"'); header ('cache-Control: max-age = 0 '); $ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5'); $ objWriter-> save ('php: // output'); exit;

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.