gpt4 book ai didi

php - Chrome 正在导出错误的 "file extension"而不是 xls

转载 作者:行者123 更新时间:2023-11-29 02:56:22 25 4
gpt4 key购买 nike

我是 phpexcel 的新手,所以我使用附加的代码将数据从数据库导出到 xls 文件。它在除 Chrome 之外的任何其他浏览器中都可以正常工作,其中下载的文件没有“扩展名”,而是 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 浏览器和其他浏览器 ;-),浏览器应在其文件保存窗口中显示哪个文件名。只需将 .xls 添加到您的文件名中,浏览器就会使用它。

关于php - Chrome 正在导出错误的 "file extension"而不是 xls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30349003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com