gpt4 book ai didi

php - 在 codeigniter 中从 Controller 下载 excel 文件

转载 作者:行者123 更新时间:2023-11-29 06:00:49 24 4
gpt4 key购买 nike

我遇到了以下问题:我正在使用代码点火器作为 php 框架,从我的一个观点来看,我对使用 PHPExcel 和来自 mysql 数据库的数据生成 xlsx 文件的函数进行了 ajax 调用。该文件已在服务器中正确创建,但当我尝试 force_download 时,它不会下载任何内容。这是 php 函数:

public function generar_excel($idCotizacion){

isLogged($this->session->userdata('logged_in'));
isAdmin($this->session->userdata('logged_in'));

$cotizacion = $this->cotizacion_model->get_cotizacion_by_id($idCotizacion);

$propuestas = $this->propuesta_model->getPropuestasPorCotizacion($idCotizacion);

error_reporting(E_ALL);
date_default_timezone_set('Europe/London');

require_once 'application/libraries/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';
require_once 'application/libraries/PHPExcel-1.8/Classes/PHPExcel.php';

$excel2 = PHPExcel_IOFactory::createReader('Excel2007');
$excel2 = $excel2->load('prueba.xlsx'); // Empty Sheet
$excel2->setActiveSheetIndex(0);





$excel2->getActiveSheet()->setCellValue('C8', $cotizacion["capitas"])
->setCellValue('C2', $cotizacion["empresa_nombre"])
->setCellValue('C9', $cotizacion["masa_salarial"])
->setCellValue('B11', $cotizacion["aseguradora_actual"])
->setCellValue('B13', $cotizacion["variable"]/100)
->setCellValue('B14', '0.6')
->setCellValue('B12', '0');





$letra = 'C';

foreach($propuestas->result_array() as $row) {


$excel2->getActiveSheet()->setCellValue($letra.'11', $row["nombre"])
->setCellValue($letra.'13', $row["variable"]/100)
->setCellValue($letra.'14', '0.6')
->setCellValue($letra.'12', '0')
->setCellValue($letra.'16', '=C$8*'.$letra.'12+C$9*'.$letra.'13+C$8*'.$letra.'14')
->setCellValue($letra.'17','=(B$16-'.$letra.'16)*13')
->setCellValue($letra.'18','=1-('.$letra.'16/B16)');
++$letra;
}


$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$nombreArchivo = 'CuadroComparativo-'.$cotizacion["empresa_nombre"].'.xlsx';

$objWriter->save('uploads/'.$nombreArchivo);
$fileContents = file_get_contents('http://rpm.singleton.com.ar/uploads/'.$nombreArchivo);
//print_r($fileContents);
$this->load->helper('download');
force_download($nombreArchivo, $fileContents);

检查浏览器的响应和预览是不可读的。谢谢!

最佳答案

在 force_download() 函数中为您发送的数据设置 MIME 类型。浏览器可能正在尝试最好地猜测它,或者只是准确输出您发送的内容(这可能是您所指的“不可读”数据)。

尝试将您的 force_download 行更改为:

force_download($nombreArchivo, $fileContents, TRUE);

这将根据您的文件扩展名 (xlsx) 设置 MIME 类型,并强制浏览器下载文件。

关于php - 在 codeigniter 中从 Controller 下载 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45129514/

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