解决ecshop加入PHPExcel导入变成乱码

更新时间:2014-12-10 点击量:742

 经过83次实践,发现代码的中间空了几行:就是ecshop生成代码结束 ?>  与新代码<?php 开始之间有空隙。汗,因为这个小问题折腾了优易软件近一个小时。后来在网上找到相似的解决方案,现一并贴出来。

在PHP代码的最前面加入:ob_start();在excel要生成的前面加上:ob_end_clean();这样就可以解决乱码的问题了。 <?php

define('IN_ECS', true);
 
require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');
 
 
/* 权限判断 */
admin_priv('xxx_order_stats');
 
 
$sql = 'select goods_id, goods_sn, goods_name, market_price, shop_price, promote_price,  is_on_sale from goods where is_delete=0 order by goods_id desc';
$data_list = $db->getAll($sql);
 
 
/*------------------------------------------------------ */
//-- 下载EXCEL报表
/*------------------------------------------------------ */
 /* 文件名 */
$filename = '商品表'.date("Y-m-d",time());
 
header("Content-type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=$filename.xls");
 
/* 文件标题 */
//echo ecs_iconv(EC_CHARSET, 'GB2312', $filename ) . "\t\n";
 
/* 头部信息 */
echo ecs_iconv(EC_CHARSET, 'GB2312', '商品ID') ."\t";
echo ecs_iconv(EC_CHARSET, 'GB2312', '商品名称') ."\t";
echo ecs_iconv(EC_CHARSET, 'GB2312', '货号') ."\t";
echo ecs_iconv(EC_CHARSET, 'GB2312', '市场价') ."\t";
echo ecs_iconv(EC_CHARSET, 'GB2312', '本店价') ."\t";
echo ecs_iconv(EC_CHARSET, 'GB2312', '促销价') ."\t";
echo ecs_iconv(EC_CHARSET, 'GB2312', '上/下架') ."\t";
 
echo "\n";
 
foreach ($data_list AS $data)
{
    echo ecs_iconv(EC_CHARSET, 'GB2312', $data['goods_id']) . "\t";
    echo ecs_iconv(EC_CHARSET, 'GB2312', $data['goods_name']) . "\t";
    echo ecs_iconv(EC_CHARSET, 'GB2312', strval($data['goods_sn'])) . "\t";
    echo ecs_iconv(EC_CHARSET, 'GB2312', $data['market_price']) . "\t";
    echo ecs_iconv(EC_CHARSET, 'GB2312', $data['shop_price']) . "\t";
    echo ecs_iconv(EC_CHARSET, 'GB2312', (intval($data['promote_price']) == 0 ?  '' : $data['promote_price'])) . "\t";
 
    echo ecs_iconv(EC_CHARSET, 'GB2312', ($data['is_on_sale'] == 1 ? '上架' : '')) . "\t";
    echo "\n";
}
//以上这段代码生成Excel正常,不会出现问题,但在打开时会提示不兼容的问题。对于那些领导或者外行人士用,会感觉这个文档有问题,于是优易软件找到PHPExcel来正常生成excel。
?>

 
<?php
header('Content-type:text/html;charset=utf-8');
require_once ROOT_PATH . '/Classes/PHPExcel.php';
require_once ROOT_PATH . '/Classes/PHPExcel/Writer/Excel2007.php';
require_once ROOT_PATH . '/Classes/PHPExcel/Writer/Excel5.php';
include_once ROOT_PATH . '/Classes/PHPExcel/IOFactory.php';
 
$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");
//$objExcel->setActiveSheetIndex(0);
 
$i=0;
//表头
$k1="商品ID";
$k2="商品名称";
$k3="货号";
$k4="市场价";
$k5="本店价";
$k6="促销价";
$k7="上/下架";
 
$objExcel->getActiveSheet()->setCellValue('a1', "$k1")->getStyle('a1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
$objExcel->getActiveSheet()->setCellValue('b1', "$k2")->getStyle('b1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
$objExcel->getActiveSheet()->setCellValue('c1', "$k3")->getStyle('c1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
$objExcel->getActiveSheet()->setCellValue('d1', "$k4")->getStyle('d1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
$objExcel->getActiveSheet()->setCellValue('e1', "$k5")->getStyle('e1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
$objExcel->getActiveSheet()->setCellValue('f1', "$k6")->getStyle('f1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
$objExcel->getActiveSheet()->setCellValue('g1', "$k7")->getStyle('g1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
 
foreach($data_list as $k=>$v) {
    $u1=$i+2;
    /*----------写入内容-------------*/
    $objExcel->getActiveSheet()->setCellValue('a'.$u1, $v["goods_id"]);
    $objExcel->getActiveSheet()->setCellValue('b'.$u1, $v["goods_name"]);
    $objExcel->getActiveSheet()->setCellValue('c'.$u1, "'".strval($v['goods_sn']));
    $objExcel->getActiveSheet()->setCellValue('d'.$u1, $v["market_price"]);
    $objExcel->getActiveSheet()->setCellValue('e'.$u1, $v["shop_price"]);
    $objExcel->getActiveSheet()->setCellValue('f'.$u1, (intval($v['promote_price']) == 0 ?  '' : $v['promote_price']));
    $objExcel->getActiveSheet()->setCellValue('g'.$u1, ($v['is_on_sale'] == 1 ? '上架' : ''));
    $i++;
}
 
// 高置列的宽度
$objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(80);
$objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
$objExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
$objExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
$objExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
 
$objExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BPersonal cash register&RPrinted on &D');
$objExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objExcel->getProperties()->getTitle() . '&RPage &P of &N');
 
// 设置页方向和规模
$objExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
$objExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$objExcel->setActiveSheetIndex(0);
ob_end_clean();
$ex = 'xxx';
if($ex == '2007') { //导出excel2007文档
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
    $objWriter->save('php://output');
    exit;
} else {  //导出excel2003文档
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
    $objWriter->save('php://output');
    exit;
}
?>
注释掉ecshop的生成代码后,PHPExcel生成的excel却变成了乱码,重新把PHPExcel单独拿出来又正常。
支付宝扫码打赏 微信打赏

如果文章对您有帮助,就打赏一个吧

在线客服

客户服务

热线电话:

0755-36656507 15889489919 服务时间:

周一到周六:9:00-18:00

在线QQ客服

在线微信客服

关于我们 常见问题

支付方式 加盟合作

提交需求
优惠红包 购物车0 反馈留言 返回顶部