$("#excel_export").live("click",function(){
var excel_date_begin =$("#excel_date_begin").val();
var excel_date_end =$("#excel_date_end").val();
if(excel_date_begin==""||excel_date_end==""){
alert("請(qǐng)選擇時(shí)間");
return false;
}
$.ajax({
url:'../app/contract/agency_excel_export',
data:{
excel_date_begin:excel_date_begin,
excel_date_end:excel_date_end,
},
type:'POST',
dataType:'json',
success:function(data){
},
error:function(){
alert("導(dǎo)出失敗")
}
})
})
public static void agency_excel_export(OutputStream os, List<Contract> contracts) {
try {
//創(chuàng)建Excel工作薄
HSSFWorkbook book = new HSSFWorkbook();
//在Excel工作薄中建一張工作表
HSSFSheet sheet = book.createSheet("代理費(fèi)統(tǒng)計(jì)");
//設(shè)置單元格格式(文本)
//HSSFCellStyle cellStyle = book.createCellStyle();
//第一行為標(biāo)題行
HSSFRow row = sheet.createRow(0);//創(chuàng)建第一行
HSSFCell cell0 = row.createCell(0);
HSSFCell cell1 = row.createCell(1);
HSSFCell cell2 = row.createCell(2);
HSSFCell cell3 = row.createCell(3);
HSSFCell cell4 = row.createCell(4);
HSSFCell cell5 = row.createCell(5);
HSSFCell cell6 = row.createCell(6);
HSSFCell cell7 = row.createCell(7);
//定義單元格為字符串類型
cell0.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
cell6.setCellType(HSSFCell.CELL_TYPE_STRING);
cell7.setCellType(HSSFCell.CELL_TYPE_STRING);
//在單元格中輸入數(shù)據(jù)
cell0.setCellValue("客戶名稱");
cell1.setCellValue("客戶地址");
cell2.setCellValue("聯(lián)系人");
cell3.setCellValue("電話");
cell4.setCellValue("手機(jī)");
cell5.setCellValue("郵箱");
cell6.setCellValue("QQ");
cell7.setCellValue("合同類型");
//循環(huán)導(dǎo)出數(shù)據(jù)到excel中
for(int i = 0; i < contracts.size(); i++) {
Contract contract = contracts.get(i);
//創(chuàng)建第i行
HSSFRow rowi = sheet.createRow(i + 1);
//在第i行的相應(yīng)列中加入相應(yīng)的數(shù)據(jù)
rowi.createCell(0).setCellValue(1);
rowi.createCell(1).setCellValue(1);
rowi.createCell(2).setCellValue(1);
rowi.createCell(3).setCellValue(1);
rowi.createCell(4).setCellValue(1);
rowi.createCell(5).setCellValue(1);
rowi.createCell(6).setCellValue(1);
rowi.createCell(7).setCellValue(1);
}
//寫(xiě)入數(shù)據(jù) 把相應(yīng)的Excel 工作簿存盤(pán)
book.write(os);
} catch (IOException e) {
e.printStackTrace();
}
}
}
通常,在web前端需要下載文件,都是通過(guò)指定<a>標(biāo)簽的href屬性,訪問(wèn)服務(wù)器端url即可下載并保存文件到本地。
但是這種方式使用的是HTTP GET方法,參數(shù)只能通過(guò)URL參數(shù)方式傳遞,無(wú)法使用POST方式傳遞參數(shù)。
于是,想到使用ajax方式下載文件。
結(jié)果:ajax方式下載文件時(shí)無(wú)法觸發(fā)瀏覽器打開(kāi)保存文件對(duì)話框,也就無(wú)法將下載的文件保存到硬盤(pán)上!
原因:ajax方式請(qǐng)求的數(shù)據(jù)只能存放在javascipt內(nèi)存空間,可以通過(guò)javascript訪問(wèn),但是無(wú)法保存到硬盤(pán),因?yàn)閖avascript不能直接和硬盤(pán)交互,否則將是一個(gè)安全問(wèn)題。
解決:使用form表單模擬ajaxti提交,然后下載文件
function downloadFileByForm(url, excel_date_begin, excel_date_end){
var form = $("<form></form>").attr("action", url).attr("method", "get");
form.append($("<input></input>").attr("type", "hidden").attr("name", "excel_date_begin").attr("value", excel_date_begin));
form.append($("<input></input>").attr("type", "hidden").attr("name", "excel_date_end").attr("value", excel_date_end));
form.appendTo('body').submit().remove();
}
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。