SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Export Mysql to ecxel/csv arabic encoding

    Hello, I want to export a mysql table to excel files and if not to csv, the problem with excel is it gives me data on one line, so I transformed it to csv and it works, the problem is it does not read arabic data

    header( 'Content-Type: text/csv; charset=utf-8; encoding=UTF-8' );
    header("Cache-Control: cache, must-revalidate");
    header("Pragma: public");
    header( 'Content-Disposition: attachment;filename=export.csv' );

    Help appreciated

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Did it work before you changed the output to CSV? You'll have to show more code for us to help better, but make sure your database connection, table, and columns all use UTF-8 too.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Database fileds are uft

    Quote Originally Posted by AnthonySterling View Post
    Did it work before you changed the output to CSV? You'll have to show more code for us to help better, but make sure your database connection, table, and columns all use UTF-8 too.
    Hello,
    Database table fields are uft8, It did not work both as excel and csv

    It is opening Fine in Notepad, but not in excell

    Thats the complete Code:

    <?php
    header( 'Content-Type: text/csv; charset=utf-8; encoding=UTF-8' );
    header("Cache-Control: cache, must-revalidate");
    header("Pragma: public");
    header( 'Content-Disposition: attachment;filename=export.csv' );
    $conn = mysql_connect( 'localhost', 'root', '' ) or die( mysql_error( ) );
    mysql_select_db( 'labora_donnation', $conn ) or die( mysql_error( $conn ) );
    mysql_query("SET NAMES 'utf8'");
    $query = sprintf( 'SELECT * FROM pr2' );
    $result = mysql_query( $query, $conn ) or die( mysql_error( $conn ) );
    $row = mysql_fetch_assoc( $result );
    if ( $row )
    {
    echocsv( array_keys( $row ) );
    }
    while ( $row )
    {
    echocsv( $row );
    $row = mysql_fetch_assoc( $result );
    }

    function echocsv( $fields )
    {
    $separator = '';
    foreach ( $fields as $field )
    {
    if ( preg_match( '/\\r|\\n|,|"/', $field ) )
    {
    $field = '"' . str_replace( '"', '""', $field ) . '"';
    }
    echo $separator . $field;
    $separator = ',';
    }
    echo "\r\n";
    }
    ?>
    when it open the file with
    Notepad:
    id,name,company,numyear,skill,lang,doc,location,subid,date1,date2,conf,oldnew,fil,selek,shahadat,typx,genderprefer
    1,CONT22,aaaaa,6,gfhghgg, Arabic English French ,hgfhgfh,0,0,2011-09-01,2011-09-30,yes,new,,إداري,,إتصل بنا,
    2,hgjjhjh11,gfdgfgfgg12,22,jhjhj, Arabic English French ,doc11,0,0,2011-10-19,2011-10-20,yes,new,,إداري,,التسجيل للدورة التدريبية,


    With Excell

    id name company numyear skill lang doc location subid date1 date2 conf oldnew fil selek shahadat typx genderprefer
    1 CONT22 aaaaa 6 gfhghgg Arabic English French hgfhgfh 0 0 9/1/2011 9/30/2011 yes new إداري إتصل بنا
    2 hgjjhjh11 gfdgfgfgg12 22 jhjhj Arabic English French doc11 0 0 10/19/2011 10/20/2011 yes new إداري التسجيل للدورة التدريبية


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •