欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

完美解决phpexcel导出到xls文件出现乱码的问题

程序员文章站 2024-03-02 23:43:40
解决方法如下所示:

解决方法如下所示:

<?php
include 'global.php';
$ids = $_get['ids'];

$sql = "select * from crm_cost_end where id in ( {$ids} )";
$result = $db->findall($sql);
//echo $result[1]['sn'];

//创建一个excel对象
$objphpexcel = new phpexcel();
// set properties 

$objphpexcel->getproperties()->setcreator("ctos")
    ->setlastmodifiedby("ctos")
    ->settitle("office 2007 xlsx test document")
    ->setsubject("office 2007 xlsx test document")
    ->setdescription("test document for office 2007 xlsx, generated using php classes.")
    ->setkeywords("office 2007 openxml php")
    ->setcategory("test result file");

//set width 
$objphpexcel->getactivesheet()->getcolumndimension('a')->setwidth(8);
$objphpexcel->getactivesheet()->getcolumndimension('b')->setwidth(10);
$objphpexcel->getactivesheet()->getcolumndimension('c')->setwidth(25);
$objphpexcel->getactivesheet()->getcolumndimension('d')->setwidth(12);
$objphpexcel->getactivesheet()->getcolumndimension('e')->setwidth(50);
$objphpexcel->getactivesheet()->getcolumndimension('f')->setwidth(10);
$objphpexcel->getactivesheet()->getcolumndimension('g')->setwidth(12);
$objphpexcel->getactivesheet()->getcolumndimension('h')->setwidth(12);
$objphpexcel->getactivesheet()->getcolumndimension('i')->setwidth(12);
$objphpexcel->getactivesheet()->getcolumndimension('j')->setwidth(30);

//设置行高度 
$objphpexcel->getactivesheet()->getrowdimension('1')->setrowheight(22);

$objphpexcel->getactivesheet()->getrowdimension('2')->setrowheight(20);

//set font size bold 
$objphpexcel->getactivesheet()->getdefaultstyle()->getfont()->setsize(10);
$objphpexcel->getactivesheet()->getstyle('a2:j2')->getfont()->setbold(true);

$objphpexcel->getactivesheet()->getstyle('a2:j2')->getalignment()->setvertical(phpexcel_style_alignment::vertical_center);
$objphpexcel->getactivesheet()->getstyle('a2:j2')->getborders()->getallborders()->setborderstyle(phpexcel_style_border::border_thin);

//设置水平居中 
$objphpexcel->getactivesheet()->getstyle('a1')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_left);
$objphpexcel->getactivesheet()->getstyle('a')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center);
$objphpexcel->getactivesheet()->getstyle('b')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center);
$objphpexcel->getactivesheet()->getstyle('d')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center);
$objphpexcel->getactivesheet()->getstyle('f')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center);
$objphpexcel->getactivesheet()->getstyle('g')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center);
$objphpexcel->getactivesheet()->getstyle('h')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center);
$objphpexcel->getactivesheet()->getstyle('i')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center);

// 
$objphpexcel->getactivesheet()->mergecells('a1:j1');

// set table header content 
$objphpexcel->setactivesheetindex(0)
    ->setcellvalue('a1', '订单数据汇总 时间:' . date('y-m-d h:i:s'))
    ->setcellvalue('a2', '订单id')
    ->setcellvalue('b2', '下单人')
    ->setcellvalue('c2', '客户名称')
    ->setcellvalue('d2', '下单时间')
    ->setcellvalue('e2', '需求机型')
    ->setcellvalue('f2', '需求数量')
    ->setcellvalue('g2', '需求交期')
    ->setcellvalue('h2', '确认bom料号')
    ->setcellvalue('i2', 'pmc确认交期')
    ->setcellvalue('j2', 'pmc交货备注');

// miscellaneous glyphs, utf-8 

for ($i = 0; $i < count($result) - 1; $i++) {
  $objphpexcel->getactivesheet(0)->setcellvalue('a' . ($i + 3), $result[$i]['id']);
  $objphpexcel->getactivesheet(0)->setcellvalue('b' . ($i + 3), $result[$i]['realname']);
  $objphpexcel->getactivesheet(0)->setcellvalue('c' . ($i + 3), $result[$i]['customer_name']);
  $objphpexcel->getactivesheet(0)->setcellvalue('d' . ($i + 3), $ordersdata[$i]['create_time']);
  $objphpexcel->getactivesheet(0)->setcellvalue('e' . ($i + 3), $result[$i]['require_product']);
  $objphpexcel->getactivesheet(0)->setcellvalue('f' . ($i + 3), $result[$i]['require_count']);
  $objphpexcel->getactivesheet(0)->setcellvalue('g' . ($i + 3), $result[$i]['require_time']);
  $objphpexcel->getactivesheet(0)->setcellvalue('h' . ($i + 3), $result[$i]['product_bom_encoding']);
  $objphpexcel->getactivesheet(0)->setcellvalue('i' . ($i + 3), $result[$i]['delivery_time']);
  $objphpexcel->getactivesheet(0)->setcellvalue('j' . ($i + 3), $result[$i]['delivery_memo']);
  $objphpexcel->getactivesheet()->getstyle('a' . ($i + 3) . ':j' . ($i + 3))->getalignment()->setvertical(phpexcel_style_alignment::vertical_center);
  $objphpexcel->getactivesheet()->getstyle('a' . ($i + 3) . ':j' . ($i + 3))->getborders()->getallborders()->setborderstyle(phpexcel_style_border::border_thin);
  $objphpexcel->getactivesheet()->getrowdimension($i + 3)->setrowheight(16);
}


// rename sheet 
$objphpexcel->getactivesheet()->settitle('订单汇总表');


// set active sheet index to the first sheet, so excel opens this as the first sheet 
$objphpexcel->setactivesheetindex(0);


// redirect output to a client's web browser (excel5) 
<span style="color:#ff0000;">ob_end_clean();//清除缓冲区,避免乱码</span>
header('content-type: application/vnd.ms-excel');
header('content-disposition: attachment;filename="订单汇总表(' . date('ymd-his') . ').xls"');
header('cache-control: max-age=0');

$objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel5');
$objwriter->save('php://output');
?>

在header() 前面加上ob_end_clean() 函数,清除缓冲区, 这样就不会乱码了!

以上就是小编为大家带来的完美解决phpexcel导出到xls文件出现乱码的问题全部内容了,希望大家多多支持~