exp4me 用java做的实用的csv导出程序 - 名传无线.freeness.yang
支持格式:csv
提供的方法:
| 數(shù)據(jù)導(dǎo)出請求 傳入?yún)?shù): 1.傳入JDAction(數(shù)據(jù)庫交互組件)支持的json格式的查詢條件 2.傳入需要導(dǎo)出的列 3.傳入導(dǎo)出的格式(目前只支持csv) 4.持續(xù)交互超時時間(S) 5.傳入導(dǎo)出文件要保存的物理路徑 返回導(dǎo)出后數(shù)據(jù)的文件名 |
| 導(dǎo)出進度輪詢 傳入導(dǎo)出請求時返回的文件名,返回當前導(dǎo)出請求的進度 |
?
使用方法.
1.首先調(diào)用數(shù)據(jù)導(dǎo)出請求 ? 傳入相應(yīng)的參數(shù),得到導(dǎo)出文件的下載地址
2.按時輪詢導(dǎo)出進度查詢方法,查出導(dǎo)出的進度,顯示進度條
(如超過數(shù)據(jù)導(dǎo)出請求時傳入的持續(xù)交互超時時間,還沒有再一次訪問導(dǎo)出進度查詢方法,數(shù)據(jù)導(dǎo)出組件則會認為本次導(dǎo)出已中斷,自動終止本次導(dǎo)出請求)
?
ps:
這里面用到了
JDAction,數(shù)據(jù)庫交互工具包,以json格式和數(shù)據(jù)庫交互,基于oracle
JO, json操作工具包
?
Export.java
/*** 導(dǎo)出用戶調(diào)用此接口,請求導(dǎo)出文件傳入?yún)?shù):1.JDAction(數(shù)據(jù)庫交互組件)支持的json格式的查詢條件2.需要導(dǎo)出的列 格式:(列名:[導(dǎo)出時候的列名],列名:[導(dǎo)出時候的列名])中括號里的是可選值3.導(dǎo)出的格式(目前只支持csv)4.持續(xù)交互超時時間(S)5.導(dǎo)出文件要保存的物理路徑返回導(dǎo)出后數(shù)據(jù)的文件名*/public static String exp(String query,String columns,String format,int outtime,String file, java.sql.Connection conn){/**驗證參數(shù)**/if ("".equals(query)) { return "false,查詢條件不能為空"; }if (!"csv".equalsIgnoreCase(format)) { return "false,只支持csv格式"; }if (outtime <= 0) { outtime = 5; }//持續(xù)交互超時時間默認為5S /**生成文件名**/String fileName = java.util.UUID.randomUUID().toString() + ".csv";/**保存請求數(shù)據(jù)**/data.ExpEntity entity = new data.ExpEntity(query,columns,format,outtime,file + "/" + fileName,System.currentTimeMillis(),System.currentTimeMillis());data.RequestData.map.put(fileName,entity);System.out.println("保存請求數(shù)據(jù):" + fileName);System.out.println("data:" + entity.toString());/**啟動導(dǎo)出線程**/entity.setExpstart(true);new thread.ExpThread(entity, conn).start();System.out.println("文件:" + entity.toString() + ",導(dǎo)出線程已啟動");/**返回下載地址**/return fileName;}public static String getExpSchedule(String fileName) {org.dom4j.Element root = org.dom4j.DocumentHelper.createElement("root");root.addAttribute("return","0");try {data.ExpEntity entity = data.RequestData.map.get(fileName);if (entity == null) {root.addAttribute("return","1");root.setText("無記錄");return root.asXML();}entity.setEndGetScheduleTime(System.currentTimeMillis());root.setText(entity.getSum() + "," + entity.getExped());return root.asXML();} catch (Exception e) {root.addAttribute("return","9");root.setText("程序異常:" + e);return root.asXML();}}?
ExpThread.java
/*** 數(shù)據(jù)導(dǎo)出線程*/public void run() {au.com.bytecode.opencsv.CSVWriter writer = null;try {/**查詢數(shù)據(jù)并寫入文件&記錄進度**/java.io.File file = new java.io.File(entity.getFile());file.createNewFile();writer = new au.com.bytecode.opencsv.CSVWriter(new java.io.FileWriter(file), ',');//查詢json.JO jp = new json.JO(entity.getQuery());json.JO ret = db.JDAction.execute(conn, jp);java.util.List<json.JO> l = ret.getJOArray("rows");//set總數(shù)entity.setSum(ret.I("totalcount"));/**寫入表頭**/Object columns[] = null;if (org.apache.commons.lang.StringUtils.isNotBlank(entity.getColumns())) {columns = entity.getColumns().split(",");} else {columns = l.get(0).keySet().toArray();}String[] title = new String[columns.length];for (int i = 0; i < columns.length; i++) {title[i] = getC((columns[i]+"").split(":"));}writer.writeNext(title);//用于判斷是否要繼續(xù)boolean isContinue = true;String contentArray[] = null;for (int i = 0; i < l.size(); i++) {contentArray = new String[columns.length];if (entity.isExpstart()) {for (int j = 0; j < columns.length; j++) {contentArray[j] = l.get(i).S((columns[j] + "").split(":")[0]);}writer.writeNext(contentArray);/**記錄進度**/entity.setExped(entity.getExped() + 1);} else {isContinue = false;break;}}//如果斷開終止了,則不必要再繼續(xù)if (!isContinue) { return; } else {if (entity.getSum() == entity.getExped()) {entity.setExpstart(false);log.info("文件:" + entity.toString() + ",導(dǎo)出線程已終止,總共耗時:XXX毫秒");return;}if ((System.currentTimeMillis() - entity.getEndGetScheduleTime()) > (entity.getOuttime() * 1000 + 2000)) {entity.setExpstart(false);log.info("文件:" + entity.toString() + ",導(dǎo)出線程已終止,總共耗時:XXX毫秒");return;} else {log.info("文件:" + entity.toString() + ",導(dǎo)出線程已耗時:XXX毫秒");}}int pagecount = ret.I("pageinfo.pagecount");for (int k = 1; k < pagecount; k++) {jp.put("pagenumber", k + 1);ret = db.JDAction.execute(conn, jp);l = ret.getJOArray("rows");for (int i = 0; i < l.size(); i++) {contentArray = new String[columns.length];if (entity.isExpstart()) {for (int j = 0; j < columns.length; j++) {contentArray[j] = l.get(i).S((columns[j] + "").split(":")[0]);}writer.writeNext(contentArray);/**記錄進度**/entity.setExped(entity.getExped() + 1);} else {isContinue = false;break;}}//如果斷開終止了,則不必要再繼續(xù)if (!isContinue) { break; } else {if (entity.getSum() == entity.getExped()) {entity.setExpstart(false);log.info("文件:" + entity.toString() + ",導(dǎo)出線程已終止,總共耗時:XXX毫秒");break;}if ((System.currentTimeMillis() - entity.getEndGetScheduleTime()) > (entity.getOuttime() * 1000 + 2000)) {entity.setExpstart(false);log.info("文件:" + entity.toString() + ",導(dǎo)出線程已終止,總共耗時:XXX毫秒");break;} else {log.info("文件:" + entity.toString() + ",導(dǎo)出線程已耗時:XXX毫秒");}}}} catch (Exception e) {log.error(entity.getFile() + e);} finally {try { writer.close(); } catch (java.io.IOException e) {log.equals(entity.getFile() + "writer關(guān)閉錯:" + e);}try { if (conn != null) { conn.close(); } } catch (java.sql.SQLException e) {log.equals(entity.getFile() + "關(guān)閉連接錯:" + e);}}}?
調(diào)用例程test.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Export Demo</title><link rel="stylesheet" type="text/css" href="../jquery-easyui-1.2.6/themes/default/easyui.css"><link rel="stylesheet" type="text/css" href="../jquery-easyui-1.2.6/themes/icon.css"><script type="text/javascript" src="../jquery-easyui-1.2.6/jquery-1.7.2.min.js"></script><script type="text/javascript" src="../jquery-easyui-1.2.6/jquery.easyui.min.js"></script><script>var s_file_name = '';function exp(){$('#p').progressbar('setValue', 0);$.post("test_act.jsp", {op: 'exp'},function(data){s_file_name = data.file_name + ''; test();}, "json"); }function test(){var value = $('#p').progressbar('getValue');if (value < 100){$.post("test_act.jsp", {'op': 'getexp', 's_file_name': s_file_name},function(ret){if (ret.sum == 0) { value = 0; } else {value = Math.floor((ret.exped / ret.sum) * 100);$('#msg').text(ret.sum + "條數(shù)據(jù)已導(dǎo)出,請點擊");}$('#p').progressbar('setValue', value);}, "json"); setTimeout(arguments.callee, 200);} else {$('#dlink').attr("href", '../csvfiles/' + s_file_name);$('#d').attr("style","display: block;");}}</script> </head> <body><div style="margin: 10px 0"><a href="#" class="easyui-linkbutton" onclick="exp()">EXPORT</a></div><div id="p" class="easyui-progressbar" style="width:400px; font-size: 12px;"></div><br><div id="d" style="display: none;"><label id="msg"></label><a id="dlink">下載</a></div> </body> </html>?
例程顯示效果圖
?
附完整的源碼下載連接,希望大家多多指教
期待下個版本能支持更多文件格式導(dǎo)出和指定導(dǎo)出數(shù)量
http://files.cnblogs.com/freeness/exp.rar
轉(zhuǎn)載于:https://www.cnblogs.com/freeness/archive/2012/07/04/export.html
總結(jié)
以上是生活随笔為你收集整理的exp4me 用java做的实用的csv导出程序 - 名传无线.freeness.yang的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用 Apache Lucene 搜索文
- 下一篇: ReentrantReadWriteLo