PHPExcel 德语元音
PHPExcel German vowels
我想使用 phpexcel 从数据库中导出 .xls 文件。
它工作正常,但是德语元音确实会带来很多麻烦。
这是我的代码:
<?php
date_default_timezone_set('Europe/Rome');
/***** EDIT BELOW LINES *****/
$DB_Server = "localhost"; // MySQL Server
$DB_Username = "user"; // MySQL Username
$DB_Password = "passw"; // MySQL Password
$DB_DBName = "test2015"; // MySQL Database Name
$xls_filename = 'export_'.date('Y-m-d').'.xls'; // Define Excel (.xls) file name
/***** DO NOT EDIT BELOW LINES *****/
// Create MySQL connection
$sql = "select * from Teilnehmer join mannschaften mann ON mann.M_id = Teilnehmer.Team";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Failed to connect to MySQL:<br />" . mysql_error() . "<br />" . mysql_errno());
// Select database
$Db = @mysql_select_db($DB_DBName, $Connect) or die("Failed to select database:<br />" . mysql_error(). "<br />" . mysql_errno());
// Execute query
$result = @mysql_query($sql,$Connect) or die("Failed to execute query:<br />" . mysql_error(). "<br />" . mysql_errno());
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Piok")
->setLastModifiedBy("PIOK")
->setTitle("Piok")
->setSubject("Piok")
->setDescription("Piok")
->setKeywords("Piok")
->setCategory("Piok");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Vorname')
->setCellValue('B1', 'Nachname')
->setCellValue('C1', 'Jahrgang')
->setCellValue('D1', 'Geschlecht')
->setCellValue('E1', 'Email')
->setCellValue('F1', 'FISICODE')
->setCellValue('G1', 'Mannschaft');
$startzeile = 2;
while($row = mysql_fetch_row($result))
{
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$startzeile , $row[1])
->setCellValue('B'.$startzeile , $row[2])
->setCellValue('C'.$startzeile , $row[3])
->setCellValue('D'.$startzeile , $row[4])
->setCellValue('E'.$startzeile , $row[6])
->setCellValue('F'.$startzeile , $row[7])
->setCellValue('G'.$startzeile , $row[9]);
$startzeile++;
}
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle($xls_filename);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header('Content-Disposition: attachment;filename="'.$xls_filename.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>
我用PHP5.5
PHP-文件编码设置为UTF-8。
感谢您的帮助。
清除输出缓冲区后再试。
以下回答对你有帮助。这对我有用。
<?php
date_default_timezone_set('Europe/Rome');
/***** EDIT BELOW LINES *****/
$DB_Server = "localhost"; // MySQL Server
$DB_Username = "user"; // MySQL Username
$DB_Password = "passw"; // MySQL Password
$DB_DBName = "test2015"; // MySQL Database Name
$xls_filename = 'export_'.date('Y-m-d').'.xls'; // Define Excel (.xls) file name
/***** DO NOT EDIT BELOW LINES *****/
// Create MySQL connection
$sql = "select * from Teilnehmer join mannschaften mann ON mann.M_id = Teilnehmer.Team";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Failed to connect to MySQL:<br />" . mysql_error() . "<br />" . mysql_errno());
// Select database
$Db = @mysql_select_db($DB_DBName, $Connect) or die("Failed to select database:<br />" . mysql_error(). "<br />" . mysql_errno());
// Execute query
$result = @mysql_query($sql,$Connect) or die("Failed to execute query:<br />" . mysql_error(). "<br />" . mysql_errno());
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
//clean output buffer previously
ob_end_clean();
//start output buffer
ob_start();
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Piok")
->setLastModifiedBy("PIOK")
->setTitle("Piok")
->setSubject("Piok")
->setDescription("Piok")
->setKeywords("Piok")
->setCategory("Piok");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Vorname')
->setCellValue('B1', 'Nachname')
->setCellValue('C1', 'Jahrgang')
->setCellValue('D1', 'Geschlecht')
->setCellValue('E1', 'Email')
->setCellValue('F1', 'FISICODE')
->setCellValue('G1', 'Mannschaft');
$startzeile = 2;
while($row = mysql_fetch_row($result))
{
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$startzeile , $row[1])
->setCellValue('B'.$startzeile , $row[2])
->setCellValue('C'.$startzeile , $row[3])
->setCellValue('D'.$startzeile , $row[4])
->setCellValue('E'.$startzeile , $row[6])
->setCellValue('F'.$startzeile , $row[7])
->setCellValue('G'.$startzeile , $row[9]);
$startzeile++;
}
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle($xls_filename);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header('Content-Disposition: attachment;filename="'.$xls_filename.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
//Cleaning the ouput buffer
ob_flush();
exit;
?>
像这样的问题通常是在将数据从一种介质传输到另一种介质的一种方式中使用了错误的编码造成的。
您的 Excel 输出将字符集设置为 utf-8,这是正常的做法。现在您应该检查您在数据库中使用的编码并确保它也是 UTF-8。
作为最后一步,请确保您也 "talk" 以 UTF-8 格式传输到数据库。
mysql_set_charset("UTF8", $Connect);
我想使用 phpexcel 从数据库中导出 .xls 文件。 它工作正常,但是德语元音确实会带来很多麻烦。 这是我的代码:
<?php
date_default_timezone_set('Europe/Rome');
/***** EDIT BELOW LINES *****/
$DB_Server = "localhost"; // MySQL Server
$DB_Username = "user"; // MySQL Username
$DB_Password = "passw"; // MySQL Password
$DB_DBName = "test2015"; // MySQL Database Name
$xls_filename = 'export_'.date('Y-m-d').'.xls'; // Define Excel (.xls) file name
/***** DO NOT EDIT BELOW LINES *****/
// Create MySQL connection
$sql = "select * from Teilnehmer join mannschaften mann ON mann.M_id = Teilnehmer.Team";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Failed to connect to MySQL:<br />" . mysql_error() . "<br />" . mysql_errno());
// Select database
$Db = @mysql_select_db($DB_DBName, $Connect) or die("Failed to select database:<br />" . mysql_error(). "<br />" . mysql_errno());
// Execute query
$result = @mysql_query($sql,$Connect) or die("Failed to execute query:<br />" . mysql_error(). "<br />" . mysql_errno());
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Piok")
->setLastModifiedBy("PIOK")
->setTitle("Piok")
->setSubject("Piok")
->setDescription("Piok")
->setKeywords("Piok")
->setCategory("Piok");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Vorname')
->setCellValue('B1', 'Nachname')
->setCellValue('C1', 'Jahrgang')
->setCellValue('D1', 'Geschlecht')
->setCellValue('E1', 'Email')
->setCellValue('F1', 'FISICODE')
->setCellValue('G1', 'Mannschaft');
$startzeile = 2;
while($row = mysql_fetch_row($result))
{
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$startzeile , $row[1])
->setCellValue('B'.$startzeile , $row[2])
->setCellValue('C'.$startzeile , $row[3])
->setCellValue('D'.$startzeile , $row[4])
->setCellValue('E'.$startzeile , $row[6])
->setCellValue('F'.$startzeile , $row[7])
->setCellValue('G'.$startzeile , $row[9]);
$startzeile++;
}
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle($xls_filename);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header('Content-Disposition: attachment;filename="'.$xls_filename.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>
我用PHP5.5 PHP-文件编码设置为UTF-8。 感谢您的帮助。
清除输出缓冲区后再试。 以下回答对你有帮助。这对我有用。
<?php
date_default_timezone_set('Europe/Rome');
/***** EDIT BELOW LINES *****/
$DB_Server = "localhost"; // MySQL Server
$DB_Username = "user"; // MySQL Username
$DB_Password = "passw"; // MySQL Password
$DB_DBName = "test2015"; // MySQL Database Name
$xls_filename = 'export_'.date('Y-m-d').'.xls'; // Define Excel (.xls) file name
/***** DO NOT EDIT BELOW LINES *****/
// Create MySQL connection
$sql = "select * from Teilnehmer join mannschaften mann ON mann.M_id = Teilnehmer.Team";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Failed to connect to MySQL:<br />" . mysql_error() . "<br />" . mysql_errno());
// Select database
$Db = @mysql_select_db($DB_DBName, $Connect) or die("Failed to select database:<br />" . mysql_error(). "<br />" . mysql_errno());
// Execute query
$result = @mysql_query($sql,$Connect) or die("Failed to execute query:<br />" . mysql_error(). "<br />" . mysql_errno());
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
//clean output buffer previously
ob_end_clean();
//start output buffer
ob_start();
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Piok")
->setLastModifiedBy("PIOK")
->setTitle("Piok")
->setSubject("Piok")
->setDescription("Piok")
->setKeywords("Piok")
->setCategory("Piok");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Vorname')
->setCellValue('B1', 'Nachname')
->setCellValue('C1', 'Jahrgang')
->setCellValue('D1', 'Geschlecht')
->setCellValue('E1', 'Email')
->setCellValue('F1', 'FISICODE')
->setCellValue('G1', 'Mannschaft');
$startzeile = 2;
while($row = mysql_fetch_row($result))
{
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$startzeile , $row[1])
->setCellValue('B'.$startzeile , $row[2])
->setCellValue('C'.$startzeile , $row[3])
->setCellValue('D'.$startzeile , $row[4])
->setCellValue('E'.$startzeile , $row[6])
->setCellValue('F'.$startzeile , $row[7])
->setCellValue('G'.$startzeile , $row[9]);
$startzeile++;
}
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle($xls_filename);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header('Content-Disposition: attachment;filename="'.$xls_filename.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
//Cleaning the ouput buffer
ob_flush();
exit;
?>
像这样的问题通常是在将数据从一种介质传输到另一种介质的一种方式中使用了错误的编码造成的。
您的 Excel 输出将字符集设置为 utf-8,这是正常的做法。现在您应该检查您在数据库中使用的编码并确保它也是 UTF-8。
作为最后一步,请确保您也 "talk" 以 UTF-8 格式传输到数据库。
mysql_set_charset("UTF8", $Connect);