@ResponseBody @RequestMapping(params = ("exportLog")) public void exportLog(String logdate,String rollcallruleId,HttpServletRequest request, HttpServletResponse response) throws RunanException { String fileName = String.valueOf("Business ledger log.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 (); } // download excel in the browser try { //Create an object of the file to be downloaded (the parameter is the path of the file to be downloaded on the server) File serverFile=new File(request.getRealPath("info/dutylog/"+fileName)); //Set the file name to be displayed in the save window. If there is Chinese in the file name, set the character set, otherwise garbled characters will appear. In addition, write the file suffix name //This step is the most critical step. Use the setHeader() method to pop up the "Do you want to save?" dialog box. The quoted part is a fixed value, do not change it response.setHeader("Content-disposition","attachment;filename="+new String("业务台账日志".getBytes(),"ISO-8859-1")+ ".xls") ; //response.setHeader("Content-disposition","attachment;filename=Chinese People"); //excel file response.setContentType("application/vnd.ms-excel"); //Define the length/bytes of the downloaded file long fileLength=serverFile.length(); //Convert the length of the long integer file to a string String length=String.valueOf(fileLength); //Set the file length (if it is a Post request, this step is indispensable) response.setHeader("content_Length",length); //The above content is just to download an empty file //The following content is used to write the corresponding file content in the server to the empty file in the form of a stream //Get a ServletOutputStream (output stream that sends binary data to the client) object OutputStream servletOutPutStream=response.getOutputStream(); //Get an input stream object that gets the input bytes from the file myFile on the server FileInputStream fileInputStream=new FileInputStream(serverFile); byte bytes[]=new byte[1024];//Set the buffer to 1024 bytes, ie 1KB int len = 0; //Read data. The return value is the total number of bytes read into the buffer, or -1 if the end of the file is reached while((len=fileInputStream.read(bytes))!=-1) { //Write len bytes starting from subscript 0 in the specified byte array to the output stream of this file, (that is, write as much as you read) servletOutPutStream.write(bytes,0,len); } servletOutPutStream.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace (); } } /** * Export real-time statistical reports and generate excel files * @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); //create workbook WritableWorkbook workbook = Workbook.createWorkbook(os); //create page WritableSheet sheet = workbook.createSheet("Sheet1", 0); // Define the cell style ARIAL font, size 18, bold, non-italic, no underline, black 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); //first row WritableCellFormat wcf_title = new WritableCellFormat(wf_title); // cell definition //wcf_title.setBackground(jxl.format.Colour.WHITE); // Set the background color of the cell //wcf_title.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //设置边框 wcf_title.setAlignment(jxl.format.Alignment.CENTRE); // set the alignment jxl.write.Label title = new jxl.write.Label(0,0,"Counting people registration form",wcf_title); //Add merged cells, the first parameter is the starting column, the second parameter is the starting row, the third parameter is the ending column, and the fourth parameter is the ending row sheet.mergeCells(0, 0, 13, 0); sheet.addCell(title);//Add the defined cell to the worksheet //second line WritableCellFormat wcf_title1 = new WritableCellFormat(wf_title1); // cell definition wcf_title1.setAlignment(jxl.format.Alignment.CENTRE); // set the alignment jxl.write.Label title1 = new jxl.write.Label(0,1,"(including the number of people entering and leaving the dorm and workshop)",wcf_title1); //Add merged cells, the first parameter is the starting column, the second parameter is the starting row, the third parameter is the ending column, and the fourth parameter is the ending row sheet.mergeCells(0, 1, 13, 1); sheet.addCell(title1);//Add the defined cell to the worksheet //The third row 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); // cell definition wcf_ThirdRow.setAlignment(jxl.format.Alignment.LEFT); // set the alignment jxl.write.Label thirdRow = new jxl.write.Label(0,2,date,wcf_ThirdRow); //Add merged cells, the first parameter is the starting column, the second parameter is the starting row, the third parameter is the ending column, and the fourth parameter is the ending row sheet.mergeCells(0, 2, 13, 2); sheet.addCell(thirdRow);//Add the defined cell to the worksheet // fourth line WritableCellFormat wcf_table = new WritableCellFormat(wf_table); wcf_table.setAlignment (jxl.format.Alignment.CENTRE); //wcf_table.setBackground(jxl.format.Colour.GRAY_25); // Set the background color of the cell wcf_table.setBorder (jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN, jxl.format.Colour.BLACK); // sheet.setRowView(4, 500); //Set the row height to 500 for(int i=3;i<200;i++){ sheet.setRowView(i+1,500); } sheet.setColumnView(0, 15); //Set the width of the column sheet.setColumnView(4, 30); //Set the width of the column sheet.setColumnView(6, 15); //Set the width of the column sheet.setColumnView(7, 15); //Set the width of the column sheet.setColumnView(11, 30);//Set the width of the column sheet.setColumnView(13, 15);//Set the width of the column //Create specific content to display 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); // right table header 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); // right table header end // Represents the first row of excel int index = 4; // loop through the data 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()); // If the data on the left reaches 12, it will be exported to the right 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++; } //Write the created content to the output stream and close the output stream workbook.write(); workbook.close(); os.close(); }
2.jsp
$(document).ready(function() { // print log $("#exportLog").bind("click",function() { var logdate = $("#logdate").val(); var rollcallruleId = $("#rollcallruleId option:selected").val(); if(rollcallruleId==null || rollcallruleId==""){ window.parent.window.artDialog.warning("The roll call area is not empty!",function(){ }); return false; } if(logdate==null || logdate==""){ window.parent.window.artDialog.warning("Time is not empty!",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(); }); });