Export an excel file and export it using sheet.

Source: Internet
Author: User

Public DownloadFile exportToExcel () throws Exception {String config_value = systemConfigService. getConfigValueByKey ("Export. xls "); logger.info (" the obtained export type is execl table "+", and the number of sheet export lines per page is: "+ config_value +" Row -------------------"); file file = new File (new SimpleDateFormat ("yyyy-MM-dd "). format (new Date () + ". xls "); WritableWorkbook wbook = null; WritableSheet wsheet = null; wbook = Workbook. createWorkbook (file); // create an ex Excel file 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 failed to obtain the query condition");} else {// cte = category2ContentDAO to be exported Based on the query condition. 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 ("start to export the excel table --"); if (StringUtils. isNotBlank (config_value) {int value = NumberUtils. toInt (config_value); if (c Te! = Null & cte. size ()> 0) {int k = 0; // Number of sheet int I = 0; // The number of Iterator used to loop through Excel <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. getContent Type ();} // computing duration contentIdAndDurationMap = category2ContentDAO. getDurationByContentIdMap (contentidAndContentTypeMap);/*** 1) uses the iterator for iteration, which can optimize the iteration efficiency compared with the for loop. * 2) a large amount of data affects efficiency, and a solution to large data volumes is required. * Add by guohua. yuan 2013-06-08 */while (it. hasNext () {ContentToExcel contentToExcel = it. next (); if (I % value = 0) {wsheet = wbook. createSheet ("menu Export (" + (int) (I/value + 1) + ")", (int) (I/value + 1 )); // worksheet name // set the Excel font WritableFont wfont = new WritableFont (WritableFont. ARIAL, 10, WritableFont. BOLD, false, jxl. format. underlineStyle. NO_UNDERLINE, jxl. format. colour. BLACK); WritableCellFormat titleForma T = new WritableCellFormat (wfont); String [] title = {"identifier", "name", "type", "bitstream file", "duration ", "editors", "reviewers", "reviewers", "Final reviewers", "Last Update time", "content creation time", "warehouse receiving time ", "status"}; // set the Excel header 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 Labe L (1, I + 1-value * (k-1), contentToExcel. getContentName (); if (contentToExcel. getContentType (). equals ("Serie") {wsheet. addCell (new Label (2, I + 1-value * (k-1), "TV series single set");} else if (contentToExcel. getContentType (). equals ("Series") {wsheet. addCell (new Label (2, I + 1-value * (k-1), "series");} else if (contentToExcel. getContentType (). equals ("Movie") {wsheet. addCell (new Label (2, I + 1-value * (k-1), "movie "));} If (contentToExcel. getHSDFlag () = 1) {wsheet. addCell (new Label (3, I + 1-value * (k-1), "SD");} else if (contentToExcel. getHSDFlag () = 2) {wsheet. addCell (new Label (3, I + 1-value * (k-1), "HD");} else if (contentToExcel. getHSDFlag () = 3) {wsheet. addCell (new Label (3, I + 1-value * (k-1), "Ultra HD");} // Add length wsheet. addCell (new Label (4, I + 1-value * (k-1), contentIdAndDurationMap. get (contentToExcel. getConten TId (); 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 L Abel (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. getCre AteTime ();} 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), "finished");} else if (StringUtils. equ Als (contentToExcel. getStatus (), "1700") {wsheet. addCell (new Label (12, I + 1-value * (k-1), "published successfully");} else if (StringUtils. equals (contentToExcel. getStatus (), "1800") {wsheet. addCell (new Label (12, I + 1-value * (k-1), "Publish failed");} else if (StringUtils. equals (contentToExcel. getStatus (), "1901") {wsheet. addCell (new Label (12, I + 1-value * (k-1), "releasing");} I ++;} wbook. write (); // write File} else {throw new Excep Tion ("no data can be exported") ;}} else {throw new Exception ("Check System Configuration Management to configure export data") ;}} catch (Exception e) {throw new Exception (e);} finally {if (wbook! = Null) {wbook. close () ;}} logger.info ("Export excel time:" + (System. currentTimeMillis ()-startTime) + "ms"); ServletContext. currentSession (). removeAttribute ("condittion"); return new DownloadFile (file. getName ()). readFrom (file );}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.