Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: polskie znaki przy eksporcie do excela z bazy MySQL przez php
Forum PHP.pl > Forum > Bazy danych
falent
Przy eksporcie danych mam krzaki zamiast ę±¶ć, reszta polskich liter działa bez zarzutu. Kodowanie w bd ustawione na utf8_polish_ci


Co można zrobić żeby ±ę¶ć działało?

  1. <?php
  2.  
  3. /*_________________________________________________ _______________
  4.  
  5.  
  6. lixlpixel PHParadise
  7.  
  8. __________________________________________________ _____________________
  9.  
  10. category : databases
  11.  
  12. snippet : mySQL to excel
  13.  
  14. downloaded : 05.04.2012 - 03:49
  15.  
  16. file URL : <a href="http://www.fundisom.com/phparadise/p...mySQL_to_excel" target="_blank">http://www.fundisom.com/phparadise/p...mySQL_to_excel</a>
  17.  
  18. description : export a mySQL database table to an EXCEL file.
  19. database table dump to WORD document possible also.
  20.  
  21. ___________________________START___COPYING___HERE_ _________________*/ ?>
  22.  
  23.  
  24. <?php
  25.  
  26. //EDIT YOUR MySQL Connection Info:
  27. $DB_Server = "localhost"; //your MySQL Server
  28. $DB_Username = "************"; //your MySQL User Name
  29. $DB_Password = "***************"; //your MySQL Password
  30. $DB_DBName = "***************"; //your MySQL Database Name
  31. $DB_TBLName = "aplikacja1"; //your MySQL Table Name
  32. //$DB_TBLName, $DB_DBName, may also be commented out & passed to the browser
  33. //as parameters in a query string, so that this code may be easily reused for
  34. //any MySQL table or any MySQL database on your server
  35.  
  36. //DEFINE SQL QUERY:
  37. //you can use just about ANY kind of select statement you want -
  38. //edit this to suit your needs!
  39. $sql = "Select * from $DB_TBLName";
  40.  
  41. //Optional: print out title to top of Excel or Word file with Timestamp
  42. //for when file was generated:
  43. //set $Use_Titel = 1 to generate title, 0 not to use title
  44. $Use_Title = 1;
  45. //define date for title: EDIT this to create the time-format you need
  46. $now_date = date('m-d-Y H:i');
  47. //define title for .doc or .xls file: EDIT this if you want
  48. $title = "Szanowna Pani kierownik na pani życzenie lista aplikacji o miejsce w akademiku dnia $now_date";
  49. /*
  50.  
  51. Leave the connection info below as it is:
  52. just edit the above.
  53.  
  54. (Editing of code past this point recommended only for advanced users.)
  55. */
  56. //create MySQL connection
  57. $Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
  58. or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
  59. mysql_query("SET NAMES 'utf8'");
  60. //select database
  61. $Db = @mysql_select_db($DB_DBName, $Connect)
  62. or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
  63. //execute query
  64. $result = @mysql_query($sql,$Connect)
  65. or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
  66.  
  67. //if this parameter is included ($w=1), file returned will be in word format ('.doc')
  68. //if parameter is not included, file returned will be in excel format ('.xls')
  69. if (isset($w) && ($w==1))
  70. {
  71. $file_type = "msword";
  72. $file_ending = "doc";
  73. }else {
  74. $file_type = "vnd.ms-excel";
  75. $file_ending = "xls";
  76. }
  77. //header info for browser: determines file type ('.doc' or '.xls')
  78. header("Content-Type: application/$file_type");
  79. header("Content-Disposition: attachment; filename=baza_aplikacji.$file_ending");
  80. header("Pragma: no-cache");
  81. header("Expires: 0");
  82.  
  83. /* Start of Formatting for Word or Excel */
  84.  
  85. if (isset($w) && ($w==1)) //check for $w again
  86. {
  87. /* FORMATTING FOR WORD DOCUMENTS ('.doc') */
  88. //create title with timestamp:
  89. if ($Use_Title == 1)
  90. {
  91. echo("$title\n\n");
  92. }
  93. //define separator (defines columns in excel & tabs in word)
  94. $sep = "\n"; //new line character
  95.  
  96. while($row = mysql_fetch_row($result))
  97. {
  98. //set_time_limit(60); // HaRa
  99. $schema_insert = "";
  100. for($j=0; $j<mysql_num_fields($result);$j++)
  101. {
  102. //define field names
  103. $field_name = mysql_field_name($result,$j);
  104. //will show name of fields
  105. $schema_insert .= "$field_name:\t";
  106. if(!isset($row[$j])) {
  107. $schema_insert .= "NULL".$sep;
  108. }
  109. elseif ($row[$j] != "") {
  110. $schema_insert .= "$row[$j]".$sep;
  111. }
  112. else {
  113. $schema_insert .= "".$sep;
  114. }
  115. }
  116. $schema_insert = str_replace($sep."$", "", $schema_insert);
  117. $schema_insert .= "\t";
  118. print(trim($schema_insert));
  119. //end of each mysql row
  120. //creates line to separate data from each MySQL table row
  121. print "\n----------------------------------------------------\n";
  122. }
  123. }else{
  124. /* FORMATTING FOR EXCEL DOCUMENTS ('.xls') */
  125. //create title with timestamp:
  126. if ($Use_Title == 1)
  127. {
  128. echo("$title\n");
  129. }
  130. //define separator (defines columns in excel & tabs in word)
  131. $sep = "\t"; //tabbed character
  132.  
  133. //start of printing column names as names of MySQL fields
  134. for ($i = 0; $i < mysql_num_fields($result); $i++)
  135. {
  136. echo mysql_field_name($result,$i) . "\t";
  137. }
  138. print("\n");
  139. //end of printing column names
  140.  
  141. //start while loop to get data
  142. while($row = mysql_fetch_row($result))
  143. {
  144. //set_time_limit(60); // HaRa
  145. $schema_insert = "";
  146. for($j=0; $j<mysql_num_fields($result);$j++)
  147. {
  148. if(!isset($row[$j]))
  149. $schema_insert .= "NULL".$sep;
  150. elseif ($row[$j] != "")
  151. $schema_insert .= "$row[$j]".$sep;
  152. else
  153. $schema_insert .= "".$sep;
  154. }
  155. $schema_insert = str_replace($sep."$", "", $schema_insert);
  156. //following fix suggested by Josue (thanks, Josue!)
  157. //this corrects output in excel when table fields contain \n or \r
  158. //these two characters are now replaced with a space
  159. $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
  160. $schema_insert .= "\t";
  161. print(trim($schema_insert));
  162. print "\n";
  163. }
  164. }
  165.  
  166. ?>
  167.  
  168.  
  169. <?php /*_____________________END___OF___THE___CODE_______ _______________
  170.  
  171.  
  172. get more code from <a href="http://www.fundisom.com/phparadise/" target="_blank">http://www.fundisom.com/phparadise/</a>
  173.  
  174.  
  175. __________________________________________________ _________________*/ ?>
franki01
Radziłbym przenie¶ć na dział PHP.

Na górze zainicjuj ładowanie do bufora przez ob_start. W ostatniej linijce kodu pobierz wszystko z bufora i przekonwertuj kodowanie z utf-8 na windows-1250:
  1. $text = ob_get_clean();
  2. $text = iconv('UTF-8', 'CP1250//TRANSLIT', $text);
  3. echo $text;
falent
niestety nie działa sad.gif Ma kto¶ jeszcze jaki¶ pomysł?

  1. <?php
  2.  
  3.  
  4. $DB_Server = "localhost"; //your MySQL Server
  5. $DB_Username = "************"; //your MySQL User Name
  6. $DB_Password = "***************"; //your MySQL Password
  7. $DB_DBName = "***************"; //your MySQL Database Name
  8. $DB_TBLName = "aplikacja1"; //your MySQL Table Name
  9. //$DB_TBLName, $DB_DBName, may also be commented out & passed to the browser
  10. //as parameters in a query string, so that this code may be easily reused for
  11. //any MySQL table or any MySQL database on your server
  12.  
  13. //DEFINE SQL QUERY:
  14. //you can use just about ANY kind of select statement you want -
  15. //edit this to suit your needs!
  16. $sql = "Select * from $DB_TBLName";
  17.  
  18. //Optional: print out title to top of Excel or Word file with Timestamp
  19. //for when file was generated:
  20. //set $Use_Titel = 1 to generate title, 0 not to use title
  21. $Use_Title = 1;
  22. //define date for title: EDIT this to create the time-format you need
  23. $now_date = date('m-d-Y H:i');
  24. //define title for .doc or .xls file: EDIT this if you want
  25. $title = "Szanowna Pani kierownik na pani życzenie lista aplikacji o miejsce w akademiku dnia $now_date";
  26. /*
  27.  
  28. Leave the connection info below as it is:
  29. just edit the above.
  30.  
  31. (Editing of code past this point recommended only for advanced users.)
  32. */
  33. //create MySQL connection
  34. $Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
  35. or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
  36. mysql_query("SET NAMES 'utf8'");
  37. //select database
  38. $Db = @mysql_select_db($DB_DBName, $Connect)
  39. or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
  40. //execute query
  41. $result = @mysql_query($sql,$Connect)
  42. or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
  43.  
  44. //if this parameter is included ($w=1), file returned will be in word format ('.doc')
  45. //if parameter is not included, file returned will be in excel format ('.xls')
  46. if (isset($w) && ($w==1))
  47. {
  48. $file_type = "msword";
  49. $file_ending = "doc";
  50. }else {
  51. $file_type = "vnd.ms-excel";
  52. $file_ending = "xls";
  53. }
  54. //header info for browser: determines file type ('.doc' or '.xls')
  55. header("Content-Type: application/$file_type");
  56. header("Content-Disposition: attachment; filename=baza_aplikacji.$file_ending");
  57. header("Pragma: no-cache");
  58. header("Expires: 0");
  59.  
  60. /* Start of Formatting for Word or Excel */
  61.  
  62. if (isset($w) && ($w==1)) //check for $w again
  63. {
  64. /* FORMATTING FOR WORD DOCUMENTS ('.doc') */
  65. //create title with timestamp:
  66. if ($Use_Title == 1)
  67. {
  68. echo("$title\n\n");
  69. }
  70. //define separator (defines columns in excel & tabs in word)
  71. $sep = "\n"; //new line character
  72.  
  73. while($row = mysql_fetch_row($result))
  74. {
  75. //set_time_limit(60); // HaRa
  76. $schema_insert = "";
  77. for($j=0; $j<mysql_num_fields($result);$j++)
  78. {
  79. //define field names
  80. $field_name = mysql_field_name($result,$j);
  81. //will show name of fields
  82. $schema_insert .= "$field_name:\t";
  83. if(!isset($row[$j])) {
  84. $schema_insert .= "NULL".$sep;
  85. }
  86. elseif ($row[$j] != "") {
  87. $schema_insert .= "$row[$j]".$sep;
  88. }
  89. else {
  90. $schema_insert .= "".$sep;
  91. }
  92. }
  93. $schema_insert = str_replace($sep."$", "", $schema_insert);
  94. $schema_insert .= "\t";
  95. print(trim($schema_insert));
  96. //end of each mysql row
  97. //creates line to separate data from each MySQL table row
  98. print "\n----------------------------------------------------\n";
  99. }
  100. }else{
  101. /* FORMATTING FOR EXCEL DOCUMENTS ('.xls') */
  102. //create title with timestamp:
  103. if ($Use_Title == 1)
  104. {
  105. echo("$title\n");
  106. }
  107. //define separator (defines columns in excel & tabs in word)
  108. $sep = "\t"; //tabbed character
  109.  
  110. //start of printing column names as names of MySQL fields
  111. for ($i = 0; $i < mysql_num_fields($result); $i++)
  112. {
  113. echo mysql_field_name($result,$i) . "\t";
  114. }
  115. print("\n");
  116. //end of printing column names
  117.  
  118. //start while loop to get data
  119. while($row = mysql_fetch_row($result))
  120. {
  121. //set_time_limit(60); // HaRa
  122. $schema_insert = "";
  123. for($j=0; $j<mysql_num_fields($result);$j++)
  124. {
  125. if(!isset($row[$j]))
  126. $schema_insert .= "NULL".$sep;
  127. elseif ($row[$j] != "")
  128. $schema_insert .= "$row[$j]".$sep;
  129. else
  130. $schema_insert .= "".$sep;
  131. }
  132. $schema_insert = str_replace($sep."$", "", $schema_insert);
  133. //following fix suggested by Josue (thanks, Josue!)
  134. //this corrects output in excel when table fields contain \n or \r
  135. //these two characters are now replaced with a space
  136. $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
  137. $schema_insert .= "\t";
  138. print(trim($schema_insert));
  139. print "\n";
  140. }
  141. }
  142.  
  143. $text = ob_get_clean();
  144. $text = iconv('UTF-8', 'CP1250//TRANSLIT', $text);
  145. echo $text;
  146.  
  147. ?>
  148.  
  149.  
To jest wersja lo-fi głównej zawarto¶ci. Aby zobaczyć pełn± wersję z większ± zawarto¶ci±, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.