php导出excel时中文名称的解决办法
你先看一下这php生成excel类文件。完全可以解决
<?php
// 数据导出 类文件
/*** 导出 XML格式的 Excel 数据* 作者: 色色*/
class XmlExcelExport{/** * 文档头标签 * * @var string */
private $header = "<?xml version="1.0" encoding="%s"?>n<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">";
/** * 文档尾标签 * * @var string */
private $footer = "</Workbook>";
/** * 内容编码 * @var string */
private $sEncoding;
/** * 是否转换特定字段值的类型 * * @var boolean */
private $bConvertTypes;
/** * 生成的Excel内工作簿的个数 * * @var int */
private $dWorksheetCount = 0;
/**
* 构造函数 *
* 使用类型转换时要确保:页码和邮编号以'0'开头 *
* @param string $sEncoding 内容编码
* @param boolean $bConvertTypes 是否转换特定字段值的类型
*/
function __construct($sEncoding = 'UTF-8', $bConvertTypes = false){
$this->bConvertTypes = $bConvertTypes;
$this->sEncoding = $sEncoding;
}
/**
* 返回工作簿标题,最大 字符数为 31 *
* @param string $title 工作簿标题
* @return string
*/
function getWorksheetTitle($title = 'Table1'){
$title = preg_replace("/[\|:|/|?|*|[|]]/", "", empty($title) ? 'Table' . ($this->dWorksheetCount + 1) : $title);
return substr($title, 0, 31);
}
/**
* 向客户端发送Excel头信息 *
* @param string $filename 文件名称,不能是中文
*/
function generateXMLHeader($filename){
$filename = preg_replace('/[^aA-zZ0-9_-]/', '', $filename);
$filename = urlencode($filename);
// 中文名称使用urlencode编码后在IE中打开能保存成中文名称的文件,但是在FF上却是乱码
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/vnd.ms-excel; charset={$this->sEncoding}");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename={$filename}.xls");
echo stripslashes(sprintf($this->header, $this->sEncoding));
}
/**
* 向客户端发送Excel结束标签 *
* @param string $filename 文件名称,不能是中文
*/
function generateXMLFoot(){
echo $this->footer;
}
/**
* 开启工作簿 *
* @param string $title
*/
function worksheetStart($title){
$this->dWorksheetCount ++;
echo "n<Worksheet ss:Name="" . $this->getWorksheetTitle($title) . "">n<Table>n";
}
/**
* 结束工作簿
*/
function worksheetEnd(){
echo "</Table>n</Worksheet>n";
}
/**
* 设置表头信息 *
* @param array $header
*/
function setTableHeader(array $header){
echo $this->_parseRow($header);
}
/**
* 设置表内行记录数据 *
* @param array $rows 多行记录
*/
function setTableRows(array $rows){
foreach ($rows as $row)
echo $this->_parseRow($row);
}
/**
* 将传人的单行记录数组转换成 xml 标签形式 *
* @param array $array 单行记录数组
*/
private function _parseRow(array $row){
$cells = "";
foreach ($row as $k => $v){
$type = 'String';
if ($this->bConvertTypes === true && is_numeric($v))
$type = 'Number';
$v = htmlentities($v, ENT_COMPAT, $this->sEncoding);
$cells .= "<Cell><Data ss:Type="$type">" . $v . "</Data></Cell>n";
}
return "<Row>n" . $cells . "</Row>n";
}
}
//此处做一个更新,解决 发出的文件名称中文乱码的问题... 自己按照修改吧
function sendFile($serverPath, $filename,$charset = 'UTF-8', $mimeType = 'application/octet-stream'){
// 文件名乱码问题
if (preg_match("/MSIE/", $_SERVER["HTTP_USER_AGENT"])) {
$filename = urlencode($filename);
$filename = str_replace("+", "%20", $filename);// 替换空格
$attachmentHeader = "Content-Disposition: attachment; filename="{$filename}";
charset={$charset}";
} else if (preg_match("/Firefox/", $_SERVER["HTTP_USER_AGENT"])) {
$attachmentHeader = 'Content-Disposition: attachment; filename*="utf8''' . $filename. '"' ;
} else {
$attachmentHeader = "Content-Disposition: attachment; filename="{$filename}"; charset={$charset}";
}
$filesize = filesize($serverPath);
//header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: {$mimeType}");
header($attachmentHeader);
header('Pragma: cache');
header('Cache-Control: public, must-revalidate, max-age=0');
header("Content-Length: {$filesize}");
readfile($serverPath);
exit;
}
}