jsp導(dǎo)出excel并支持分sheet導(dǎo)出的方法
更新時間:2015年10月24日 11:51:10 作者:姜宏杰
這篇文章主要介紹了jsp導(dǎo)出excel并支持分sheet導(dǎo)出的方法,涉及Java針對excel操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了jsp導(dǎo)出excel并支持分sheet導(dǎo)出的方法。分享給大家供大家參考,具體如下:
public DownloadFile exportToExcel() throws Exception{ String config_value = systemConfigService.getConfigValueByKey("Export.Xls"); logger.info("獲取的導(dǎo)出類型為execl表格"+",每頁sheet導(dǎo)出的行數(shù)為:"+config_value+"行-------------------"); File file = new File(new SimpleDateFormat("yyyy-MM-dd").format(new Date())+".xls"); WritableWorkbook wbook = null ; WritableSheet wsheet =null; wbook = Workbook.createWorkbook(file); //建立excel文件 long startTime = System.currentTimeMillis(); try { ExportToExcelSearchCondition condittion = ((ExportToExcelSearchCondition) ServletContext.currentSession().getAttribute("condittion")); List<ContentToExcel> cte = new ArrayList<ContentToExcel>(); if(condittion == null){ throw new Exception("session獲取查詢條件失敗"); }else{ //根據(jù)條件查詢需要導(dǎo)出的數(shù)據(jù) cte = category2ContentDAO.searchByCondition(condittion.getCategoryId(), condittion.getHSDFlag(), condittion.getExternalContentId(),condittion.getContentName(),condittion.getContentId(), condittion.getSystemId(), condittion.getState(), condittion.getCpId(),condittion.getContentType(),condittion.getBeforemodifyTime(),condittion.getAftermodifyTime()); } logger.info("開始導(dǎo)出excel表格--"); if(StringUtils.isNotBlank(config_value)){ int value = NumberUtils.toInt(config_value); if( cte !=null && cte.size()>0 ){ int k = 0;//分sheet的個數(shù) int i = 0;//用于循環(huán)Excel的行號 Iterator<ContentToExcel> it = cte.iterator(); Map<String,String> contentidAndContentTypeMap = new HashMap<String,String>(); Map<String,String> contentIdAndDurationMap= new HashMap<String,String>(); for (ContentToExcel contentToExcel : cte) { contentidAndContentTypeMap.put(contentToExcel.getContentId(), contentToExcel.getContentType()); } //計算時長 contentIdAndDurationMap = category2ContentDAO.getDurationByContentIdMap(contentidAndContentTypeMap); /** * 1)采用iterator迭代器進(jìn)行迭代,與for循環(huán)相比可優(yōu)化迭代效率。 * 2)大量數(shù)據(jù)影響效率,求大數(shù)據(jù)量的解決辦法。 * add by guohua.yuan 2013-06-08 */ while(it.hasNext()){ ContentToExcel contentToExcel = it.next(); if( i % value == 0){ wsheet = wbook.createSheet("節(jié)目單導(dǎo)出("+(int)(i/value+1)+")", (int)(i/value+1)); //工作表名稱 //設(shè)置Excel字體 WritableFont wfont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); WritableCellFormat titleFormat = new WritableCellFormat(wfont); String[] title = { "標(biāo)識","名稱", "類型", "碼流分檔", "時長","快編人員" ,"初審人員","復(fù)審人員","終審人員","最后更新時間","內(nèi)容創(chuàng)建時間","入庫時間","狀態(tài)"}; //設(shè)置Excel表頭 for (int j = 0; j < title.length; j++) { Label excelTitle = new Label(j, 0, title[j], titleFormat); wsheet.addCell(excelTitle); } k = k+1; } wsheet.addCell(new Label(0, i+1 -value*(k-1), contentToExcel.getContentId())); wsheet.addCell(new Label(1, i+1 - value*(k-1), contentToExcel.getContentName())); if(contentToExcel.getContentType().equals("Serie")){ wsheet.addCell(new Label(2, i+1 - value*(k-1), "電視劇單集")); }else if(contentToExcel.getContentType().equals("Series")){ wsheet.addCell(new Label(2, i+1 - value*(k-1), "連續(xù)劇")); }else if(contentToExcel.getContentType().equals("Movie")){ wsheet.addCell(new Label(2, i+1 - value*(k-1), "電影")); } if(contentToExcel.getHSDFlag() == 1){ wsheet.addCell(new Label(3, i+1 - value*(k-1), "標(biāo)清")); }else if(contentToExcel.getHSDFlag() == 2){ wsheet.addCell(new Label(3, i+1 -value*(k-1), "高清")); }else if(contentToExcel.getHSDFlag() == 3){ wsheet.addCell(new Label(3, i+1 - value*(k-1), "超高清")); } //添加時長 wsheet.addCell(new Label(4, i+1 - value*(k-1),contentIdAndDurationMap.get(contentToExcel.getContentId()))); wsheet.addCell(new Label(5, i+1 - value*(k-1), contentToExcel.getKbPersonner())); wsheet.addCell(new Label(6, i+1 - value*(k-1), contentToExcel.getCsPersonner())); wsheet.addCell(new Label(7, i+1 - value*(k-1), contentToExcel.getFsPersonner())); wsheet.addCell(new Label(8, i+1 - value*(k-1), contentToExcel.getZsPersonner())); if(contentToExcel.getModifyTime()==null){ wsheet.addCell(new Label(9, i+1 - value*(k-1), "")); }else{ wsheet.addCell(new Label(9, i+1 - value*(k-1), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(contentToExcel.getModifyTime()))); } if(contentToExcel.getCreateTime() == null){ wsheet.addCell(new Label(10, i+1 - value*(k-1), "")); }else{ wsheet.addCell(new Label(10, i+1 - value*(k-1), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(contentToExcel.getCreateTime()))); } if(contentToExcel.getInstorageTime() ==null){ wsheet.addCell(new Label(11,i+1 - value*(k-1),"")); }else{ wsheet.addCell(new Label(11,i+1 - value*(k-1),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(contentToExcel.getInstorageTime()))); } if(StringUtils.equals(contentToExcel.getStatus(), "1500")){ wsheet.addCell(new Label(12, i+1 - value*(k-1), "成品就緒")); }else if(StringUtils.equals(contentToExcel.getStatus(), "1700")){ wsheet.addCell(new Label(12, i+1 - value*(k-1), "發(fā)布成功 ")); }else if(StringUtils.equals(contentToExcel.getStatus(), "1800")){ wsheet.addCell(new Label(12, i+1 - value*(k-1), "發(fā)布失敗")); }else if(StringUtils.equals(contentToExcel.getStatus(), "1901")){ wsheet.addCell(new Label(12, i+1 - value*(k-1), "發(fā)布中")); } i++; } wbook.write();//寫入文件 }else{ throw new Exception("沒有數(shù)據(jù)可導(dǎo)"); } }else{ throw new Exception("請檢查系統(tǒng)配置管理是否配置導(dǎo)出類型的數(shù)據(jù)"); } } catch (Exception e) { throw new Exception(e); }finally{ if(wbook!=null){ wbook.close(); } } logger.info("導(dǎo)出excel耗時:"+(System.currentTimeMillis() - startTime)+"ms"); ServletContext.currentSession().removeAttribute("condittion"); return new DownloadFile(file.getName()).readFrom(file); }
希望本文所述對大家jsp程序設(shè)計有所幫助。
相關(guān)文章
Spring獲取ApplicationContext對象工具類的實現(xiàn)方法
這篇文章主要介紹了 Spring獲取ApplicationContext對象工具類的實現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10JSP頁面中文參數(shù)的傳遞(get和post方法分析)
JSP頁面中文參數(shù)傳遞在實際的編程中是十分有用的,get和post方法更是十分的常見,那如何掌握好這些方法呢?本文將會向你實現(xiàn)這些方法的具體事宜2013-08-08