This article mainly introduces two methods of exporting excel files natively in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
First method:
$filename='文件名称'; $filetitle='你的标题'; if($_POST){ set_time_limit(10000); $title = ''; ini_set('memory_limit','300M'); header('Content-Type: application/vnd.ms-excel;charset=utf-8'); $name = $title.".xls"; header('Content-Disposition: attachment;filename='.$name.''); header('Cache-Control: max-age=0'); $where = "1=1"; $sql = ""; $query = DB::Query($sql); // PHP文件句柄,php://output 表示直接输出到浏览器 $fp = fopen('php://output', 'a'); // 输出Excel列头信息 $head = array('ID'); //字符替换 $p_new_lines = array("\r\n", "\n","\t","\r","\r\n", "","","
","
","
"); $p_change_line_in_excel_cell = ''; foreach($head as $v){ echo iconv('utf-8','gb2312',$v) . "\t"; } echo "\n"; // 计数器 $cnt = 0; // 每隔$limit行,刷新一下输出buffer,节约资源 $limit = 100000; // 逐行取出数据,节约内存 while ($res = mysql_fetch_assoc($query)) { $cnt ++; if ($limit == $cnt) { //刷新一下输出buffer,防止由于数据过多造成问题 ob_flush(); flush(); $cnt = 0; } echo trim($res['id']). "\t"; echo "\n"; } }Copy after login
Second Method:
$filename='文件名称'; $filetitle='你的标题'; if($_POST){ $title = ''; ini_set('memory_limit','300M'); header('Content-Type: application/vnd.ms-excel;charset=utf-8'); $name = $title.".xls"; header('Content-Disposition: attachment;filename='.$name.''); header('Cache-Control: max-age=0'); echo ' '; $where = "1=1"; $sql = " "; mysql_query('set names "utf8"'); mysql_set_charset('utf8'); $query = DB::Query($sql); // PHP文件句柄,php://output 表示直接输出到浏览器 $fp = fopen('php://output', 'a'); // 输出Excel列头信息 $head = array('ID','xxx'); //字符替换 $p_new_lines = array("\r\n", "\n","\t","\r","\r\n", "","","
","
","
"); $p_change_line_in_excel_cell = ''; echo "
".iconv('utf-8','gb2312',$v)." | "; } echo "|
".$res['id']." | "; echo "".iconv('utf-8', 'gb2312', $res['xxx']." | "; echo"
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
phpMethod for deleting specified elements from an array
Exception and error parsing in
php
##php
Detailed explanation of the method of sorting arrays in pinyin order
######
The above is the detailed content of Detailed explanation of two methods of exporting excel files natively in php. For more information, please follow other related articles on the PHP Chinese website!