代码之家  ›  专栏  ›  技术社区  ›  Patrick Dapa

Excel文件下载被Ext JS阻止

  •  0
  • Patrick Dapa  · 技术社区  · 6 年前

    实际上,我想在extjs页面中触发自动下载生成的excel文件。是否有任何示例源代码可以帮助您? 在下面找到我的个人源代码,它实际上不起作用(单击“导出csv”按钮时调用此函数:

    function exportCsv($criteresRecherche)
    {
        $data = $this->executeRequete($sql);           
    
        header("Content-type: application/vnd.ms-excel");
        header("Content-disposition: attachment; filename=\"export_commandes.csv\"");
    
    
        $colToShow = array(
            "fournisseur"=>"Fournisseur",
            "lieudepart"=>"Départ",
            "datedepartprevue"=> "Date Dep Prevu",
            "confirme"=> "Emb.",
            "modepaiement"=>"Pmt.", 
            "incoterm"=>"Inc.",
            "containers"=>"Containers",
            "nocde"=>"CDE BIE N°",
            "nocde_associees"=>"CDE BIE ASSOC",
            "nocde_erp"=>"N° GNX",
            "aa"=>"Acheteur",
            "acp"=>"Acheteur CP",
            "resp_suivi"=>"Suivi par",
            "shippingmarks"=>"Shp.",
            "artwrk"=>"Art.",
            "positionetiquette"=>"Pos.",
            "relance1mois"=>"Rel",
            "echantillon"=>"Ech.",
            "certification"=>"Cer.",
            "consolidation"=>"Cns.",
            "etiquette_securite"=>"Sec.",
            "devise"=>"Dev.",
            "montant_total"=>"Total Achat", 
            "statut"=>"Statut",
            "datecreation"=>"Date de création",
            "dateintegration"=>"Date intégration"
        );
        echo $this->getHtmlToCsv($data, $colToShow); 
        exit();
    }
    

    function getHtmlToCsv($elements, $colToShow)
    {
        $retour = "<table >"."\n";
        $retour .= "<tr class=\"titre\">"."\n";
    
        if ($colToShow && is_array($colToShow)) {
            foreach ($colToShow as $col => $libColonne) {
                $retour .= "<td>".strtr(
                    $libColonne, get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                )."</td>"."\n";
            }
        }
        $retour .= "</tr>"."\n";
    
        foreach ($elements as $element) {
            $retour .= "<tr>"."\n";
            $retour .= "<td>".strtr(
                $element['FOURNISSEUR'], get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                )."</td>"."\n" ;
    
                $retour .= "<td>".strtr(
                    $element['LIEUDEPART'], get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                    )."</td>"."\n" ;
                $retour .= "<td>".strtr(
                    $element['DATEDEPARTPREVUE'], get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                    )."</td>"."\n" ;
                $retour .= "<td>".strtr(
                    $element['CONFIRME'], get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                    )."</td>"."\n" ;
                $retour .= "<td>".strtr(
                    $element['MODEPAIEMENT'], get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                    )."</td>"."\n" ;
                    $retour .= "</tr>"."\n".strtr(
                    $element['INCOTERM'], get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                    )."</td>"."\n" ;
                    $retour .= "</tr>"."\n".strtr(
                    $element['CONTAINER'], get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                    )."</td>"."\n" ;
                    $retour .= "</tr>"."\n".strtr(
                    $element['NOCDE'], get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                    )."</td>"."\n" ;
                    $retour .= "</tr>"."\n".strtr(
                    $element['NOCDE_ASSOCIEES'], get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                    )."</td>"."\n" ;
                    $retour .= "</tr>"."\n".strtr(
                    $element['NOCDE_ERP'], get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                    )."</td>"."\n" ;
                    $retour .= "</tr>"."\n".strtr(
                    $element['AA'], get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                    )."</td>"."\n" ;
                    $retour .= "</tr>"."\n".strtr(
                    $element['ACP'], get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                    )."</td>"."\n" ;
                    $retour .= "</tr>"."\n".strtr(
                    $element['RESP_SUIVI'], get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                    )."</td>"."\n" ;
                    $retour .= "</tr>"."\n".strtr(
                    $element['SHIPPINGMARKS'], get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "ISO-8859-1")
                    )."</td>"."\n" ;
                $retour .= "</tr>"."\n";
    
    
        }
        $retour .= "</table>"."\n";
        $retour .= "<br>";
    
        // Balise qui force le type a texte pour ne subir aucun formattage excel
        $style = "<style>\n"
            . "td{mso-number-format:\"\@\"; border-bottom: 1px solid black; border-right: 1px solid black;}\n"
            . ".titre { font-size: 16px; font-weight: bold;}\n"
            . "table { border: 1px solid black;}\n"
                . "</style>\n";
    
        $retour = utf8_decode($retour);
        return $retour ;
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   GriffoGoes    6 年前

    在我的Ext.js客户端,我在一个“按钮”上有一个处理程序:

    handler: function(){
        var url = "rest/my/endpoint/file?limit="+MY.singleton.AppConfig.exportLimit;
        window.open(url,'_blank');
    }
    

    httpHeaders.putSingle("Content-Disposition", "attachment; filename=\"" + FILENAME + "\"");
    
    @Produces({CsvObjectMapperProvider.TEXT_CSV, CsvObjectMapperProvider.APPLICATION_EXCEL})
    

    我建议尝试不同的内容类型:我的应用程序可以提供 text/csv application/vnd.ms-excel

    <tags> 允许,正确的换行符,没有“样式”),或者输出正确的Excel格式(对此不确定)