ralasafe数据导出Excel.
分析:导出Excel分为两个大的步骤:1、获取表头;2、列出数据
WebRalasafe.getColumns(request, RalasafeConstants.QueryXM); 可以直接获取到Ralasafe配置列
WebRalasafe.query(request,RalasafeConstants.QueryXM, customizedWhere); 可以根据筛选条件换取到数据集合。
获取到数据集合主要是对数据进行处理
String worksheet = "项目库"; // 输出的excel文件工作表名
String realpath = request.getServletContext().getRealPath(
"\\pub\\项目库\\");
File excelfolder = new File(realpath);
if (!excelfolder.exists()){
excelfolder.mkdirs();// 创建这个文件夹
}
String excelfile = realpath+"\\项目查询.xls";
WritableWorkbook workbook = null;
OutputStream os = null;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
os = new FileOutputStream(excelfile);
workbook = Workbook.createWorkbook(os); // 创建一个excel文件
WritableSheet sheet = workbook.createSheet(worksheet, 0);
Label label = null; // jxl标签类,实例对象即对应于excel的单元格
List<org.ralasafe.db.sql.Column> fields = WebRalasafe.getColumns(
request, RalasafeConstants.QueryXM);
int i = 0;
for (org.ralasafe.db.sql.Column col : fields) {
label = new Label(i, 0, col.getTitle()); // 第一行为标题
sheet.addCell(label);
i++;
}
CustomizedWhere customizedWhere = new CustomizedWhere();
String xmbh = request.getParameter("xmbh");
String xmmc = request.getParameter("xmmc");
if (xmbh != null&&!xmbh.equals("")) {
customizedWhere.addLike("xmbh", xmbh);
}
if (xmmc != null&&!xmmc.equals("")) {
customizedWhere.addLike("xmmc", xmmc);
}
Collection<Map> c = WebRalasafe.query(request,
RalasafeConstants.QueryXM, customizedWhere);
int total = WebRalasafe.queryCount(request,
RalasafeConstants.QueryXM, customizedWhere);
// for(Object )
Iterator iter = c.iterator();
i=0;
while(iter.hasNext()){
Xm xm = (Xm) iter.next();
int j=0;
for(org.ralasafe.db.sql.Column col : fields){
Field f = xm.getClass().getDeclaredField(col.getName().toLowerCase());
f.setAccessible(true);
Object o =f.get(xm);
String content = "";
if(o!=null&&!"".equals(o)){
content= o.toString();
}
label = new Label(j, i + 1,content);
sheet.addCell(label);
j++;
}
i++;
}
workbook.write();
workbook.close();
os.close();
Map m = new HashMap();
m.put("success", true);
m.put("url","项目库/项目查询.xls");
JsonUtils.write(m, response.getWriter());