我剛剛試過你的代碼部分,它的工作很好,如果你只是亂碼,問題實際上是你正在生成表的部分,檢查該部分,看看是否有任何錯誤,波紋管是我用來測試它的代碼:
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
/** Include PHPExcel */
require_once '../../PHPExcel/Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Lumina")
->setLastModifiedBy("Lumina")
->setTitle("Entry members")
->setSubject("Entry members")
->setDescription("List of members participting.")
->setKeywords("office PHPExcel php")
->setCategory("Entry members");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', $row_titles['TITLE01'])
->setCellValue('B1', $row_titles['TITLE02'])
->setCellValue('C1', $row_titles['TITLE03'])
->setCellValue('D1', $row_titles['TITLE04'])
->setCellValue('E1', $row_titles['TITLE05'])
->setCellValue('F1', $row_titles['TITLE06'])
->setCellValue('G1', $row_titles['TITLE07'])
->setCellValue('H1', $row_titles['TITLE08'])
->setCellValue('I1', $row_titles['TITLE09'])
->setCellValue('J1', $row_titles['TITLE10'])
->setCellValue('K1', $row_titles['TITLE11'])
->setCellValue('L1', $row_titles['TITLE12'])
->setCellValue('M1', $row_titles['TITLE13'])
->setCellValue('N1', $row_titles['TITLE14'])
->setCellValue('O1', $row_titles['TITLE15'])
->setCellValue('P1', $row_titles['TITLE16'])
->setCellValue('Q1', $row_titles['TITLE17'])
->setCellValue('R1', $row_titles['TITLE18'])
->setCellValue('S1', $row_titles['TITLE19'])
->setCellValue('T1', $row_titles['TITLE20'])
->setCellValue('U1', $row_titles['TEAM_TX'])
->setCellValue('V1', $row_titles['T_SHIRT_SIZE'])
->setCellValue('W1', $row_titles['INPUT1_TX'])
->setCellValue('X1', $row_titles['INPUT2_TX'])
->setCellValue('Y1', $row_titles['INPUT3_TX'])
->setCellValue('Z1', $row_titles['INPUT4_TX']);
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Participants');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Save Excel5 file
//echo date('H:i:s') , " Write to Excel5 format" , EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.html', '.xls', __FILE__));
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment; filename="file.xls"');
// Write file to the browser
$objWriter->save('php://output');
我希望這可以是任何幫助,因爲我也是PHP新手。
在設置標題之前,您是否有任何回聲?你有沒有看過你在屏幕上看到的標題,以確保它們是正確的? – Pitchinnate
執行「另存爲」並在文本編輯器中打開文件:然後查找空白(空格,製表符,新行等)作爲文件中的第一個或最後一個字符,BOM標記或任何可見的PHP錯誤郵件 –
@Mark Baker,我不能「保存」,我沒有收到任何錯誤,那就是問題所在。 – AnchovyLegend