phpexcel导出excel,保存本地乱码,而在服务端不是乱码问题解决方案

遇到一个奇怪的问题,phpexcel导出excel,保存本地乱码,而在服务端不是乱码问题。
 http://127.0.0.1:30983/index.php?m=report  这个是apache下的路径,可以正常保存打开excel,
 http://localhost:8080/project/index.php?m=report  这个是本地路径,不可以正常打开excel,
 其实都是在我本机上,费了一番周折,解决方案如下:

header("Content-type: text/html;charset=utf-8");
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PhpExcel/Writer/Excel2007.php';
require_once 'Classes/PhpExcel/Writer/Excel5.php';
include_once 'Classes/PhpExcel/IOFactory.php'; 
//创建一个处理对象实例(此对象对于2003 2007是相同的)
$objExcel = new PHPExcel(); 
//设置属性 (这段代码无关紧要,其中的内容可以替换为你需要的)
$objExcel->getProperties()->setCreator("andy");
$objExcel->getProperties()->setLastModifiedBy("andy");
$objExcel->getProperties()->setTitle("Office 2003 XLS Test Document");
$objExcel->getProperties()->setSubject("Office 2003 XLS Test Document");
$objExcel->getProperties()->setDescription("Test document for Office 2003 XLS, generated using PHP classes.");
$objExcel->getProperties()->setKeywords("office 2003 openxml php");
$objExcel->getProperties()->setCategory("Test result file");
$userid=$_POST["user_id"];
$startdate=$_POST["log_start_date"];
$enddate=$_POST["log_end_date"];
$dbuser="root";
$dbpwd="123456";
$dbhost="localhost";
$dbdatabase="project";
$db=mysql_connect($dbhost,$dbuser,$dbpwd);
//mysql_query("set names utf8'");//这就是指定数据库字符集,一般放在连接数据库后面就行了(非常重要)
mysql_select_db($dbdatabase,$db);
function  createHeader($objExcel)
{
 //表头
 $k1="日期";
 $k2="项目";
 $k3="工作性质";
 $k4="任务内容";
 $k5="用时数(小时)";
 $k6="姓名";
 /*-----------转码-----------*/
 $k1=iconv("gb2312","utf-8",$k1);
 $k2=iconv("gb2312","utf-8",$k2);
 $k3=iconv("gb2312","utf-8",$k3);
 $k4=iconv("gb2312","utf-8",$k4);
 $k5=iconv("gb2312","utf-8",$k5);
 $k6=iconv("gb2312","utf-8",$k6);
 /*---------------------栏目名称-----------------------*/
 $objExcel->getActiveSheet()->setCellValue('a1', "$k1");
 $objExcel->getActiveSheet()->setCellValue('b1', "$k2");
 $objExcel->getActiveSheet()->setCellValue('c1', "$k3");
 $objExcel->getActiveSheet()->setCellValue('d1', "$k4");
 $objExcel->getActiveSheet()->setCellValue('e1', "$k5");
 $objExcel->getActiveSheet()->setCellValue('f1', "$k6");
}
if($_POST['submitone'])
{
 $mysql="select concat(c.contact_first_name,c.contact_last_name) as fullname,
   date(log.task_log_date)as logdate,p.project_name,
   t.work_property_id,t.task_name,sum(log.task_log_hours) as hours
   from task_log as log
   join tasks    as t
   on  log.task_log_task=t.task_id
   join projects as  p
   on p.project_id=t.task_project
   join  user_tasks  as ut
   on t.task_id=ut.task_id
   join contacts as c
   on c.contact_id=ut.user_id
   where ut.user_id=".$userid."
   and date_format(task_log_date,'%Y%m%d') between '".$startdate."' and '".$enddate.
  "' group by t.task_name,ut.user_id,date_format(task_log_date,'%Y%m%d'),p.project_name order by logdate";
 $myresult=mysql_query($mysql,$db);
 $i=0;
 createHeader($objExcel);
 $sheetname;
 $flag=false;
 while($arr=mysql_fetch_array($myresult))
 {
  $flag=true;
  /*----------从数据库读取数据--------------------*/
  $logdate=$arr["logdate"];
  $projectname=$arr["project_name"];
  //1:需求  2:设计 3:编码  4:代码走查   5测试用例   6系统测试  7维护(these  code  to  avoid generating messy code)
  $message="未填写";
  switch ($arr["work_property_id"])
  {
   case 1:
    $message="需求";
    break;
   
   case 2:
    $message="设计";
    break;
   case 3:
    $message="编码";
    break;
   case 4:
    $message="代码走查";
    break;
   case 5:
    $message="测试用例";
    break;
   case 6:
    $message="系统测试";
    break;
   case 7:
    $message="维护";
    break;
   default:
    $message;
    break;
   
  }
  $workproperty=$message;
  $taskname=$arr["task_name"];
  $hours=$arr["hours"];
  $name=$arr["fullname"];
  $sheetname=$arr["fullname"];
  /*-----------转码-----------*/
  $workproperty=iconv("gb2312","utf-8", $workproperty);
  
  /*$logdate=iconv("gb2312","utf-8",$logdate);
  $projectname=iconv("gb2312","utf-8",$projectname);
  $workproperty=iconv("gb2312","utf-8", $workproperty);
  $taskname=iconv("gb2312","utf-8",$taskname);
  $hours=iconv("gb2312","utf-8",$hours);*/
  
  $u1=$i+2;
  
  /*----------写入内容-------------*/
  $objExcel->getActiveSheet()->setCellValue('a'.$u1, "$logdate");
  $objExcel->getActiveSheet()->setCellValue('b'.$u1, "$projectname");
  $objExcel->getActiveSheet()->setCellValue('c'.$u1, "$workproperty");
  $objExcel->getActiveSheet()->setCellValue('d'.$u1, "$taskname");
  $objExcel->getActiveSheet()->setCellValue('e'.$u1, "$hours");
  $objExcel->getActiveSheet()->setCellValue('f'.$u1, "$name");
  $i++;
 }
 if($flag)
 {
  // 高置列的宽度
  //echo date('H:i:s') . " Set column widths\n";
  $objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  $objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(12);
  
  // 添加条件格式 设置字体echo date('H:i:s') . " Set fonts\n";
  $objExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
  $objExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
  $objExcel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
  $objExcel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
  $objExcel->getActiveSheet()->getStyle('E1')->getFont()->setBold(true);
  $objExcel->getActiveSheet()->getStyle('F1')->getFont()->setBold(true);
  // 设置页眉和页脚。如果没有不同的标题奇数/即使是使用单头假定.echo date('H:i:s') . " Set header/footer\n";
  $objExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BPersonal cash register&RPrinted on &D');
  $objExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objExcel->getProperties()->getTitle() . '&RPage &P of &N');
  
  // 设置页方向和规模
  //echo date('H:i:s') . " Set page orientation and size\n";
  $objExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
  $objExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
  
  // 重命名表
  //echo date('H:i:s') . " Rename sheet\n";
  $objExcel->getActiveSheet()->setTitle($sheetname);
  
  // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  $objExcel->setActiveSheetIndex(0);
  //输出内容 (保存到一个默认的路径,用户无法选择路径)
  //$objWriter->save(str_replace('.php', '.xls', __FILE__));
  // or  phpexcel 保存时可以选择路径
  //保存为excel2007格式
  //$sheetname=iconv("utf-8","gb2312", $sheetname);
  $sheetname=iconv("utf-8", "gb2312//IGNORE", $sheetname);
  $filename=date("Y-m-d-H-i-s").'-'.$sheetname.'.xls';

 

 

//下面注释掉的代码,会产生上述问题
  //header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  //header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8');
 /* Header("Content-type: application/x-msdownload");
  //header('Content-Disposition: attachment;filename="01simple.xls"');
  header('Content-Disposition: attachment;filename="'.$filename.'"');
  header('Cache-Control: max-age=0');
  $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
  $objWriter->save('php://output');
  exit;*/

 

 

//解决方法
  header("Content-type: text/csv");//重要
  header('Content-Disposition: attachment;filename="'.$filename.'"');
  header('Cache-Control: must-revalidate, post-check=0,pre-check=0');
  header('Expires:0');
  header('Pragma:public');
  $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
  $objWriter->save('php://output');
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值