使用PHPExcel导出到xlsx [英] Using PHPExcel to export to xlsx
问题描述
我正在使用PHPEXxcel导出使用MYSQL及其类似方式生成的HTML表.
I am using PHPEXxcel to export an HTML Table generated using MYSQL and its like this.
<?php $query = "SELECT `Firstname`,`Lastname`,`Branch`,`Gender`,`Mobileno`, `Email`
FROM `student_details` WHERE Branch IN ('$branch') and `Year`='$year'
and Tenthresult > '$tenth' and
Twelthresult > '$twelth' and (CGPA > '$cgpa' || CGPA = '$cgpa')";
$result = mysql_query($query);
confirm_query($result);
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$rowCount = 1;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount,'Firstname');
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount,'Lastname');
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount,'Branch');
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount,'Gender');
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount,'Mobileno');
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowCount,'Email');
while($row = mysql_fetch_array($result)){
$rowCount++;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['0']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['1']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row['2']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount, $row['3']);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount, $row['4']);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowCount, $row['5']);
}
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('some_excel_file.xlsx');
?>
它可以工作,但是它将xlsx文件保存在根文件夹中,而不会向用户显示任何已下载文件的迹象. 当我单击一个按钮时,此代码运行.现在,我是否可以像下载邮件附件一样将其下载,并在前端向用户显示其下载位置.
Its working but it saves the xlsx file in the root folder without showing to user any signs that its being downloaded. This code rund when i click a button.now, can i make it to be downloaded like we download a mail attachment and showing the user in the front end that its being downloaded along with the location.
我尝试使用
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
有了这个,我得到了上面想要的东西,但是打开时下载的xls文件显示 讯息 您要打开的文件'filename'的格式与指定的扩展名..... etc不同.要立即打开吗?
With this, i am getting what i wanted above but the xls file downloaded when opened shows the message 'The File you are trying to open 'filename' is in a different format than the specified extension.....etc.Do you want to open now?
打开时,它包含整个HTML页或仅包含空白页... 有人可以帮助我吗?.
On opening it contains either the entire HTML Page or its simply blank... Can anybody help me..?
推荐答案
电子表格101
有许多不同的电子表格文件格式,每种都有自己不同的文件扩展名,并且可以使用不同的mime类型发送到网络浏览器.这些在PHPExcel文档中进行了描述,并且在PHPExcel中每个都有其自己的不同Writer.您错配了两种不同的格式
There are many different spreadsheet file formats, each with their own different filename extensions, and that can be sent to a web browser using different mime types. These are described in the PHPExcel documentation, and each has its own different Writer in PHPExcel. You're mismatching two different formats
BIFF格式
-
由Microsoft Excel在版本95和2003 File之间使用
Used by Microsoft Excel between versions 95 and 2003 File
扩展名: xls
PHPEXcel编写器: PHPExcel_Writer_Excel5
PHPEXcel Writer: PHPExcel_Writer_Excel5
MIME类型: application/vnd.ms-excel
Mime Type: application/vnd.ms-excel
OfficeOpenXML格式
-
自2007年版本开始由Microsoft Excel使用
Used by Microsoft Excel since version 2007
文件扩展名: xlsx
PHPEXcel编写器: PHPExcel_Writer_Excel2007
PHPEXcel Writer: PHPExcel_Writer_Excel2007
MIME类型: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Mime Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
请勿混搭:如果您这样做,则Excel将(并且理所当然地)抱怨.如果要使用BIFF文件,请使用PHPExcel的BIFF Writer(Excel5),文件扩展名为.xls,以及上面针对BIFF格式列出的mime类型.如果需要OfficeOpenXML文件,请使用PHPExcel的Excel2007 Writer,文件扩展名为.xlsx以及上面列出的OfficeOpenXML的mime类型.
Don't mix and match: if you do, then Excel will (and justifiably) complain. If you want a BIFF file, use PHPExcel's BIFF Writer (Excel5), a file extension of .xls, and the mime type listed above for BIFF Format. If you want an OfficeOpenXML file, then use PHPExcel's Excel2007 Writer, a file extension of .xlsx, and the mime type listed above for OfficeOpenXML.
编辑
请注意,PHPExcel发行版随附的示例包括01simple-download-xls.php和01simple-download-xlsx.php来演示您想要的内容
Note that the examples provided with the PHPExcel distribution include 01simple-download-xls.php and 01simple-download-xlsx.php to demonstrate exactly what you want
这篇关于使用PHPExcel导出到xlsx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!