我正在尝试将drupal视图导出为excel,我正在使用PHPExcel创建excel文件。代码如下
public static function ExportExcel() {
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
/** Include PHPExcel */
require_once 'libraries/phpexcel/Classes/PHPExcel.php';
require_once 'libraries/phpexcel/Classes/PHPExcel/IOFactory.php';
// Create new PHPExcel object
$objPHPExcel = new \PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
//$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getCell('A1')->setValue('TEST PHPEXCEL');
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// 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');
header('Content-Disposition: attachment;filename="02simple.xls"');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
示例数据(' TEST PHPEXCEl')也提供了特殊字符和图纸属性。
可以做些什么来解决这个问题?
与此同时,我正在使用Ubuntu进行开发。
答案 0 :(得分:1)
我找到了一种解决方法,可以省略工作表中的属性并对其进行格式化。
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
ob_end_clean();
$objWriter->save('php://output');
我认为一行代码解决了我的问题
ob_end_clean();
答案 1 :(得分:0)
尝试更改
header('Content-Type: application/vnd.ms-excel')
到
header('Content-Type: application/vnd.ms-excel; charset=utf-8')
答案 2 :(得分:0)
试试这个:
public static function ExportExcel() {
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header('Content-Disposition: attachment;filename="02simple.xls"');
/** Include PHPExcel */
require_once 'libraries/phpexcel/Classes/PHPExcel.php';
require_once 'libraries/phpexcel/Classes/PHPExcel/IOFactory.php';
// Create new PHPExcel object
$objPHPExcel = new \PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
//$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getCell('A1')->setValue('TEST PHPEXCEL');
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}