亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP電子表格不要下載excel文件

PHP電子表格不要下載excel文件

PHP
守候你守候我 2022-01-08 20:15:07
我正在使用 php spreedsheet 處理 excel 文件,我試圖提供一個包含數據的數組以在 excel 上下載。我用ajax做這個請求。這是我的代碼。我的代碼在 symfony 3.4 上。我嘗試使用標題,但是當我單擊下載文件時,ajax 請求說好的,但不要給我 excel 文件$datos = $request->request->get('datos');                $datos = json_decode($datos,true);                $cuenta = count($datos);                $spreadsheet = new Spreadsheet();                $cuentaColumnas = count($datos[0]);                 $letra = "a";                $user =  $this->getUser()->getUser();                  for ($i=0; $i <$cuenta ; $i++) {                     $numero = 1;                        for ($j=0; $j < $cuentaColumnas ; $j++) {                            $informacion = $datos[$i][$j];                            $spreadsheet->getActiveSheet()->setCellValue($letra.$numero,$informacion);                            $numero++;                        }                        $letra++;                }                // Set active sheet index to the first sheet, so Excel opens this as the first sheet                 // create the writer                $writer = new Xlsx($spreadsheet);                $writer->save();                return $writer;
查看完整描述

1 回答

?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

實際上,您不能使用 Ajax 下載文件,當您使用 PHP 創建 excel 文件時,您是在服務器端創建它,您必須要求服務器下載它。因此,如果您想下載它,您可以做一個小技巧,使用您剛剛使用 PHP 創建的文件的 URL 創建一個隱藏的錨鏈接,并在 ajax 返回成功時單擊它。


這是一個錨鏈接的例子,把它放在html視圖上。


<a href="replace_src_of_the_file" download id="hiddenDonwloader" hidden></a>

這是 Ajax 的一個示例。當您達到“成功”意味著文件已成功創建時,您可以確保下載它。如果您之前不知道文件名,那么您也可以返回文件名并在“成功”中獲取它并在單擊之前更改href屬性。


$.ajax({

headers: {

// Put your headers here

  _your_header_

},

url: url_of_php, // put your url here

type: 'POST', 

dataType: 'text',

data: {

  datos: data // put your data to send here

},

error: function (jqXHR, textStatus, errorThrown) {

  console.log(jqXHR, textStatus, errorThrown);

},

success: function (data) {

//Here we click the anchor link be sure that the href attribute is right

  document.getElementById('hiddenDonwloader').click();

},

});

希望有幫助!


查看完整回答
反對 回復 2022-01-08
  • 1 回答
  • 0 關注
  • 178 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號