将中文字符导出为ex​​cel文件

时间:2012-10-06 07:15:24

标签: php excel utf-8 character-encoding export-to-excel

在我的数据库中,中文字符采用此格式大股东

原来汉字是大股东。并正确显示在网页上。 当我从* .xls文件中的页面下载数据时,它生成与数据库(大股东)中相同的字符串。我正在寻找解决方案,将这些字符转换为下载的.xls文件中的原始中文字符。

我在页面上执行此操作,将header('Content-type: text/html; charset=utf-8');放在页面顶部。

我还在excel下载代码中添加了此标题但未解决。

这是用于下载excel文件的php代码。

header('Content-type: text/html; charset=utf-8');

    @include_once('Worksheet.php');
    @include_once('Workbook.php');
    $Module = "searchxlship.php";


    @include_once("../db_class.php");
    $DB = new DBConnect();

    @ini_set('max_execution_time', 1200);
    //================================================================
    //DELETE THE OLD EXISTING FILE
    @chmod("searchxlship.xls", 0777);
    @unlink("searchxlship.xls");

    $workbook       = new Workbook("searchxlship.xls");
    $ColumnWidthArr = array(
        "0" => "0##23##20"
    );

    $worksheet2 =& $workbook->add_worksheetdefault('Ship_order-' . date('d-M-Y'), $ColumnWidthArr);
    //$worksheet2->set_footer("Copyright (R) Worldyards.com Pte Ltd, details without guarantee.");


    //=======Format For Merge Cells========================================================         
    $formatmerg =& $workbook->add_format();
    $formatmerg->set_color('black');
    $formatmerg->set_pattern();
    $formatmerg->set_fg_color('white');
    $formatmerg->set_merge_cell();
    //=====================================================================================

    //======================================================================================================
    $ColHead =& $workbook->add_format();
    $ColHead->set_size(8);
    $ColHead->set_align('center');
    $ColHead->set_color('white');
    $ColHead->set_pattern();
    $ColHead->set_bg_color('green');
    $ColHead->set_bold(1);
    $ColHead->set_bottom(2);
    $ColHead->set_top(2);
    $ColHead->set_bottom_color('black');
    //======================================================================================================

    $formatot =& $workbook->add_format();
    $formatot->set_size(8);
    $formatot->set_align('center');
    $formatot->set_color('black');
    $formatot->set_pattern();
    $formatot->set_fg_color('white');
    $formatot->set_left(2);
    $formatot->set_left_color('black');
    $formatot->set_right_color('black');

    $colformatot =& $workbook->add_format();
    $colformatot->set_size(8);
    $colformatot->set_align('center');
    $colformatot->set_color('brown');
    $colformatot->set_bold(1);
    $colformatot->set_pattern();
    $colformatot->set_fg_color('white');
    $colformatot->set_left(2);
    $colformatot->set_left_color('black');
    $colformatot->set_right_color('black');

    $forredformat =& $workbook->add_format();
    $forredformat->set_size(8);
    $forredformat->set_align('center');
    $forredformat->set_color('red');
    $forredformat->set_bold(1);
    $forredformat->set_pattern();
    $forredformat->set_fg_color('white');
    $forredformat->set_left(2);
    $forredformat->set_left_color('black');
    $forredformat->set_right_color('black');


    $forgreenformat =& $workbook->add_format();
    $forgreenformat->set_size(8);
    $forgreenformat->set_align('center');
    $forgreenformat->set_color('green');
    $forgreenformat->set_bold(1);
    $forgreenformat->set_pattern();
    $forgreenformat->set_fg_color('white');
    $forgreenformat->set_left(2);
    $forgreenformat->set_left_color('black');
    $forgreenformat->set_right_color('black');


    $forbluematot =& $workbook->add_format();
    $forbluematot->set_size(8);
    $forbluematot->set_align('center');
    $forbluematot->set_color('blue');
    $forbluematot->set_bold(1);
    $forbluematot->set_pattern();
    $forbluematot->set_fg_color('white');
    $forbluematot->set_left(2);
    $forbluematot->set_left_color('black');
    $forbluematot->set_right_color('black');

    $formatforVerticalLine =& $workbook->add_format();
    $formatforVerticalLine->set_color('black');
    $formatforVerticalLine->set_pattern();
    $formatforVerticalLine->set_fg_color('white');
    $formatforVerticalLine->set_bold(0);
    $formatforVerticalLine->set_size(8);
    $formatforVerticalLine->set_align('center');
    $formatforVerticalLine->set_left(2);
    $formatforVerticalLine->set_bottom(2);
    $formatforVerticalLine->set_left_color('black');
    $formatforVerticalLine->set_bottom_color('white');

    //==========Format for horizontal line=========
    $formatforDarkHLine =& $workbook->add_format();
    $formatforDarkHLine->set_size(8);
    $formatforDarkHLine->set_align('center');
    $formatforDarkHLine->set_color('black');
    $formatforDarkHLine->set_pattern();
    $formatforDarkHLine->set_bold(1);
    // $formatforDarkHLine->set_bottom(2);            
    $formatforDarkHLine->set_top(2);
    $formatforDarkHLine->set_left(2);
    $formatforDarkHLine->set_right(2);
    $formatforDarkHLine->set_left_color('white');
    $formatforDarkHLine->set_right_color('white');
    $formatforDarkHLine->set_fg_color('white');
    //=============================================//

    //==========Format for horizontal line=========
    $formatformLine =& $workbook->add_format();
    $formatformLine->set_size(8);
    $formatformLine->set_align('center');
    $formatformLine->set_color('black');
    $formatformLine->set_fg_color('white');
    $formatformLine->set_pattern();
    $formatformLine->set_bold(1);
    //  $formatformLine->set_bottom(1);           
    $formatformLine->set_top(2);
    //$formatformLine->set_left(2);
    //$formatformLine->set_right(2);
    $formatformLine->set_bottom_color('black');
    $formatformLine->set_right_color('black');
    $formatformLine->set_fg_color('white');

    $HeadFormat =& $workbook->add_format();
    $HeadFormat->set_size(12);
    $HeadFormat->set_align('left');
    $HeadFormat->set_color('black');
    $HeadFormat->set_pattern();
    $HeadFormat->set_fg_color('white');
    $HeadFormat->set_bold(1);
    $HeadFormat->set_text_wrap(0);


    $ShipQuery = $_POST['xlquery'];


        $Heading = array(
            'S.no',
            'Built/Delivery date',
            'IMO num / WY num WY No in red and Invalid IMO/WY no in brown ',
            'Ship name ',
            'Chinese ship name ',
            'Segment',
            'Sub-segment'
        );




    $ShipRes = $DB->Select($ShipQuery, 'FetchShips', $Module);
    for ($i = 0; $i < $RecordCount + 2000; $i++)
    {
        $worksheet2->set_row($i, 20, $formatmerg);
    }
    $Sno = 2;

    $Sno = $Sno + 2;

        $worksheet2->write_string($Sno, 0, $Heading[0], $ColHead);
        $worksheet2->write_string($Sno, 1, $Heading[1], $ColHead);
        $worksheet2->write_string($Sno, 2, $Heading[2], $ColHead);
        $worksheet2->write_string($Sno, 3, $Heading[3], $ColHead);
        $worksheet2->write_string($Sno, 4, $Heading[4], $ColHead);
        $worksheet2->write_string($Sno, 5, $Heading[5], $ColHead);


    $Sno = $Sno + 1;
    $Col = 0;
    $Num = 1;
    while ($Row = @mysql_fetch_object($ShipRes))
    {
        $worksheet2->write_string($Sno, $Col++, $Num++, $formatot);
        $worksheet2->write_string($Sno, $Col++, $Row->builtdate, $formatot);
        $worksheet2->write_string($Sno, $Col++, $Row->imo, $formatot);
        $worksheet2->write_string($Sno, $Col++, $Row->shipnam, $formatot);
        $worksheet2->write_string($Sno, $Col++, $Row->chineseshipnam, $formatot);        
        $worksheet2->write_string($Sno, $Col++, $Row->segnam, $formatot);
        $worksheet2->write_string($Sno, $Col++, $Row->typnam, $formatot);

        $worksheet2->write_string($Sno, $Col++, "", $formatforVerticalLine);
        $Sno = $Sno + 1;
        $Col = 0;
    }
    //$Sno=$Sno+1;
    $worksheet2->write_string($Sno, 0, '', $formatforDarkHLine);
    $worksheet2->write_string($Sno, 1, '', $formatforDarkHLine);
    $worksheet2->write_string($Sno, 2, '', $formatforDarkHLine);
    $worksheet2->write_string($Sno, 3, '', $formatforDarkHLine);
    $worksheet2->write_string($Sno, 4, '', $formatforDarkHLine);
    $worksheet2->write_string($Sno, 5, '', $formatforDarkHLine);

    $workbook->close();

    $filename = "searchxlship.xls";

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=" . basename($filename) . ";");
    header("Content-Transfer-Encoding: binary");
    readfile($filename);

2 个答案:

答案 0 :(得分:1)

鉴于输入中输入3个字符的输出中有9个字符,Windows很可能默认认为您的电子表格是使用Cpc-1252编码的。

我没有使用过相关的库,但谷歌搜索表明this可能就是答案。

按照你的代码,我认为一旦你创建了$ worksheet变量,你想尝试以下方法:

$worksheet2->setInputEncoding('utf-8');

假设您的数据库存储和连接设置为UTF-8。基本上,将工作表编码设置为与存储数据的编码相同。

答案 1 :(得分:0)

尝试使用此代码:

$text = htmlentities ( "大股东" , ENT_COMPAT ,  'UTF-8', true);