项目中需要将结果集保存到一个excel表格中, 下面是我的实现方法
先要把jxl.jar包下载copy到classpath路径下
public class SpcpExcel {
//os应为response.getOutputStream(),list为需要写入表格的数据集,
public static void writeExcel(OutputStream os, List list) throws Exception {
WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet("sheet1",0);//设置表单的名称
if(list!=null){
Label labela = new Label(0,0,"栏目名称"); //零列零行
ws.addCell(labela);
Label labelc = new Label(1,0,"访问次数");
ws.addCell(labelc);
Label labelb = new Label(2,0,"访问流量");
ws.addCell(labelb);
HashMap hm = null;
for (int i = 0; i < list.size(); i++) {
hm = (HashMap) list.get(i);
String tab = hm.get("csdm").toString();
//之前为了表现层次关系将数据取出后加了些 
Label labelC = new Label(0,(i+1),tab.replaceAll(" "," "));
ws.addCell(labelC);
Label labelE = new Label(1,(i+1),hm.get("csdj").toString());
ws.addCell(labelE);
Label labelD = new Label(2,(i+1),hm.get("cscs").toString());
ws.addCell(labelD);
}
}
wwb.write();//写入Exel工作表
wwb.close();//关闭Excel工作薄对象
}
}