php导入到excel乱码是因为utf8编码在xp系统不支持所有utf8编码转码一下就完美解决了
utf-8编码案例
Php代码
<?php <BR>header("Content-Type: application/vnd.ms-excel; charset=UTF-8"); <BR>header("Pragma: public"); <BR>header("Expires: 0"); <BR>header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); <BR>header("<a>@本文9来源gao($daima.com搞@代@#码8网^</a><strong>搞代gaodaima码</strong>Content-Type: application/force-download"); <BR>header("Content-Type: application/octet-stream"); <BR>header("Content-Type: application/download"); <BR>header("Content-Disposition: attachment;filename=11.xls "); <BR>header("Content-Transfer-Encoding: binary "); <BR>?> <BR>
Php代码
<? <BR>$filename="php导入到excel-utf-8编码"; <BR>$filename=iconv("utf-8", "gb2312", $filename); <BR>echo $filename; <BR>?> <BR>
gbk编码案例
Php代码
<?php <BR>header("Content-Type: application/vnd.ms-excel; charset=UTF-8"); <BR>header("Pragma: public"); <BR>header("Expires: 0"); <BR>header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); <BR>header("Content-Type: application/force-download"); <BR>header("Content-Type: application/octet-stream"); <BR>header("Content-Type: application/download"); <BR>header("Content-Disposition: attachment;filename=11.xls "); <BR>header("Content-Transfer-Encoding: binary "); <BR>?> <BR>
Php代码
0.<? <BR>0.$filename="php导入到excel-utf-8编码"; <BR>0.echo $filename; <BR>0.?> <BR>
访问网站的时候就下载到excel里面
要弄单元格区别的话
用table表格做网页的就可以了
====================== 其他方法 =============================
1、制作简单 Excel
0.<?php <BR>0.header("Content-type:application/vnd.ms-excel"); <BR>0.header("Content-Disposition:filename=php2excel.xls"); <BR>0. <BR>0.echo "A1/t B1/t C1/n"; <BR>0.echo "A2/t B2/t C2/n"; <BR>0.echo "A3/t B3/t C3/n"; <BR>0.echo "A4/t B4/t C4/n"; <BR>0.?> <BR>
2、制作简单 CSV
<?php<BR>$action =$_GET['action'];<BR>if ($action=='make'){<BR> $fp = fopen("demo_csv.csv","a"); //打开csv文件,如果不存在则创建<BR> $title = array("First_Name","Last_Name","Contact_Email","Telephone"); //第一行数据<BR> $data_1 = array("42343","423432","4234","4234"); <BR> $data_2 = array("4234","Last_Name","Contact_Email","Telephone"); <BR> $title = implode(",",$title); //用 ' 分割成字符串<BR> $data_1 = implode(",",$data_1); // 用 ' 分割成字符串<BR> $data_2 = implode(",",$data_2); // 用 ' 分割成字符串<BR> $data_str =$title."/r/n".$data_1."/r/n".$data_2."/r/n"; //加入换行符<BR> fwrite($fp,$data_str); // 写入数据<BR> fclose($fp); //关闭文件句柄<BR> echo "生成成功";<BR>}<BR>echo "<br>";<BR>echo "生成csv文件";<BR>?><BR>
也可以做一个封闭函数:
封闭函数一:
function exportToCsv($csv_data, $filename = 'export.csv') {<BR> $csv_terminated = "/n";<BR> $csv_separator = ",";<BR> $csv_enclosed = '"';<BR> $csv_escaped = "//";<BR> // Gets the data from the database<BR> $schema_insert = '';<BR> $out = '';<BR> // Format the data<BR> foreach ($csv_data as $row)<BR> {<BR> $schema_insert = '';<BR> $fields_cnt = count($row);<BR> //printr($row);<BR> $tmp_str = '';<BR> foreach($row as $v)<BR> {<BR> $tmp_str .= $csv_enclosed.str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $v).$csv_enclosed.$csv_separator;<BR> } // end for<br><br> $tmp_str = substr($tmp_str, 0, -1);<BR> $schema_insert .= $tmp_str;<BR> $out .= $schema_insert;<BR> $out .= $csv_terminated;<BR> } // end while<BR> header("Cache-Control: must-revalidate, post-check=0, pre-check=0");<BR> header("Content-Length: " . strlen($out));<BR> header("Content-type: text/x-csv");<BR> header("Content-Disposition:filename=$filename");<BR> echo $out;<BR>}<BR>/*<BR>$csv_data = array(array('Name', 'Address'));<BR>array_push($csv_data, array($row['name'],$row['address']));<BR>...<BR>exportToCsv($csv_data,'new_file.csv');<BR>*/<BR>
封闭函数二:
<?<BR>/**<BR> * Simple class to properly output CSV data to clients. PHP 5 has a built<BR> * in method to do the same for writing to files (fputcsv()), but many times<BR> * going right to the client is beneficial.<BR> *<BR> * @author Jon Gales<BR> */<BR>class CSV_Writer {<BR> public $data = array();<BR> public $deliminator;<BR> /**<BR> * Loads data and optionally a deliminator. Data is assumed to be an array<BR> * of associative arrays.<BR> *<BR> * @param array $data<BR> * @param string $deliminator<BR> */<BR> function __construct($data, $deliminator = ",")<BR> {<BR> if (!is_array($data))<BR> {<BR> throw new Exception('CSV_Writer only accepts data as arrays');<BR> }<BR> $this->data = $data;<BR> $this->deliminator = $deliminator;<BR> }<BR> private function wrap_with_quotes($data)<BR> {<BR> $data = preg_replace('/"(.+)"/', '""$1""', $data);<BR> return sprintf('"%s"', $data);<BR> }<BR> /**<BR> * Echos the escaped CSV file with chosen delimeter<BR> *<BR> * @return void<BR> */<BR> public function output()<BR> {<BR> foreach ($this->data as $row)<BR> {<BR> $quoted_data = array_map(array('CSV_Writer', 'wrap_with_quotes'), $row);<BR> echo sprintf("%s/n", implode($this->deliminator, $quoted_data));<BR> }<BR> }<BR> /**<BR> * Sets proper Content-Type header and attachment for the CSV outpu<BR> *<BR> * @param string $name<BR> * @return void<BR> */<BR> public function headers($name)<BR> {<BR> header('Content-Type: application/csv');<BR> header("Content-disposition: attachment; filename={$name}.csv");<BR> }<BR>}<BR>/*<BR>//$data = array(array("one","two","three"), array(4,5,6));<BR>$data[] = array("one","two","three");<BR>$data[] = array(4,5,6);<BR>$csv = new CSV_Writer($data);<BR>$csv->headers('test');<BR>$csv->output();<BR>*/<BR>
3. 使用excel类
<?php<BR>require_once 'Spreadsheet/Writer.php';<BR>$workbook = new Spreadsheet_Excel_Writer();<BR>/* 生成 CSV<BR>$filename = date('YmdHis').'.csv';<BR>$workbook->send($filename); // 发送 Excel 文件名供下载<BR>*/<BR>// 生成 Excel<BR>$filename = date('YmdHis').'.xls';<BR>$workbook->send($filename); // 发送 Excel 文件名供下载<BR>$workbook->setVersion(8);<BR>$workbook->setBIFF8InputEncoding('UTF-8');<BR>$worksheet =& $workbook->addWorksheet("Sheet-1");<BR>$data[]= array('id','username','company','email','mob','daytime','intent');<BR>$data[] = array(1,'老梁','**工作室','jb51.net','1363137966*',time(),'y');<BR>$total_row = count($data);<BR>$total_col = count($data[0]);<BR>for ($row = 0; $row < $total_row; $row ++) {<BR> for ($col = 0; $col < $total_col; $col ++) {<BR> $worksheet->writeString($row, $col, $data[$row][$col]); // 在 sheet-1 中写入数据<BR> }<BR>}<BR>/*<BR>$worksheet =& $workbook->addWorksheet("Sheet-2");<BR>$data[]= array('id','username','company','email','mob','daytime','intent');<BR>$data[] = array(1,'老梁','**工作室','jb51.net','1363137966*',time(),'y');<BR>$total_row = count($data);<BR>$total_col = count($data[0]);<BR>for ($row = 0; $row < $total_row; $row ++) {<BR> for ($col = 0; $col < $total_col; $col ++) {<BR> $worksheet->writeString($row, $col, $data[$row][$col]); // 在 sheet-2 中写入数据<BR> }<BR>}<BR>*/<BR>$workbook->close(); // 完成下载<BR>?><BR>
类二
—–函数说明
读取Excel文件
function Read_Excel_File($ExcelFile,$Result)
$ExcelFile Excel文件名
$Result 返回的结果
函数返回值 正常返回0,否则返回错误信息
返回的值数组
$result[sheet名][行][列] 的值为相应Excel Cell的值
建立Excel文件
function Create_Excel_File($ExcelFile,$Data)
$ExcelFile Excel文件名
$Data Excel表格数据
请把函数写在PHP脚本的开头
例1:
<?<BR>require "excel_class.php";<BR>Read_Excel_File("Book1.xls",$return);<BR>for ($i=0;$i<count($return[Sheet1]);$i++)<BR>{<BR> for ($j=0;$j<count($return[Sheet1][$i]);$j++)<BR> {<BR> echo $return[Sheet1][$i][$j]."|";<BR> }<BR> echo "<br>";<BR>}<BR>?><BR>
例2:
<?<BR>require "excel_class.php";<BR>Read_Excel_File("Book1.xls",$return);<BR>Create_Excel_File("ddd.xls",$return[Sheet1]);<BR>?><BR>