@ResponseBody
@RequestMapping(params = ("exportLog"))
public void exportLog(String logdate,String rollcallruleId,HttpServletRequest request, HttpServletResponse response) throws RunanException {
String fileName = String.valueOf("业务台账日志.xls");
try {
File fil = new File(request.getRealPath("info/dutylog/"+fileName));
fil.createNewFile();
FileOutputStream os = new FileOutputStream(fil);
createExcel(os,logdate,rollcallruleId);
} catch (Exception e) {
e.printStackTrace();
}
//在浏览器中下载excel
try {
//创建要下载的文件的对象(参数为要下载的文件在服务器上的路径)
File serverFile=new File(request.getRealPath("info/dutylog/"+fileName));
//设置要显示在保存窗口的文件名,如果文件名中有中文的话,则要设置字符集,否则会出现乱码。另外,要写上文件后缀名
//该步是最关键的一步,使用setHeader()方法弹出"是否要保存"的对话框,打引号的部分都是固定的值,不要改变
response.setHeader("Content-disposition","attachment;filename="+new String("业务台账日志".getBytes(),"ISO-8859-1")+ ".xls") ;
//response.setHeader("Content-disposition","attachment;filename=中文人民");
//excel文件
response.setContentType("application/vnd.ms-excel");
//定义下载文件的长度 /字节
long fileLength=serverFile.length();
//把长整形的文件长度转换为字符串
String length=String.valueOf(fileLength);
//设置文件长度(如果是Post请求,则这步不可少)
response.setHeader("content_Length",length);
//以上内容仅是下载一个空文件
//以下内容用于将服务器中相应的文件内容以流的形式写入到该空文件中
//获得一个 ServletOutputStream(向客户端发送二进制数据的输出流)对象
OutputStream servletOutPutStream=response.getOutputStream();
//获得一个从服务器上的文件myFile中获得输入字节的输入流对象
FileInputStream fileInputStream=new FileInputStream(serverFile);
byte bytes[]=new byte[1024];//设置缓冲区为1024个字节,即1KB
int len=0;
//读取数据。返回值为读入缓冲区的字节总数,如果到达文件末尾,则返回-1
while((len=fileInputStream.read(bytes))!=-1)
{
//将指定 byte数组中从下标 0 开始的 len个字节写入此文件输出流,(即读了多少就写入多少)
servletOutPutStream.write(bytes,0,len);
}
servletOutPutStream.close();
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 导出实时统计报表,生成excel文件
* @param os
* @param logdate
* @param rollcallruleId
* @throws WriteException
* @throws IOException
* @throws ParseException
*/
public void createExcel(OutputStream os,String logdate,String rollcallruleId) throws WriteException,IOException, ParseException {
Map<String,Object> param = new HashMap<String, Object>();
param.put("logdate",logdate);
param.put("rollcallruleId",rollcallruleId);
List<BusinesslogDetail> businesslogDetailList = businesslogService.getBusinesslogDetailById(param);
//创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(os);
//创建页
WritableSheet sheet = workbook.createSheet("Sheet1", 0);
// 定义单元格样式 ARIAL字体、18号、粗体、非斜体、无下划线、黑色
WritableFont wf_title = new WritableFont(WritableFont.ARIAL, 18,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableFont wf_title1 = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableFont wf_ThirdRow = new WritableFont(WritableFont.ARIAL, 16, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableFont wf_table = new WritableFont(WritableFont.ARIAL, 14,WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
//第一行
WritableCellFormat wcf_title = new WritableCellFormat(wf_title); // 单元格定义
//wcf_title.setBackground(jxl.format.Colour.WHITE); // 设置单元格的背景颜色
//wcf_title.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //设置边框
wcf_title.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
jxl.write.Label title = new jxl.write.Label(0,0,"清 点 人 数 登 记 表 ",wcf_title);
//添加合并单元格,第一个参数是起始列,第二个参数是起始行,第三个参数是终止列,第四个参数是终止行
sheet.mergeCells(0, 0, 13, 0);
sheet.addCell(title);//将定义好的单元格添加到工作表中
//第二行
WritableCellFormat wcf_title1 = new WritableCellFormat(wf_title1); // 单元格定义
wcf_title1.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
jxl.write.Label title1 = new jxl.write.Label(0,1,"(包括出入监舍、车间人数登记)",wcf_title1);
//添加合并单元格,第一个参数是起始列,第二个参数是起始行,第三个参数是终止列,第四个参数是终止行
sheet.mergeCells(0, 1, 13, 1);
sheet.addCell(title1);//将定义好的单元格添加到工作表中
//第三行
SimpleDateFormat reqDate = new SimpleDateFormat("yyyy-MM-dd");
Date parse = reqDate.parse(logdate);
SimpleDateFormat sdfs = new SimpleDateFormat("MM月dd日");
String date = sdfs.format(parse);
WritableCellFormat wcf_ThirdRow = new WritableCellFormat(wf_ThirdRow); // 单元格定义
wcf_ThirdRow.setAlignment(jxl.format.Alignment.LEFT); // 设置对齐方式
jxl.write.Label thirdRow = new jxl.write.Label(0,2,date,wcf_ThirdRow);
//添加合并单元格,第一个参数是起始列,第二个参数是起始行,第三个参数是终止列,第四个参数是终止行
sheet.mergeCells(0, 2, 13, 2);
sheet.addCell(thirdRow);//将定义好的单元格添加到工作表中
//第四行
WritableCellFormat wcf_table = new WritableCellFormat(wf_table);
wcf_table.setAlignment(jxl.format.Alignment.CENTRE);
//wcf_table.setBackground(jxl.format.Colour.GRAY_25); // 设置单元格的背景颜色
wcf_table.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK);
// sheet.setRowView(4, 500);
//设置行的高度为500
for(int i=3;i<200;i++){
sheet.setRowView(i+1,500);
}
sheet.setColumnView(0, 15); //设置列的宽度
sheet.setColumnView(4, 30); //设置列的宽度
sheet.setColumnView(6, 15); //设置列的宽度
sheet.setColumnView(7, 15); //设置列的宽度
sheet.setColumnView(11, 30);//设置列的宽度
sheet.setColumnView(13, 15);//设置列的宽度
//创建要显示的具体内容
jxl.write.Label lbDate = new jxl.write.Label(0,3,"时间",wcf_table);
sheet.addCell(lbDate);
jxl.write.Label lbYd = new jxl.write.Label(1,3,"应到",wcf_table);
sheet.addCell(lbYd);
jxl.write.Label lbOut = new jxl.write.Label(2,3,"出",wcf_table);
sheet.addCell(lbOut);
jxl.write.Label lbJoin = new jxl.write.Label(3,3,"入",wcf_table);
sheet.addCell(lbJoin);
jxl.write.Label lbDesc = new jxl.write.Label(4,3,"原因",wcf_table);
sheet.addCell(lbDesc);
jxl.write.Label lbSd = new jxl.write.Label(5,3,"实到",wcf_table);
sheet.addCell(lbSd);
jxl.write.Label lbPri = new jxl.write.Label(6,3,"负责警察",wcf_table);
sheet.addCell(lbPri);
// 右边表格表头 start
jxl.write.Label lbDate1 = new jxl.write.Label(7,3,"时间",wcf_table);
sheet.addCell(lbDate1);
jxl.write.Label lbYd1 = new jxl.write.Label(8,3,"应到",wcf_table);
sheet.addCell(lbYd1);
jxl.write.Label lbOut1 = new jxl.write.Label(9,3,"出",wcf_table);
sheet.addCell(lbOut1);
jxl.write.Label lbJoin1 = new jxl.write.Label(10,3,"入",wcf_table);
sheet.addCell(lbJoin1);
jxl.write.Label lbDesc1 = new jxl.write.Label(11,3,"原因",wcf_table);
sheet.addCell(lbDesc1);
jxl.write.Label lbSd1 = new jxl.write.Label(12,3,"实到",wcf_table);
sheet.addCell(lbSd1);
jxl.write.Label lbPri1 = new jxl.write.Label(13,3,"负责警察",wcf_table);
sheet.addCell(lbPri1);
// 右边表格表头end
// 表示excel的第几行
int index = 4;
//遍历数据
for (int j = 0; j < businesslogDetailList.size(); j++) {
if(j>23){
break;
}
SimpleDateFormat sdf = new SimpleDateFormat("HH时mm分");
String d = sdf.format(businesslogDetailList.get(j).getLogtime());
// 如果左边数据到达12条就导出到右边
if(j>11){
if(j==12){
index = 4;
}
sheet.addCell(new jxl.write.Label(7,index, d,wcf_table));
sheet.addCell(new jxl.write.Label(8,index, businesslogDetailList.get(j).getChecknum()+"",wcf_table));
sheet.addCell(new jxl.write.Label(9,index, businesslogDetailList.get(j).getOutnum()+"",wcf_table));
sheet.addCell(new jxl.write.Label(10,index, businesslogDetailList.get(j).getInnum()+"",wcf_table));
sheet.addCell(new jxl.write.Label(11,index, businesslogDetailList.get(j).getReason(),wcf_table));
sheet.addCell(new jxl.write.Label(12,index, businesslogDetailList.get(j).getLivenum()+"",wcf_table));
sheet.addCell(new jxl.write.Label(13,index, businesslogDetailList.get(j).getPoliceNumber(),wcf_table));
}else{
sheet.addCell(new jxl.write.Label(0,index, d,wcf_table));
sheet.addCell(new jxl.write.Label(1,index, businesslogDetailList.get(j).getChecknum()+"",wcf_table));
sheet.addCell(new jxl.write.Label(2,index, businesslogDetailList.get(j).getOutnum()+"",wcf_table));
sheet.addCell(new jxl.write.Label(3,index, businesslogDetailList.get(j).getInnum()+"",wcf_table));
sheet.addCell(new jxl.write.Label(4,index, businesslogDetailList.get(j).getReason(),wcf_table));
sheet.addCell(new jxl.write.Label(5,index, businesslogDetailList.get(j).getLivenum()+"",wcf_table));
sheet.addCell(new jxl.write.Label(6,index, businesslogDetailList.get(j).getPoliceNumber(),wcf_table));
}
index++;
}
//把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
os.close();
}
2.jsp
$(document).ready(function() {
//打印日志
$("#exportLog").bind("click",function() {
var logdate = $("#logdate").val();
var rollcallruleId = $("#rollcallruleId option:selected").val();
if(rollcallruleId==null || rollcallruleId==""){
window.parent.window.artDialog.warning("点名区域不为空!",function(){ });
return false;
}
if(logdate==null || logdate==""){
window.parent.window.artDialog.warning("时间不为空!",function(){ });
return false;
}
var $iframe = $('#downIframe')
var url = "${base}/info/businesslog.do?exportLog&logdate="+logdate+"&rollcallruleId="+rollcallruleId+"&t="+new Date().getTime();
if($iframe.length == 0){
$iframe = $('<iframe id="downIframe" src="'+url+'"></iframe>');
$iframe.appendTo('body');
$iframe.hide();
}else{
$iframe.attr('src',url);
}
});
$("#closeBnt").bind("click", function() {
var api = frameElement.api;
api.close();
});
});