Export excel data with php_PHP tutorial

WBOY
Release: 2016-07-15 13:21:57
Original
1061 people have browsed it

Provide two methods to export excel

1 The simplest way to export excel
header('Content-Type: application/vnd.ms-excel'); //Set the file type You can also change vnd.ms-excel' to xml (export xml file)
header('Content-Disposition: attachment;filename="Cloud platform user report.xls"'); //Set the name of the exported excel
header('Cache-Control: max-age=0');
echo iconv("utf-8", "gbk", "Time t error code t number of occurrences t error code meaning n"); // t is a tab character n is a newline character
foreach ($arr as $key=>$val){ //$arr is the data to be exported
echo iconv("utf-8","gbk",date("Y-m-d",$val["time"])."t".$val["error_code"]."t".$val[ "num"]."t".$val["code_mean"]."n");
}
2. Use phpexcel to export excel
Go to the Internet to download the phpexcel compressed package and put phpexcel.php and the phpexcel compressed package into your project
Example:
require_once ('PHPExcel.php'); //Reference these two files
require_once ('PHPExcel/Writer/Excel2007.php');
//Export excel
$objExcel = new PHPExcel();
$objProps = $objExcel->getProperties();
$objProps->setCreator("Zeal Li");
$objProps->setLastModifiedBy("Zeal Li");
$objProps->setTitle("Office XLS Test Document");
$objProps->setSubject("Office XLS Test Document, Demo");
$objProps->setDescription("Test document, generated by PHPExcel.");
$objProps->setKeywords("office excel PHPExcel");
$objProps->setCategory("Test");
$objProps = $objExcel->getProperties();
$objProps->setCreator("Zeal Li");
$objProps->setLastModifiedBy("Zeal Li");
$objProps->setTitle("Office XLS Test Document");
$objProps->setSubject("Office XLS Test Document, Demo");
$objProps->setDescription("Test document, generated by PHPExcel.");
$objProps->setKeywords("office excel PHPExcel");
$objProps->setCategory("Test");
//************************************
//Set the current sheet index for subsequent content operations.
//Generally, display calls are only needed when using multiple sheets.
//By default, PHPExcel will automatically create the first sheet and set SheetIndex=0
$objExcel->setActiveSheetIndex(0);
$objActSheet = $objExcel->getActiveSheet();
//Online games
//Set the name of the current active sheet
$objActSheet->setTitle('Online Games');
$objActSheet->setCellValue('A1','Game Name'); //The first row of data in the first sheet in the generated excel
$objActSheet->setCellValue('B1','Specific domain name');
$objActSheet->setCellValue('C1','Server Description');
$objActSheet->setCellValue('D1','Line Attribute');
$objActSheet->setCellValue('E1','Submitted server IP');
$objActSheet->setCellValue('F1','corresponding to optimized routing entry');
$objActSheet->setCellValue('G1','Keyword Optimization');
$objActSheet->setCellValue('H1','Optimize Line');
$objActSheet->setCellValue('I1','Optimization status');
foreach($result_webgame as $k1=>$v1){ //Traverse the data read from the database
$objActSheet->setCellValue('A'.($k1+2),$v1["gamename"]);
$objActSheet->setCellValue('B'.($k1+2),$v1['domain']);
$objActSheet->setCellValue('C'.($k1+2),$v1['gameserver']);
$objActSheet->setCellValue('D'.($k1+2),$v1['line_attribute']);
$objActSheet->setCellValue('E'.($k1+2),$v1['server_ip']);
$objActSheet->setCellValue('F'.($k1+2),$v1['optimize_route']);
$objActSheet->setCellValue('G'.($k1+2),$v1['keyword']);
$objActSheet->setCellValue('H'.($k1+2),$v1['line']);
$objActSheet->setCellValue('I'.($k1+2),$v1['optimize_status']);
}
//Website category
//Add a new worksheet
$objExcel->createSheet();
$reource_ready=$objExcel->getSheet(1)->setTitle('Website Class');
$reource_ready->setCellValue('A1','Website Name');
$reource_ready->setCellValue('B1','Specific domain name');
$resource_ready->setCellValue('C1','Line Attribute');
$resource_ready->setCellValue('D1','IP network segment');
$resource_ready->setCellValue('E1','Optimized line');
$resource_ready->setCellValue('F1','Optimization status');
foreach($result_website as $k1=>$v2){
$resource_ready->setCellValue('A'.($k1+2),$v2["name"]);
$reource_ready->setCellValue('B'.($k1+2),$v2['domain']);
$reource_ready->setCellValue('C'.($k1+2),$v2['line_attribute']);
$resource_ready->setCellValue('D'.($k1+2),$v2['ip']);
$resource_ready->setCellValue('E'.($k1+2),$v2['line']);
$reource_ready->setCellValue('F'.($k1+2),$v2['optimize_status']);
}
//By analogy, you can export excel containing multiple sheets

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477130.htmlTechArticle provides two methods to export excel 1. The simplest export excel header (Content-Type: application/vnd.ms- excel); //Setting the file type can also change vnd.ms-excel to xml (export xml file)...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!