Chrome 导出错误 "file extension" 而不是 xls
Chrome is exporting wrong "file extension" instead of xls
我是 phpexcel 的新手,所以我使用附加的代码将数据从数据库导出到 xls 文件。它在任何其他浏览器中工作正常,除了 Chrome,下载的文件没有 "extension" 而不是 xls。
我的代码有问题还是应该修改浏览器设置?有人能帮我吗?谢谢
文件已下载 [Inventário Livros (20-05-2015)-]
我的代码:
<?php
date_default_timezone_set("Atlantic/Cape_Verde");
require_once '../../assets/PHPExcel_1.8.0/Classes/PHPExcel.php';
require_once '../../assets/PHPExcel_1.8.0/Classes/PHPExcel/IOFactory.php';
$objPHPExcel = new PHPExcel();
$Archive = "Inventário Livros";
$date = date('d/m/Y');
require '../conexao/conexao.php';
$objPHPExcel->getProperties()
->setCreator("RELM")
->setLastModifiedBy("RELM")
->setTitle("Inventário Livros")
->setSubject("Inventário")
->setDescription("")
->setKeywords("")
->setCategory("");
$objPHPExcel->getDefaultStyle()->getFont()->setName('Calibri');
$objPHPExcel->getDefaultStyle()->getFont()->setSize(11);
$objPHPExcel->getActiveSheet()->getRowDimension('9')->setRowHeight(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(9);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(40);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(12);
//=======================================Cabeçalho do Doc==============================================//
$objPHPExcel->getActiveSheet()->setCellValue('C7', "Relação dos livros existentes na biblioteca");
$objPHPExcel->setActiveSheetIndex(0)->mergeCells('C7:F7');
$objPHPExcel->getActiveSheet()
->getStyle('C7:F7')
->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()
->getStyle('C7:F7')
->getFont()
->setBold(true);
$objPHPExcel->getActiveSheet()
->getStyle('C7:F7')
->getFont()
->setSize(14);
//=======================================Imagem==============================================//
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('header');
$objDrawing->setDescription('Cabeçalho da pagina');
$objDrawing->setPath('../../images/header.png');
$objDrawing->setCoordinates('C1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
//=================================Conteudo(posiçao e estilo)==============================//
$y = 9;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("A" . $y, 'Nº')
->setCellValue("B" . $y, 'Estante')
->setCellValue("C" . $y, 'Obra')
->setCellValue("D" . $y, 'Autor')
->setCellValue("E" . $y, 'Categoria')
->setCellValue("F" . $y, 'Ano Esc.')
->setCellValue("G" . $y, 'Obs.');
$objPHPExcel->getActiveSheet()
->getStyle('A9:G9')
->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()->setARGB('FFEEEEEE');
$objPHPExcel->getActiveSheet()
->getStyle('A9:G9')
->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()
->getStyle('A9:G9')
->getFont()
->setBold(true);
$objPHPExcel->getActiveSheet()
->getStyle('A9:G9')
->getFont()
->setSize(12);
$borders = array(
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN,
'color' => array('argb' => 'FF000000'),
)
),
);
$objPHPExcel->getActiveSheet()
->getStyle('A9:G9')
->applyFromArray($borders);
$query = "SELECT * FROM book AS B INNER JOIN category AS C ON B.category_id=C.id_category INNER JOIN scholarity AS S ON B.scholarity_id=S.id_scholarity ORDER BY number";
$res = mysql_query($query);
while ($row = mysql_fetch_array($res)) {
$y++;
$objPHPExcel->setActiveSheetIndex(0)
->getStyle('A' . $y . ":G" . $y)
->applyFromArray($borders);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("A" . $y, $row['number'])
->setCellValue("B" . $y, $row['shelf'])
->setCellValue("C" . $y, $row['title'])
->setCellValue("D" . $y, $row['author'])
->setCellValue("E" . $y, $row['category_name'])
->setCellValue("F" . $y, $row['scholarity_name'])
->setCellValue("G" . $y, $row['obs']);
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename= "' . $Archive . ' (' . $date . ') " ');
header('Cache-Control: max-age-0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit; ?>
您在响应中添加了 http header "Content-Disposition"。 header 告诉 Chrome 浏览器,以及其他浏览器 ;-),浏览器应该在其文件保存 windows 中显示哪个文件名。只需将 .xls 添加到您的文件名中,浏览器就会使用它。
我是 phpexcel 的新手,所以我使用附加的代码将数据从数据库导出到 xls 文件。它在任何其他浏览器中工作正常,除了 Chrome,下载的文件没有 "extension" 而不是 xls。 我的代码有问题还是应该修改浏览器设置?有人能帮我吗?谢谢
文件已下载 [Inventário Livros (20-05-2015)-]
我的代码:
<?php
date_default_timezone_set("Atlantic/Cape_Verde");
require_once '../../assets/PHPExcel_1.8.0/Classes/PHPExcel.php';
require_once '../../assets/PHPExcel_1.8.0/Classes/PHPExcel/IOFactory.php';
$objPHPExcel = new PHPExcel();
$Archive = "Inventário Livros";
$date = date('d/m/Y');
require '../conexao/conexao.php';
$objPHPExcel->getProperties()
->setCreator("RELM")
->setLastModifiedBy("RELM")
->setTitle("Inventário Livros")
->setSubject("Inventário")
->setDescription("")
->setKeywords("")
->setCategory("");
$objPHPExcel->getDefaultStyle()->getFont()->setName('Calibri');
$objPHPExcel->getDefaultStyle()->getFont()->setSize(11);
$objPHPExcel->getActiveSheet()->getRowDimension('9')->setRowHeight(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(9);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(40);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(12);
//=======================================Cabeçalho do Doc==============================================//
$objPHPExcel->getActiveSheet()->setCellValue('C7', "Relação dos livros existentes na biblioteca");
$objPHPExcel->setActiveSheetIndex(0)->mergeCells('C7:F7');
$objPHPExcel->getActiveSheet()
->getStyle('C7:F7')
->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()
->getStyle('C7:F7')
->getFont()
->setBold(true);
$objPHPExcel->getActiveSheet()
->getStyle('C7:F7')
->getFont()
->setSize(14);
//=======================================Imagem==============================================//
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('header');
$objDrawing->setDescription('Cabeçalho da pagina');
$objDrawing->setPath('../../images/header.png');
$objDrawing->setCoordinates('C1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
//=================================Conteudo(posiçao e estilo)==============================//
$y = 9;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("A" . $y, 'Nº')
->setCellValue("B" . $y, 'Estante')
->setCellValue("C" . $y, 'Obra')
->setCellValue("D" . $y, 'Autor')
->setCellValue("E" . $y, 'Categoria')
->setCellValue("F" . $y, 'Ano Esc.')
->setCellValue("G" . $y, 'Obs.');
$objPHPExcel->getActiveSheet()
->getStyle('A9:G9')
->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()->setARGB('FFEEEEEE');
$objPHPExcel->getActiveSheet()
->getStyle('A9:G9')
->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()
->getStyle('A9:G9')
->getFont()
->setBold(true);
$objPHPExcel->getActiveSheet()
->getStyle('A9:G9')
->getFont()
->setSize(12);
$borders = array(
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN,
'color' => array('argb' => 'FF000000'),
)
),
);
$objPHPExcel->getActiveSheet()
->getStyle('A9:G9')
->applyFromArray($borders);
$query = "SELECT * FROM book AS B INNER JOIN category AS C ON B.category_id=C.id_category INNER JOIN scholarity AS S ON B.scholarity_id=S.id_scholarity ORDER BY number";
$res = mysql_query($query);
while ($row = mysql_fetch_array($res)) {
$y++;
$objPHPExcel->setActiveSheetIndex(0)
->getStyle('A' . $y . ":G" . $y)
->applyFromArray($borders);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("A" . $y, $row['number'])
->setCellValue("B" . $y, $row['shelf'])
->setCellValue("C" . $y, $row['title'])
->setCellValue("D" . $y, $row['author'])
->setCellValue("E" . $y, $row['category_name'])
->setCellValue("F" . $y, $row['scholarity_name'])
->setCellValue("G" . $y, $row['obs']);
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename= "' . $Archive . ' (' . $date . ') " ');
header('Cache-Control: max-age-0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit; ?>
您在响应中添加了 http header "Content-Disposition"。 header 告诉 Chrome 浏览器,以及其他浏览器 ;-),浏览器应该在其文件保存 windows 中显示哪个文件名。只需将 .xls 添加到您的文件名中,浏览器就会使用它。