概述
js部分:
var jsonstr=getSearchSel(os);
var str = "";//传参数
for(var i in jsonstr){
if(jsonstr[i]!=""){
str += i+"="+jsonstr[i]+"&";
}
}
str = str.substr(0, str.length - 1);
var url = "/QAdevices/ExcelServlet?" + str;
window.open(url);
java端:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
StringBuilder jsonstr = new StringBuilder();//转化成json
jsonstr.append("{");
String reqstr = request.getQueryString();//获取请求参数
if(!"".equals(reqstr) &&reqstr !=null){
String [] stringArr= reqstr.split("&");
for(int i = 0;i<stringArr.length;i++){
String [] strArr = stringArr[i].split("=");
jsonstr.append("""+strArr[0]+"":"+"""+strArr[1]+"""+",") ;
}
jsonstr.deleteCharAt(jsonstr.length()-1);
}
jsonstr.append("}");
String filename =getExcelPath() ;
List<Map<String, Object>> searchlist = ServerInit.managerAction.searchDeviceInfoByConditions3(jsonstr.toString());
if(searchlist!=null){
HSSFWorkbook wb = createExcel(searchlist);
try{ //写入浏览器
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
OutputStream outputStream=response.getOutputStream();
wb.write(outputStream);
outputStream.flush();
outputStream.close();
}catch(Exception e){
logger.error("Exception",e);
}
}
}
/**
* 返回excel表生成的路径
* 参数
* os,devicestatue,peopleId
* @return excel表的服务器存储地址
* */
public String getExcelPath() {
//String str = this.getServletContext().getRealPath("/WEB-INF");
List<Map<String, Object>> searchlist = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String timestr = df.format(new Date()).toString().replace(" ", "-");
timestr = timestr.replace(":","");
String filename = "设备一览表-"+ timestr+".xls";
logger.info("下载:"+filename);
return filename;
}
/**
* 创建excel表
* 参数
* 数据库查询数据list,存储路径
* @return boolean true:生成成功 false: 生成失败
* @throws
* */
public HSSFWorkbook createExcel(List<Map<String, Object>> list) {
boolean bool = true;
// 第一步,创建一个webbook,对应一个Excel文件
//XSSFWorkbook wb = new XSSFWorkbook();
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("设备信息");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
ArrayList<String> title = new ArrayList<>(Arrays.asList("编号","设备编号","品牌名称","品牌","系统","系统版本","cpu","cpu型号","内存大小","屏幕尺寸","分辨率高","分辨率宽","imei1","imei2","sn","是否越狱","颜色","前置摄像头","后置摄像头","数据线","充电器","耳机","电池","手机壳","入库时间","设备管理员","借用人","状态","备注"));
// System.out.println(title.size());
HSSFCell cell;
for(int i = 0 ;i<title.size();i++){
cell = row.createCell((short) i);
cell.setCellValue(title.get(i));
}
HSSFRow row1 = sheet.createRow((int) 1);
ArrayList<String> title2 = new ArrayList<>(Arrays.asList("必填","必填","必填","必填","android/ios","必填","厂商","厂商+型号","","","","","必填","","","0=否,1=是","","单位万","单位万","0=否,1=是","0=否,1=是","0=否,1=是","0=否,1=是","0=否,1=是","","","","状态(0完好 1破损 2老旧 3待修复 4已坏)",""));
// System.out.println(title2.size());
for(int i = 0 ;i<title2.size();i++){
cell = row1.createCell((short) i);
cell.setCellValue(title2.get(i));
}
HSSFRow row2;
HSSFCell cell2;
int i = 0;
int j = 0;
for (Map<String, Object> map : list) {//填充数据
row2 = sheet.createRow((int) i+2);
List<String> datalist = getData(map);
for( j = 0 ;j<datalist.size();j++){
cell2 = row2.createCell((short) j);
cell2.setCellValue(datalist.get(j));
}
i++;
}
return wb;
}
/**
* 处理数据库查询出的数据,按照excel 列的顺序
* 参数
* Map<String , Object>一行数据
* @return datalist
* */
public List<String> getData(Map<String, Object> map){
List<String> stringList = new ArrayList<String>();
for(Map.Entry<String,Object> entry : map.entrySet()){
if(entry.getValue() == null){
entry.setValue("");
}
if(entry.getValue() instanceof Boolean){//false:0 true:1
if((Boolean)entry.getValue()){
entry.setValue(1);
}
else {
entry.setValue(0);
}
}
}
stringList.add(map.get("deviceid").toString());
stringList.add(map.get("tag").toString());
stringList.add(map.get("model").toString());
stringList.add(map.get("brand").toString());
stringList.add(map.get("os").toString());
stringList.add(map.get("osversion").toString());
stringList.add(map.get("cpu").toString());
stringList.add(map.get("cpumodel").toString());
stringList.add(map.get("mem").toString());
stringList.add(map.get("screensize").toString());
stringList.add(map.get("resolution_high").toString());
stringList.add(map.get("resolution_width").toString());
stringList.add(map.get("imei1").toString());
stringList.add(map.get("imei2").toString());
stringList.add(map.get("sn").toString());
stringList.add(map.get("root").toString());
stringList.add(map.get("color").toString());
stringList.add(map.get("frontcam").toString());
stringList.add(map.get("backcam").toString());
stringList.add(map.get("usbcable").toString());
stringList.add(map.get("charger").toString());
stringList.add(map.get("headset").toString());
stringList.add(map.get("battery").toString());
stringList.add(map.get("phoneshell").toString());
stringList.add(map.get("adddate").toString());
stringList.add(map.get("managername").toString());
stringList.add(map.get("name").toString());
stringList.add(map.get("devicestatus").toString());
stringList.add(map.get("devicenotes").toString());
return stringList;
}
}
最后
以上就是难过春天为你收集整理的java 生成excel表传给前端下载的全部内容,希望文章能够帮你解决java 生成excel表传给前端下载所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复