//===============================控制层============================================
@Controller
@RequestMapping("switchLog")
public class SwitchLogController extends BaseController<SwitchLog, String> {
// 引用Service开始
@Resource(name = "switchLogService")
private SwitchLogService switchLogService;
@Override
protected BaseService<SwitchLog, String> getService() {
return switchLogService;
}
/**
* 导出日志
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/queryExcel.do", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> queryExcel(HttpServletResponse response)
throws Exception {
return switchLogService.queryExcel(response);
}
}
//=========================serviceImpl层========================================================
*/
@Service("switchLogService")
public class SwitchLogServiceImpl extends BaseServiceImpl<SwitchLog, String>
implements SwitchLogService {
@Resource(name = "switchLogMapper")
private SwitchLogMapper switchLogMapper;
@Override
protected BaseMapper<SwitchLog, String> getMapper() {
return switchLogMapper;
}
/**
* 日志导出
*/
@Override
public Map<String, Object> queryExcel(HttpServletResponse response)
throws Exception {
String fileName = "交换机日志" + System.currentTimeMillis() + ".xls"; // 文件名
String sheetName = "交换机日志";// sheet名
String[] title = new String[] { "交换机名称", "交换机IP", "时间", "等级", "日志内容" };// 标题
List<SwitchLog> list = switchLogMapper.selectAll();//此处是从数据库中取出的List数据,根据实际需求进行更改
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String[][] values = new String[list.size()][];
for (int i = 0; i < list.size(); i++) {
values[i] = new String[title.length];
// 将对象内容转换成string
SwitchLog obj = list.get(i);
values[i][0] = obj.getSwitchName();
values[i][1] = obj.getSwitchIp();
values[i][2] = sdf.format(obj.getCreateTime());
values[i][3] = obj.getGradeLevel();
values[i][4] = obj.getLogContent();
}
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, values,
null);
// 将文件存到指定位置
try {
this.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return Message.getMessage("success", "成功");
}
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
//================================前台jsp&js============================================================
#jsp:
<a id="exportLog_button"
href="javascript:void(0);" plain="true" class="easyui-linkbutton"
icon="icon-set1" title="导出日志">导出日志</a>
#js:
/**
* 初始化
*/
$(function() {
// 初始化日志表格
init_switchLogDatagrid();
// 初始化操作按钮
init_clickButton();
});
/**
* 初始化各种操作按钮
*/
function init_clickButton() {
// 初始化导出日志按钮
init_exportLog_button();
}
/**
* 导出日志
*/
function init_exportLog_button() {
$('#exportLog_button').click(function() {
window.location.href = basePath + 'switchLog/queryExcel.do';
});
}
#==========================Tips==============================================================
#注意:
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
# 这种向页面返回的方法不能用ajax异步请求,不然会出现没有反应的情况。
# 如果需要用ajax请求,响应给浏览器要用流的方式,不能用如上方式。