JQuery 中怎么利用Ajax 导出报表
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,JQuery 中怎么利用Ajax 导出报表,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。利用Ajax发送Get请求js代
千家信息网最后更新 2026年11月02日JQuery 中怎么利用Ajax 导出报表
JQuery 中怎么利用Ajax 导出报表,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
利用Ajax发送Get请求
js代码
$.ajax({ type: "GET", url: encodeURI("http://localhost:8080/a?dd=" + dayinList+"), beforeSend: function () { }, success: function (msg) { }, error: function () { } })后台代码
@RestControllerpublic class LogDownloadApi { @GetMapping("/a") public void cs(@RequestParam("dd") String aa){ System.out.println(aa); }}利用Ajax发送POST请求
js代码
$.ajax({ type : "POST", url : "http://localhost:8080/logDownload", data : JSON.stringify({ logList : dayinList,//数据List logName : "logName"//数据String }), contentType : "application/json", dataType : "json", success: function (msg) { //成功执行 }, error: function () { //失败执行 } });后台代码
@RestControllerpublic class LogDownloadApi { @PostMapping("/logDownload") public void ajaxPost(@RequestBody Map map){ System.out.println(map); }}利用POST请求,导出报表
js代码
/*导出选择,并下载*/ 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; //设置文件名称 a.download = logName+'.xls'; a.click(); } }; xhr.send(JSON.stringify({ logList : dayinList,//数据源 logName : logName//文件名 })); }后台POST方法
@RestControllerpublic 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. * 报表下载 */@Servicepublic class InputExcelService { private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd-hh-mm-ss"); /** * 创建报表 * * @param response 请求响应信息 * @param request 请求信息 * @param logList 日志数据结果 * @param xlsName 报表名称 * @throws Exception 抛出异常错误 */ public void exportExcel(HttpServletResponse response, HttpServletRequest request, List logList, String xlsName)throws Exception{ /*第一步创建workbook*/ HSSFWorkbook wb = new HSSFWorkbook(); /*给表格簿赋值*/ HSSFSheet sheet = wb.createSheet("日志数据"); /*设置打印页面为水平居中*/ sheet.setHorizontallyCenter(true); /*第三步创建行row:添加表头0行*/ HSSFRow row = sheet.createRow(0); /*创建表标题与列标题单元格格式*/ 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); /*创建表格内数据格式*/ 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); /*创建下标为0的单元格*/ row = sheet.createRow(0); /*设定值 row.createCell列名*/ HSSFCell cell = row.createCell(0); cell.setCellValue("日志数据"); cell.setCellStyle(style); for (int i = 0; i < 12; i++) { cell = row.createCell(i + 1); cell.setCellValue(""); cell.setCellStyle(style); } /*起始行,结束行,起始列,结束列*/ CellRangeAddress callRangeAddress = new CellRangeAddress(0, 0, 0, 12); sheet.addMergedRegion(callRangeAddress); /*创建下标为1行的单元格*/ row = sheet.createRow(1); /*设定值*/ cell = row.createCell(0); cell.setCellValue("序号"); /*内容居中*/ cell.setCellStyle(style); /*设定值*/ cell = row.createCell(1); cell.setCellValue("日志数据"); cell.setCellStyle(style); /*自适应宽度*/ for (int i = 0; i <= 13; i++) { sheet.autoSizeColumn(i); sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 12 / 10); } /*第五步插入数据*/ /*创建行(可在此for循环插入数据 row = sheet.createRow(i + 2))*/ for (int i = 0; i < logList.size(); i++) { row = sheet.createRow(2+i); //创建单元格并且添加数据 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"); /*针对IE或者以IE为内核的浏览器:*/ 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 看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
数据
浏览器
浏览
报表
代码
编码
单元
日志
边框
后台
设定值
中文
输出
下标
信息
内容
右边
名称
文件
标题
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技网
科技股互联网
科技互联网项目
互联网为科技
唐信互联网科技
互联网高科技企业
哈尔滨新华互联网科技
海南购全球互联网科技
恒银科技与互联网金融
来此购互联网科技有限公司
青岛科技大学互联网大赛研究生
深圳市优智互联网科技有限公司
青海科技产业互联网发展
华夏恒生科技与互联网科技
互联网科技如何创业
北京市互联网新科技
半导体行业属于互联网高科技类吗
易企秀互联网科技是做什么的
九思连强互联网电子科技有限公司
华人运通互联网科技公司
手磨咖啡
腾讯阿里互联网科技
科技制造互联网公司
语蝶互联网科技有限公司
深圳市优智互联网科技有限公司
西安小鹰速贷互联网科技有限公司
中和天地互联网科技有限公司
无锡工业互联网科技有限公司
本届互联网大会上的黑科技
互联网科技企业文化