java jxl导出EXCEL

来源:互联网 发布:mac双系统win无法上网 编辑:程序博客网 时间:2024/05/01 19:57

导出excel的action方法,我这里是直接调用接口过来的JSON数据需用自己解析


/** * 设备导出excel *  * @return */public String exportDevDataExcel() {OutputStream os;try {String fileName="设备管理";String outFileName = new String(fileName.getBytes("GBK"), "ISO8859_1");os = response.getOutputStream();response.reset();response.setContentType("application/vnd.ms-excel");// 设置生成的文件类型response.setHeader("Content-disposition", "attachment; filename="+ outFileName + ".xls");WritableWorkbook workbook = Workbook.createWorkbook(os);// 建立excel文件WritableSheet wsheet = workbook.createSheet("设备", 0); // // 工作表名称// 设置Excel字体WritableFont wfont = new WritableFont(WritableFont.ARIAL, 15,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);WritableCellFormat titleFormat = new WritableCellFormat(wfont);titleFormat.setAlignment(Alignment.CENTRE);// 水平居中显示titleFormat.setVerticalAlignment(VerticalAlignment.CENTRE);// 垂直居中显示wsheet.mergeCells(0, 0, 6, 2);// 合并单元格,从0开始titleFormat.setBorder(Border.ALL, BorderLineStyle.THIN); // 设置边框线wsheet.addCell(new Label(0, 0, fileName, titleFormat));// 填写工作表标题String[] title = { "序号", "设备名称", "设置Mac地址", "库存状态", "在线状态", "在线用户","在线时长" };// 设置Excel表头for (int i = 0; i < title.length; i++) {WritableFont font = new WritableFont(WritableFont.ARIAL, 10,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);// 定义字体WritableCellFormat titleWritableFormat = new WritableCellFormat(font);// 定义格式化对象titleWritableFormat.setAlignment(Alignment.CENTRE);// 水平居中显示titleWritableFormat.setVerticalAlignment(VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中titleWritableFormat.setWrap(true);// 自动换行// wsheet.mergeCells(i, 3, i, 4);//合并单元格wsheet.setColumnView(i, 13);// 设置列宽// wsheet.setRowView(3,12);// 设置行高titleWritableFormat.setBorder(Border.ALL, BorderLineStyle.THIN); // 设置边框线Label excelTitle = new Label(i, 3, title[i],titleWritableFormat);// System.out.println("Excel  title = " + title[i]);wsheet.addCell(excelTitle);}//获取JSON数据(---这里也换成java操作数据查询的LIST等等)json = this.getDevListMethod(agencyName, keyword, devName, devSn, modelId, modelName, versionName, stock, online, shopName, maintainerName);JSONObject dataJson = JSONObject.fromObject(json);JSONArray dataArray =JSONArray.fromObject(dataJson.get("data"));WritableCellFormat rowFormat = new WritableCellFormat();// 定义格式化对象rowFormat.setAlignment(Alignment.CENTRE);// 水平居中显示// rowFormat.setVerticalAlignment(VerticalAlignment.CENTRE);//把垂直对齐方式指定为居中// rowFormat.setWrap(true);//设置自动换行rowFormat.setBorder(Border.ALL, BorderLineStyle.THIN);int c = 3; // 用于循环时Excel的行号for (int i = 1; i < dataArray.size(); i++) {c++;Label content0 = new Label(0, c, String.valueOf(i ), rowFormat);wsheet.addCell(content0);Label content2 = new Label(1, c, dataArray.getJSONObject(i).getString("name"), rowFormat);wsheet.addCell(content2);Label content3 = new Label(2, c, dataArray.getJSONObject(i).getString("sn"), rowFormat);wsheet.addCell(content3);String stock = "未绑定"; if(dataArray.getJSONObject(i).getInt("stock")==1){ stock = "未绑定"; }else if (dataArray.getJSONObject(i).getInt("stock")==2) { stock = "绑定";}else if(dataArray.getJSONObject(i).getInt("stock")==3) {stock = "调拨";}else {stock = "其他";};Label content4 = new Label(3, c,stock, rowFormat);wsheet.addCell(content4);String online = "离线"; if(dataArray.getJSONObject(i).getInt("online")==1){ online = "在线"; }else { online = "离线";};Label content5= new Label(4, c,online, rowFormat);wsheet.addCell(content5);Integer  users=dataArray.getJSONObject(i).getInt("users");Label content6= new Label(5, c,users.toString() ,rowFormat);wsheet.addCell(content6);Integer  duration=dataArray.getJSONObject(i).getInt("duration");Label content7= new Label(6, c,duration.toString(),rowFormat);wsheet.addCell(content7);}workbook.write(); // 写入文件workbook.close();os.close();} catch (WriteException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {e.printStackTrace();}return null;}


0 0
原创粉丝点击