实际上,我想在Ext JS页面中触发自动下载生成的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();
}
下面是生成csv文件的函数。
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 ;
}
答案 0 :(得分:1)
在Ext.js客户端中,我在“按钮”上具有此处理程序:
handler: function(){
var url = "rest/my/endpoint/file?limit="+MY.singleton.AppConfig.exportLimit;
window.open(url,'_blank');
}
在服务器端,我正在使用Java,但唯一真正相关的部分是您已经提供的标头:
httpHeaders.putSingle("Content-Disposition", "attachment; filename=\"" + FILENAME + "\"");
@Produces({CsvObjectMapperProvider.TEXT_CSV, CsvObjectMapperProvider.APPLICATION_EXCEL})
我建议尝试使用不同的内容类型:我的应用可以提供text/csv
或application/vnd.ms-excel
此外,请确保返回的内容是有效的CSV(不允许使用<tags>
,没有合适的换行符,没有“样式”),或者输出的是正确的Excel格式(对此不确定)