后台方法1:
@RequestMapping(value="/SMSLogExport")
public void SMSLogExport(HttpServletRequest request,HttpServletResponse response) throws IOException{
HttpSession session = request.getSession();
int comType = -1;
comType = (Integer)session.getAttribute("comType");
int operType = -1;
operType = (Integer)session.getAttribute("operType");
Log log = new Log();
log.setComType(comType);
log.setOperType1(operType);
log.setCompanyNo((String)session.getAttribute("companyNo"));
String oper = request.getParameter("oper");
if(oper!=null&&!oper.trim().equals("")){
log.setOper(oper.trim());
}
String type = request.getParameter("type");
String start = request.getParameter("start");
String end = request.getParameter("end");
if(start==null||start.trim().equals("")) start="0";
if(end==null||end.trim().equals("")) end="10000";
int start1 = Integer.parseInt(start);
int end1 = Integer.parseInt(end);
log.setStart(start1<0?0:start1);
log.setEnd(end1<0?0:end1);
String operSTime = request.getParameter("operSTime");
String operETime = request.getParameter("operETime");
log.setOperSTime(operSTime);
log.setOperETime(operETime);
if(type==null||type.equals("")){
log.setType(-1);
}else{
log.setType(Integer.parseInt(type));
}
List<Log> list = new ArrayList<Log>();
list = qd.querySMSLogExport(log);
List<Log> list1 = new ArrayList<Log>();
if(list==null||list.size()==0) return;
list1.add(new Log());
list1.addAll(list);
Map<String, String> zd6 = new HashMap<String, String>();
zd6.put("0id", ReadProperties.getValue("id"));//属性前边的数字代表字段的先后顺序。
zd6.put("1phone", ReadProperties.getValue("phone"));//属性前边的数字代表字段的先后顺序。
zd6.put("2typeName", ReadProperties.getValue("type"));
zd6.put("3msgContent", ReadProperties.getValue("content"));
zd6.put("4operTime", ReadProperties.getValue("operTime"));
Map data = new HashMap();
data.put("listData", list1);
data.put("ziDuan", zd6);
String path1 =ReadProperties.getValue("frame.menu.Delaylog")+" "+df.format(new java.util.Date())+".xls";
String dir = System.getProperty("user.dir");
dir.replaceAll("\\\\", "\\\\\\\\");
String path = dir+path1;
HSSFWorkbook wb = new ExportExcel().objListToExcel1(path, data);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename="+path1);
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}
生成excel方法2:
@SuppressWarnings("unchecked")
public HSSFWorkbook objListToExcel1(String name, Map data) {
Map<String, String> ziDuan = (Map<String, String>) data.get("ziDuan");
List listData = (List) data.get("listData");
Object[] keys = ziDuan.keySet().toArray();
String[] ziDuanKeys = new String[keys.length];
for (int k = 0; k < keys.length; k++) {
String temp = keys[k].toString();
int xuHao = 0;
String s = temp.substring(0, 2);
if(Tool.isNum(s)){
xuHao = Integer.valueOf(s);
ziDuanKeys[xuHao] = temp.substring(2);
}else{
xuHao = Integer.valueOf(temp.substring(0, 1));
ziDuanKeys[xuHao] = temp.substring(1);
}
}
try {
// String path = name;
// File newFile = new File(path);
// newFile.createNewFile();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
for (int i = 0; i < listData.size(); i++) {
HSSFRow row = sheet.createRow(i);
Object obj = listData.get(i);
for (int j = 0; j < ziDuanKeys.length; j++) {
HSSFCell cell = row.createCell(j);
if (i == 0) {
sheet.setColumnWidth(j, 6000);
cell.setCellValue(new HSSFRichTextString(ziDuan.get(j
+ ziDuanKeys[j])));
} else {
String ziDuanName = (String) ziDuanKeys[j];
ziDuanName = ziDuanName.replaceFirst(ziDuanName
.substring(0, 1), ziDuanName.substring(0, 1)
.toUpperCase());
ziDuanName = "get" + ziDuanName;
Class clazz = Class.forName(obj.getClass().getName());
Method[] methods = clazz.getMethods();
Pattern pattern = Pattern.compile(ziDuanName);
Matcher mat = null;
for (Method m : methods) {
mat = pattern.matcher(m.getName());
if (mat.find()) {
Object shuXing = m.invoke(obj, null);
if (shuXing != null) {
cell.setCellValue(shuXing.toString());//这里可以做数据格式处理
} else {
cell.setCellValue("");
}
break;
}
}
}
}
}
// OutputStream out = new FileOutputStream(path);
// wb.write(out);// 写入File
// out.flush();
// out.close();
return wb;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}