针对从数据库取大量数据导出到Excel的记录摘要
2014-04-25 22:51
561 查看
鉴于公司作为刚创业的小公司,没有技术大牛,什么逻辑业务都要靠自己摸索查询,记录下来作为自己的成长经历。由于之前项目需求导出数据不多,大多在1万条之内,因此直接将前台取到的数据用get或post方式发送到后台,直接写入Excel。如今需要提供原始数据的导出,原始数据是每隔40秒就往数据库中插入一条数据,仍用之前的方式势必导致浏览器响应奔溃,于是想到的思路是:用户点击‘下载’链接的时候,用ajax链接到后台,web.xml拦截此路径,在servlet中将数据从数据库全部取出写入Excel,存放到项目工程中新建的文件夹中,前台用<a>标签链接到此Excel资源。1、前台链接a标签
<a href='#' id='originalExport' onClick='return originalExport(this)'>导出数据</a>2、用ajax链接到后台,将需要的数据绑定一起传递
function originalExport(obj){ var store = $("#storeOriginal option:selected").text(); var sID = $('#storeOriginal').val(); var accID = ''; if($("#accOriginal").length > 0){ accID = $("#accOriginal").val(); } $.ajax({ type:"post", async:false, //为防止还未完Excel结果就响应回来,采用同步请求 url:"./js/exportOriginalData", data:{store:store,sID:sID,accID:accID,startTime:sdate,endTime:edate}, success:function(data){ obj.href = "/tts-dunan/fileTemp/"+data+""; //链接到新建的文件夹中的生成的Excel文件 } }); return true; }3、web.xml拦截路径
<servlet> <servlet-name>exportOriginal</servlet-name> <servlet-class>com.tusung.tts.comm.tool.OriginalDataExport</servlet-class> </servlet> <servlet-mapping> <servlet-name>exportOriginal</servlet-name> <url-pattern>/js/exportOriginalData</url-pattern> </servlet-mapping>4、servlet中处理业务
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); //取到原始数据集合 String store = req.getParameter("store"); store = URLDecoder.decode(store, "UTF-8"); String sID = req.getParameter("sID"); String accID = req.getParameter("accID"); int startTime = Integer.parseInt(req.getParameter("startTime")); int endTime = Integer.parseInt(req.getParameter("endTime")); String accountID = StringTools.isBlank(accID)?(String)req.getSession().getAttribute("account"):accID; List<EventDataExt> ede_list = new ArrayList<EventDataExt>(); ede_list = JDBCUtil.getOriginalDataById(accountID, sID, startTime, endTime); //指定存放路径 String fileRealPath = ""; //存放到项目路径下的fileTemp文件夹下 String savePath = this.getServletConfig().getServletContext().getRealPath("/") + "fileTemp\\"; File file = new File(savePath); if (!file.isDirectory()) { file.mkdirs(); } //写入文件 try{ resp.setContentType("application/vnd.ms-excel");//保证不乱码 SimpleDateFormat df=new SimpleDateFormat("yyyyMMddHHmmss"); String time = df.format(new Date()); String fileName=time+"导出原始数据.xls"; fileRealPath = savePath + fileName; WritableWorkbook wwb = Workbook.createWorkbook(new File(fileRealPath)); WritableSheet ws = wwb.createSheet("原始数据",0); ws.mergeCells(0,0,1,0);//合并单元格 //向合并单元格写数据 WritableFont font = new WritableFont(WritableFont.TIMES, 12 ,WritableFont.BOLD); WritableCellFormat format = new WritableCellFormat(font); format.setAlignment(jxl.format.Alignment.CENTRE); //单独设置居中显示 WritableFont wfont = new WritableFont(WritableFont.ARIAL, 10); WritableCellFormat wformat = new WritableCellFormat(wfont); wformat.setAlignment(jxl.format.Alignment.CENTRE); wformat.setBorder(Border.ALL,BorderLineStyle.THIN); Label label = new Label(0,0,store+"原始数据",format); ws.addCell(label); Blank b = new Blank(0,1); ws.addCell(b); b = new Blank(0,2); ws.addCell(b); CellView cellView = new CellView(); cellView.setAutosize(true); label = new Label(0,1,"日期",wformat);//列、行、内容、格式 ws.addCell(label); ws.setColumnView(1,12); label = new Label(1,1,"能耗(度)",wformat); ws.addCell(label); int count = 2; Number labelN = null; for(int i=0;i<ede_list.size();i++){ EventDataExt ede = (EventDataExt) ede_list.get(i); double energy = ede.getEnergy(); long energy_time = ede.getTimestamp(); df=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String sd = df.format(new Date((energy_time*1000))); ws.setColumnView(0,cellView); label = new Label(0,count,sd,wformat); ws.addCell(label); labelN = new Number(1,count,energy,wformat); ws.addCell(labelN); count++; } resp.setCharacterEncoding("UTF-8"); resp.getWriter().print(fileName);//将文件名字送回页面 wwb.write(); wwb.close(); }catch(Exception e){ e.printStackTrace(); } }最后导出的Excel如图:遇到的问题:由于项目后台是采用Springmvc,开始在servlet直接使用注解方式调用dao层查询数据的时候,产生异常:javax.naming.NamingException: Cannot create resource instance在google搜索发现应当是Tomcat7.0.35之前一个纰漏,原文链接如下:https://issues.apache.org/bugzilla/show_bug.cgi?id=54448
相关文章推荐
- winform保存二进制图片到oracle
- sql优化(oracle)
- 数据库事务中的脏读,虚读和不可重复读
- mysql数据库主从备份
- MySQL字符集 GBK、GB2312、UTF8区别 解决 MYSQL中文乱码问题
- 数据库事务中的脏读,虚读和不可重复读
- Exchange Server 2010 LAB Part7.邮箱数据库可用性组DAG
- 多角度彻底理解数据库事务中的脏读,虚读和不可重复读
- Oracle 用户和权限
- 提高mysql 批量插入和更新性能
- oracle在导入时指定不导入某张表
- Oracle删除当前用户下所有的表的方法
- Oracle删除当前用户下所有的表的方法
- Oracle删除当前用户下所有的表的方法
- Oracle删除当前用户下所有的表的方法
- SQL Server 约束
- 非常棒的My SQL.
- sql server 如何通过mdf文件和ldf文件恢复数据库
- oracle中PCTFREE, PCTUSED, INITRANS, MAXTRANX参数
- Oracle数据库程序设计学习笔记