数据迁出
2015-12-29 10:56
465 查看
1.jsp
2.js
3.java
<button type="button" id="exportBtn1" class="btn btn-mini btn-primary" rel="tooltip" data-placement="top" data-original-title="导出模版" data-html="true"> <i class="fa fa-download">导出模版</i> </button>
2.js
//导出.xsl按钮 $("#exportBtn1").click(function(){ $.SmartMessageBox({ title:"系统提示:", content:"<br><br>正在下载数据,导出完毕后点击确认关闭", buttons:"[确认]" }) location.href = "../../xxx/exportFile.action"; })
3.java
//导出 public String exportFile() throws Exception{ //导出数据 //String filename = assetsDetectService.exportData(planId,type); //导出模版 String filename = flowDAO.exportTemplate(); downloadFile(filename, false); return NONE; } /** * 下载文件 * * @param del * ,是否需要删除,如果是true那么下载之后将删除文件 * @throws IOException */ protected void downloadFile(String path, boolean del) throws IOException { HttpServletResponse response = getResponse(); response.setCharacterEncoding("gb2312"); response.reset();// response.setContentType("application/x-download"); int last1 = path.lastIndexOf("\\"); int last2 = path.lastIndexOf("/"); String filedisplay = path.substring((last1 > last2 ? last1 : last2) + 1); filedisplay = StrConvert.convert(filedisplay, "gbk", "iso-8859-1"); response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay); OutputStream outp = null; FileInputStream in = null; java.io.File fp = new java.io.File(path); try { outp = response.getOutputStream(); in = new FileInputStream(fp); byte[] b = new byte[1024]; int i = 0; while ((i = in.read(b)) > 0) { // Debug.println("1024个字节溜出"); outp.write(b, 0, i); } outp.flush(); in.close(); outp.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { in.close(); in = null; } if (outp != null) { outp.close(); outp = null; } if (del) { fp.delete(); } } }实现层
//导出模版 public String exportTemplate(){ File file = new File(this.path); if(!file.exists()) file.mkdirs(); String filename= path + (new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss")).format(new Date())+".xls"; try { WritableWorkbook wb = Workbook.createWorkbook(new File(filename)); WritableSheet sheet = wb.createSheet("sheet1",0); //为(0,0),(0,1)单元格写入内容 String tableHead = "x:"; String tableHead2 = "xx"; sheet.addCell(new Label(0,0,tableHead)); sheet.addCell(new Label(1,0,tableHead2)); wb.write(); wb.close(); } catch(Exception e) { e.printStackTrace(); return null; } return filename; }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树