2012-10-06 45 views
1

在我的数据库中,汉字的格式为大股东将汉字导出为ex​​cel文件

本来汉字是大股东。并正确显示在网页上。 当我从* .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); 
+1

将excel表单的编码更改为中文。 –

回答

1

假设你在输出输入3个字符得到9个字符,这是最有可能的Windows默认到以为您的电子表格使用Cpc-1252进行编码。

我没有使用问题的库,但谷歌搜索暗示this可能是答案。

您的代码去,我认为一旦你创建$表变量,你想尝试以下操作:

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

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

+0

也许将代码添加到您的答案 – EaterOfCode

+0

我已经尝试过这个错误: - 胎儿错误功能找不到'setInputEncoding'。 – Bajrang

+0

您可以打开Worksheet.php和寻找可能的方法,还是混淆了? – chooban

0

尝试使用此代码:

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

这种编码格式为'大è,¡ä¸œ'(复制自excel文件) – Bajrang

+0

它与数据库中的相同。 – Bajrang

+0

尝试使用utf8_encode/decode和htmlentities(... utf8 txt ...) –