Chrome正在导出错误的“文件扩展名”而不是xls

时间:2015-05-20 11:57:50

标签: php mysql google-chrome phpexcel

我是phpexcel的新手,所以我使用附加的代码将数据从数据库导出到xls文件。它在除Chrome之外的任何其他浏览器中都可以正常工作,其中下载的文件没有“扩展名”而是xls。 这是我的代码有问题,或者是一个应该修改的浏览器设置?有人能帮我吗?谢谢

下载文件[InventárioLivros(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; ?>

1 个答案:

答案 0 :(得分:1)

您为自己的回复添加了一个http标题“Content-Disposition”。此标题告诉Chrome浏览器以及其他人;-),浏览器应在其文件保存窗口中显示哪个文件名。只需将.xls添加到您的文件名中,浏览器就会使用它。