JQuery 中怎么利用Ajax 導(dǎo)出報(bào)表,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)專(zhuān)注于陸豐企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站開(kāi)發(fā)。陸豐網(wǎng)站建設(shè)公司,為陸豐等地區(qū)提供建站服務(wù)。全流程定制設(shè)計(jì),專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)
js代碼
$.ajax({ type: "GET", url: encodeURI("http://localhost:8080/a?dd=" + dayinList+"), beforeSend: function () { }, success: function (msg) { }, error: function () { } })
后臺(tái)代碼
@RestController public class LogDownloadApi { @GetMapping("/a") public void cs(@RequestParam("dd") String aa){ System.out.println(aa); } }
js代碼
$.ajax({ type : "POST", url : "http://localhost:8080/logDownload", data : JSON.stringify({ logList : dayinList,//數(shù)據(jù)List logName : "logName"//數(shù)據(jù)String }), contentType : "application/json", dataType : "json", success: function (msg) { //成功執(zhí)行 }, error: function () { //失敗執(zhí)行 } });
后臺(tái)代碼
@RestController public class LogDownloadApi { @PostMapping("/logDownload") public void ajaxPost(@RequestBody Map map){ System.out.println(map); } }
js代碼
<button class="btn btn-primary pull-right" onclick="outputResult(' + id + ')">導(dǎo)出查詢結(jié)果</button>
/*導(dǎo)出選擇,并下載*/ function outputResult(str) { var logName=document.getElementById("logName").textContent; console.log(logName) var dayinList = []; var List = $(".checkbox" + str.id + ":checked"); for (var i = 0; i < List.length; i++) { dayinList.push(List[i].value) } var xhr = new XMLHttpRequest(); xhr.open('post', 'http://localhost:8080/logDownload', true); xhr.responseType = 'blob'; xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8'); xhr.onload = function () { if (this.status == 200) { var blob = this.response; var a = document.createElement('a'); var url = window.URL.createObjectURL(blob); a.href = url; //設(shè)置文件名稱 a.download = logName+'.xls'; a.click(); } }; xhr.send(JSON.stringify({ logList : dayinList,//數(shù)據(jù)源 logName : logName//文件名 })); }
后臺(tái)POST方法
@RestController public class LogDownloadApi { private final InputExcelService inputExcelService; public LogDownloadApi(InputExcelService inputExcelService) { this.inputExcelService = inputExcelService; } @PostMapping("/logDownload") public void aa(@RequestBody Map map, @Context HttpServletResponse response, @Context HttpServletRequest request) { List list = (List) map.get("logList"); String logName = (String) map.get("logName"); String fileName = logName.substring(1, logName.length()); try { inputExcelService.exportExcel(response, request, list, fileName); } catch (Exception e) { e.printStackTrace(); } } }
package com.yx.http.service; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.*; /** * Created by gao on 2018/12/24. * 報(bào)表下載 */ @Service public class InputExcelService { private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd-hh-mm-ss"); /** * 創(chuàng)建報(bào)表 * * @param response 請(qǐng)求響應(yīng)信息 * @param request 請(qǐng)求信息 * @param logList 日志數(shù)據(jù)結(jié)果 * @param xlsName 報(bào)表名稱 * @throws Exception 拋出異常錯(cuò)誤 */ public void exportExcel(HttpServletResponse response, HttpServletRequest request, List<String> logList, String xlsName)throws Exception{ /*第一步創(chuàng)建workbook*/ HSSFWorkbook wb = new HSSFWorkbook(); /*給表格簿賦值*/ HSSFSheet sheet = wb.createSheet("日志數(shù)據(jù)"); /*設(shè)置打印頁(yè)面為水平居中*/ sheet.setHorizontallyCenter(true); /*第三步創(chuàng)建行row:添加表頭0行*/ HSSFRow row = sheet.createRow(0); /*創(chuàng)建表標(biāo)題與列標(biāo)題單元格格式*/ HSSFCellStyle style = wb.createCellStyle(); /*居中*/ style.setAlignment(HSSFCellStyle.ALIGN_CENTER); /*下邊框*/ style.setBorderBottom(HSSFCellStyle.BORDER_THIN); /*左邊框*/ style.setBorderLeft(HSSFCellStyle.BORDER_THIN); /*上邊框*/ style.setBorderTop(HSSFCellStyle.BORDER_THIN); /*右邊框*/ style.setBorderRight(HSSFCellStyle.BORDER_THIN); HSSFFont font = wb.createFont(); font.setFontHeightInPoints((short) 16); style.setFont(font); /*創(chuàng)建表格內(nèi)數(shù)據(jù)格式*/ HSSFCellStyle styleCell = wb.createCellStyle(); /*居中*/ styleCell.setAlignment(HSSFCellStyle.ALIGN_CENTER); /*下邊框*/ styleCell.setBorderBottom(HSSFCellStyle.BORDER_THIN); /*左邊框*/ styleCell.setBorderLeft(HSSFCellStyle.BORDER_THIN); /*上邊框*/ styleCell.setBorderTop(HSSFCellStyle.BORDER_THIN); /*右邊框*/ styleCell.setBorderRight(HSSFCellStyle.BORDER_THIN); HSSFFont fontCell = wb.createFont(); fontCell.setFontHeightInPoints((short) 14); styleCell.setFont(fontCell); /*創(chuàng)建下標(biāo)為0的單元格*/ row = sheet.createRow(0); /*設(shè)定值 row.createCell列名*/ HSSFCell cell = row.createCell(0); cell.setCellValue("日志數(shù)據(jù)"); cell.setCellStyle(style); for (int i = 0; i < 12; i++) { cell = row.createCell(i + 1); cell.setCellValue(""); cell.setCellStyle(style); } /*起始行,結(jié)束行,起始列,結(jié)束列*/ CellRangeAddress callRangeAddress = new CellRangeAddress(0, 0, 0, 12); sheet.addMergedRegion(callRangeAddress); /*創(chuàng)建下標(biāo)為1行的單元格*/ row = sheet.createRow(1); /*設(shè)定值*/ cell = row.createCell(0); cell.setCellValue("序號(hào)"); /*內(nèi)容居中*/ cell.setCellStyle(style); /*設(shè)定值*/ cell = row.createCell(1); cell.setCellValue("日志數(shù)據(jù)"); cell.setCellStyle(style); /*自適應(yīng)寬度*/ for (int i = 0; i <= 13; i++) { sheet.autoSizeColumn(i); sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 12 / 10); } /*第五步插入數(shù)據(jù)*/ /*創(chuàng)建行(可在此for循環(huán)插入數(shù)據(jù) row = sheet.createRow(i + 2))*/ for (int i = 0; i < logList.size(); i++) { row = sheet.createRow(2+i); //創(chuàng)建單元格并且添加數(shù)據(jù) cell = row.createCell(0); cell.setCellValue(i+1); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue(logList.get(i)); cell.setCellStyle(style); } String fileName = xlsName + ".xls"; String rtn = ""; fileName = URLEncoder.encode(fileName, "UTF8"); String userAgent = request.getHeader("User-Agent"); /*針對(duì)IE或者以IE為內(nèi)核的瀏覽器:*/ if (userAgent != null) { userAgent = userAgent.toLowerCase(); /*IE瀏覽器,只能采用URLEncoder編碼*/ if (userAgent.indexOf("msie") != -1) { rtn = "filename=\"" + fileName + "\""; } /*Opera瀏覽器只能采用filename**/ else if (userAgent.indexOf("opera") != -1) { rtn = "filename*=UTF-8''" + fileName; } /*Safari瀏覽器,只能采用ISO編碼的中文輸出*/ else if (userAgent.indexOf("safari") != -1) { try { rtn = "filename=\"" + new String(fileName.getBytes("UTF-8"), "ISO8859-1") + "\""; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /*Chrome瀏覽器,只能采用MimeUtility編碼或ISO編碼的中文輸出*/ // else if (userAgent.indexOf("applewebkit") != -1) { // fileName = MimeUtility.encodeText(fileName, "UTF8", "B"); // rtn = "filename=\"" + fileName + "\""; // } /* FireFox瀏覽器,可以使用MimeUtility或filename*或ISO編碼的中文輸出*/ else if (userAgent.indexOf("mozilla") != -1) { rtn = "filename*=UTF-8''" + fileName; } } response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;" + rtn); response.setCharacterEncoding("UTF-8"); OutputStream outputStream = null; outputStream = response.getOutputStream(); wb.write(outputStream); outputStream.flush(); outputStream.close(); } /** * 排序 * @param list * @param orderByParams * @return */ public static List<Map<String, Object>> listContainMapSortByStringAsc(List<Map<String, Object>> list, String orderByParams) { Collections.sort(list, new Comparator<Map<String, Object>>() { @Override public int compare(Map<String, Object> o1, Map<String, Object> o2) { /*name1是從你list里面拿出來(lái)的一個(gè)*/ String name1 = o1.get(orderByParams).toString(); /*name1是從你list里面拿出來(lái)的第二個(gè)name*/ String name2 = o2.get(orderByParams).toString(); return name1.compareTo(name2); } }); return list; } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
分享題目:JQuery中怎么利用Ajax導(dǎo)出報(bào)表
當(dāng)前地址:http://www.chinadenli.net/article26/piihjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、云服務(wù)器、微信公眾號(hào)、品牌網(wǎng)站制作、小程序開(kāi)發(fā)、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)