Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: eksport mysql -> excel + polskie
Forum PHP.pl > Forum > Gotowe rozwiązania
revyag
Witam. Mam skrypt, który eksportuje mi dane z bazy mysql do pliku excela. Jest jednak mały problem, wyświetla mi dobrze wszystkie polskie litery oprócz ś. Nie wiem jak to naprawić. Skrypt jest w porządku, działa dobrze, może trzeba coś dopisać ?
  1. <?php
  2. define(db_host, &#092;"localhost\");
  3. define(db_user, &#092;"user\");
  4. define(db_pass, &#092;"pass\");
  5. define(db_link, mysql_connect(db_host,db_user,db_pass));
  6. define(db_name, &#092;"baza\");
  7. mysql_select_db(db_name);
  8.  
  9. $select = &#092;"select * from table\";
  10. $export = mysql_query($select);
  11. $fields = mysql_num_fields($export);
  12.  
  13. for ($i = 0; $i < $fields; $i++)
  14. {
  15. $header .= mysql_field_name($export, $i) . &#092;"t\";
  16. }
  17.  
  18. while($row = mysql_fetch_row($export))
  19. {
  20. $line = '';
  21. foreach($row as $value)
  22. {
  23. if ((!isset($value)) OR ($value == &#092;"\"))
  24. {
  25. $value = &#092;"t\";
  26. }
  27. else
  28. {
  29. $value = str_replace('\"', '\"\"', $value);
  30. $value = '\"' . $value . '\"' . &#092;"t\";
  31. }
  32. $line .= $value;
  33. }
  34. $data .= trim($line).&#092;"n\";
  35. }
  36. $data = str_replace(&#092;"r\",\"\",$data);
  37.  
  38. if ($data == &#092;"\")
  39. {
  40. $data = &#092;"n(0) Nie znaleziono rekordów!n\";
  41. }
  42.  
  43. header(&#092;"Content-type: application/octet-stream\"); 
  44. header(&#092;"Content-Disposition: attachment; filename=extraction.xls\");
  45. header(&#092;"Pragma: no-cache\");
  46. header(&#092;"Expires: 0\");
  47. print &#092;"$headern$data\";
  48. ?>

Update:
Okazało się że źle są odczytywane także ą i ź. Inne są w porządku.
QsQ
Witam
Mialem podobny problem z ś ź ale w troche innym przypadku....

A pomogla zmiana kodowania znakow w bazie na win-1250
ARJ
niełatwiej będzie użyć funkcji COM?
  1. <?php
  2.  
  3. include(&#092;"connect.php\"); //połączenie z bazą
  4. $excel_handle = new COM(&#092;"excel.application\");
  5. $excel_handle->Visible = false;
  6. $worksheet = $excel_handle->workbooks->add();
  7. $values = array(&#092;"Name\", \"Salary\", \"Time of Employment\");
  8. for ($i = 1; $i < 4; ++$i){
  9. $cell = &$worksheet->Cells(1, $i);
  10. $cell->value = $values[$i - 1];
  11. }
  12. $dbh = new DB;
  13. $dbh->connect(&#092;"mssql://username:password@localhost/empreports\");
  14. $sth = $dbh->query(&#092;"SELECT * FROM empnames WHERE salary='salary'\");
  15.  
  16. $idx = 2;
  17. while($row = $dbh->fetchRow($sth, DB_GETMODE_ASSOC)){
  18. $values = array($row['name'], $row['salary'], $row['toe']);
  19. for($i = 0; $i < 4; ++$i){
  20. $sell = &$worksheet->Cells($idx, $i);
  21. $cell->value = $values[$i - 1];
  22. }
  23. }
  24. $dbh->disconnect();
  25. $worksheet->SaveAs(&#092;"em_reports-$salary.xls\");
  26. $excel_handle->quit();
  27.  
  28. ?>

taki mam przykład w książce. nietestowałem czy działa.
revyag
QsQ: Kodowania znaków w bazie nie moge zmienić, bo informacje są pobierane od usera przez formularz na stronie. I muszą być też na stronie wyświetlane. Jak zmienie kodowanie będą krzaki.
ARJ: COM wymaga Windowsa do działania, co go dyskwalifikuje u mnie.
Myślałem o troche łopatologicznym rozwiązaniu:
- sprawdzać każdy znak i jeśli pasuje do wzorca(ą, ś, ź) zmieniać jego kod.
Ewentualnie w locie przekonwertować całość na inne kodowanie. Ma ktoś może lepszy pomysł ?
codo
Mialem identyczny przypadek. Wystarczy dodac:
$data=iconv('ISO-8859-2', 'WINDOWS-1250',$data);
falent
mam podobny problem i za nic nie może mi wyeksportować do excela polskich znaków sad.gif Na serwerze jest wszystko ustawione ok - są polskie znaki. Jak eksportuje do pdf lub worda są a jak eksportuje do excela to mam zamiast polskich liter krzaki. Męczę się już 3 dzień co zrobić?

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

  1. mysql_query("set names cp1250");
  2. mysql_query("set character_set cp1250");
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-2024 Invision Power Services, Inc.