//===============================控制層============================================
@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請求,響應給瀏覽器要用流的方式,不能用如上方式。
javascript