Produced by Araxis Merge on 6/9/2017 3:51:30 PM Eastern Daylight Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
| # | Location | File | Last Modified |
|---|---|---|---|
| 1 | Fri Jun 9 19:51:30 2017 UTC | ||
| 2 | eHealth_Exch (eHealth Exchange Enhancements) Build 3 docs & code_May_2017.zip\VAP_CIF_CODE0502.zip\VAP_CIF_CODE0502\VAP_CIF_CODE0502\nvap-web\src\main\java\gov\va\nvap\web\util\xls | ExcelExporter.java | Fri Apr 21 20:03:30 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 1 | 756 |
| Removed | 0 | 0 |
| Whitespace | |
|---|---|
| Character case | Differences in character case are significant |
| Line endings | Differences in line endings (CR and LF characters) are ignored |
| CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
| 1 | package go v.va.nvap. web.util.x ls; | |||||
| 2 | ||||||
| 3 | import gov .va.nvap.c ommon.vali dation.Ass ert; | |||||
| 4 | import gov .va.nvap.c ommon.vali dation.Nul lChecker; | |||||
| 5 | import gov .va.nvap.s vc.consent mgmt.stub. dao.DelayR easonDAO; | |||||
| 6 | import gov .va.nvap.w eb.dao.Fac ilityDAO; | |||||
| 7 | import gov .va.nvap.w eb.facilit y.Facility ; | |||||
| 8 | import gov .va.nvap.w eb.helper. report.Rep ortHelper; | |||||
| 9 | import gov .va.nvap.w eb.util.Co nstants; | |||||
| 10 | import jav a.io.ByteA rrayOutput Stream; | |||||
| 11 | ||||||
| 12 | import jav a.io.IOExc eption; | |||||
| 13 | import jav a.text.Par seExceptio n; | |||||
| 14 | import jav a.text.Sim pleDateFor mat; | |||||
| 15 | import jav a.util.Arr ayList; | |||||
| 16 | import jav a.util.Col lection; | |||||
| 17 | import jav a.util.Dat e; | |||||
| 18 | import jav a.util.Has hMap; | |||||
| 19 | import jav a.util.Lin kedHashMap ; | |||||
| 20 | import jav a.util.Lis t; | |||||
| 21 | import jav a.util.Map ; | |||||
| 22 | import jav a.util.Map .Entry; | |||||
| 23 | ||||||
| 24 | import jav ax.servlet .ServletEx ception; | |||||
| 25 | import jav ax.servlet .ServletOu tputStream ; | |||||
| 26 | import jav ax.servlet .http.Http ServletReq uest; | |||||
| 27 | import jav ax.servlet .http.Http ServletRes ponse; | |||||
| 28 | import org .apache.co mmons.lang .StringUti ls; | |||||
| 29 | ||||||
| 30 | import org .apache.po i.hssf.use rmodel.HSS FWorkbook; | |||||
| 31 | import org .apache.po i.ss.userm odel.Cell; | |||||
| 32 | import org .apache.po i.ss.userm odel.CellS tyle; | |||||
| 33 | import org .apache.po i.ss.userm odel.Creat ionHelper; | |||||
| 34 | import org .apache.po i.ss.userm odel.Font; | |||||
| 35 | import org .apache.po i.ss.userm odel.Index edColors; | |||||
| 36 | import org .apache.po i.ss.userm odel.Row; | |||||
| 37 | import org .apache.po i.ss.userm odel.Sheet ; | |||||
| 38 | import org .apache.po i.ss.userm odel.Workb ook; | |||||
| 39 | import org .apache.po i.ss.util. CellRangeA ddress; | |||||
| 40 | import org .springfra mework.bea ns.factory .annotatio n.Required ; | |||||
| 41 | ||||||
| 42 | /** | |||||
| 43 | * Helper class to e xport a se t of data rows to ex cel. It en capsulates the | |||||
| 44 | * apache POI excel workbook m odules. It serves as a helper class to c reate VAP | |||||
| 45 | * specifi c excel re ports whic h can be r eused for all report s. | |||||
| 46 | * | |||||
| 47 | * The cla ss can be refactored to follow a more Ob ject Orien ted struct ure when | |||||
| 48 | * the rep orts are a lso migrat ed to foll ow a an ob ject struc ture with Title, | |||||
| 49 | * Headers and Data Rows. | |||||
| 50 | * | |||||
| 51 | * @author Asha Amri traj | |||||
| 52 | * | |||||
| 53 | */ | |||||
| 54 | public cla ss ExcelEx porter { | |||||
| 55 | ||||||
| 56 | // Sta tic fields | |||||
| 57 | ||||||
| 58 | pr ivate stat ic final S tring EXCE L_CACHE_CO NTROL = "C ache-Contr ol"; | |||||
| 59 | pr ivate stat ic final S tring EXCE L_CONTENT_ DISPOSITIO N = "Conte nt-Disposi tion"; | |||||
| 60 | ||||||
| 61 | // Pri vate const ants | |||||
| 62 | ||||||
| 63 | privat e final in t MAX_ROWS = 65536; | |||||
| 64 | ||||||
| 65 | // Pub lic static methods | |||||
| 66 | ||||||
| 67 | public static Ob ject getFi lterValue( Object fil terValue) { | |||||
| 68 | re turn NullC hecker.isN ullOrEmpty (filterVal ue) ? "ALL " : filter Value; | |||||
| 69 | } | |||||
| 70 | ||||||
| 71 | public static St ring getCo nsentTypeF ilter(Stri ng consent Type) { | |||||
| 72 | if (NullChec ker.isNull OrEmpty(co nsentType) ) { | |||||
| 73 | return " ALL"; | |||||
| 74 | } else{ | |||||
| 75 | if (cons entType.co ntains("Nw HIN")) { | |||||
| 76 | cons entType = consentTyp e.replace( "NwHIN", C onstants.g etOrganiza tionName() ); | |||||
| 77 | } | |||||
| 78 | return c onsentType ; | |||||
| 79 | } | |||||
| 80 | } | |||||
| 81 | ||||||
| 82 | public static St ring getFa cilitiesFi lter(Facil ityDAO dao , String s tationNumb ers) { | |||||
| 83 | if (NullChec ker.isNull OrEmpty(st ationNumbe rs)) { | |||||
| 84 | return " "; | |||||
| 85 | } else if (" ALL".equal s(stationN umbers)) { | |||||
| 86 | return s tationNumb ers; | |||||
| 87 | } else { | |||||
| 88 | String t ext = ""; | |||||
| 89 | List<Fac ility> fac ilities = dao.findBy StationNum bers(stati onNumbers) ; | |||||
| 90 | if (faci lities.siz e() < 12) { | |||||
| 91 | for (int i = 0 ; i < faci lities.siz e(); i++) { | |||||
| 92 | text += i == 0 ? "" : "\n"; | |||||
| 93 | text += fa cilities.g et(i).getF acilityNam e(); | |||||
| 94 | } | |||||
| 95 | } else { | |||||
| 96 | for (int i = 0 ; i < 10; i++) { | |||||
| 97 | text += fa cilities.g et(i).getF acilityNam e() + "\n" ; | |||||
| 98 | } | |||||
| 99 | text += "and " + (facili ties.size( ) - 10) + " other fa cilities"; | |||||
| 100 | } | |||||
| 101 | return t ext; | |||||
| 102 | } | |||||
| 103 | } | |||||
| 104 | ||||||
| 105 | public static St ring getPa tientPrefe rredFacili tyFilter(F acilityDAO dao, Stri ng facilit yName) { | |||||
| 106 | Fa cility fac ility = da o.findBySt ationNumbe r(facility Name); | |||||
| 107 | re turn facil ity == nul l ? "ALL" : facility .getFacili tyName(); | |||||
| 108 | } | |||||
| 109 | ||||||
| 110 | public static St ring getRe asonsForDe layFilter( DelayReaso nDAO dao, String del ayReasonId s) { | |||||
| 111 | if (NullChec ker.isEmpt y(delayRea sonIds)) { | |||||
| 112 | return " "; | |||||
| 113 | } else if (" ALL".equal s(delayRea sonIds)) { | |||||
| 114 | return d elayReason Ids; | |||||
| 115 | } else { | |||||
| 116 | String t ext = ""; | |||||
| 117 | List<Str ing> delay ReasonName sList = ne w ArrayLis t<String>( ); | |||||
| 118 | for (fin al String delayReaso nId : dela yReasonIds .split("," )) { | |||||
| 119 | dela yReasonNam esList.add (dao.findB yDelayReas onId(Long. parseLong( delayReaso nId)).getN ame()); | |||||
| 120 | } | |||||
| 121 | text = d elayReason NamesList. toString() .replaceAl l("\\[|\\] ", ""); | |||||
| 122 | return t ext; | |||||
| 123 | } | |||||
| 124 | } | |||||
| 125 | ||||||
| 126 | public static vo id populat eFilterMap ForExport( HttpServle tRequest r equest, fi nal Linked HashMap<St ring, Obje ct> filter s, | |||||
| 127 | fi nal Map<St ring, List <Object>> filterMap, String pa tientTypes ) { | |||||
| 128 | ||||||
| 129 | if ("1".equa ls(patient Types)) { | |||||
| 130 | // Read patients o nly | |||||
| 131 | filters. put("Patie nt Types", "Real Pat ients"); | |||||
| 132 | } else if (" 2".equals( patientTyp es)) { | |||||
| 133 | // Test patients o nly | |||||
| 134 | filters. put("Patie nt Types", "Test Pat ients"); | |||||
| 135 | } else if (" 3".equals( patientTyp es)) { | |||||
| 136 | // Both real and t est patien ts | |||||
| 137 | filters. put("Patie nt Types", "ALL"); | |||||
| 138 | } else { | |||||
| 139 | // Don't output th is filter | |||||
| 140 | } | |||||
| 141 | ||||||
| 142 | fo r (Map.Ent ry<String, Object> f ilter : fi lters.entr ySet()) { | |||||
| 143 | final St ring filte rName = fi lter.getKe y(); | |||||
| 144 | final Ob ject value = filter. getValue() ; | |||||
| 145 | List<Obj ect> headi ngValue = new ArrayL ist<Object >() {{ add (value); } }; | |||||
| 146 | filterMa p.put(filt erName, he adingValue ); | |||||
| 147 | } | |||||
| 148 | } | |||||
| 149 | ||||||
| 150 | // Pri vate field s | |||||
| 151 | ||||||
| 152 | /* * | |||||
| 153 | * The HTTP applicatio n type for Excel. No te: it wil l be diffe rent for | |||||
| 154 | * Office 20 03, 2011 e tc. | |||||
| 155 | * / | |||||
| 156 | pr ivate Stri ng streamA pplication Type; | |||||
| 157 | ||||||
| 158 | /* * | |||||
| 159 | * Specify t he cache c ontrol tha t is neede d when str eaming exc el export. | |||||
| 160 | * / | |||||
| 161 | pr ivate Stri ng streamC acheContro l; | |||||
| 162 | ||||||
| 163 | /* * | |||||
| 164 | * The title font size . | |||||
| 165 | * / | |||||
| 166 | pr ivate shor t titleFon tSize; | |||||
| 167 | ||||||
| 168 | Date d ate; | |||||
| 169 | Simple DateFormat df = new SimpleDate Format("yy yyMMdd_hhm mss"); | |||||
| 170 | Simple DateFormat dateCell = new Simp leDateForm at("MM/dd/ yyyy"); | |||||
| 171 | Simple DateFormat timeStamp = new Sim pleDateFor mat("MM/dd /yyyy hh:m m a"); | |||||
| 172 | ||||||
| 173 | String formatted Date; | |||||
| 174 | ||||||
| 175 | /** | |||||
| 176 | * Boo lean for d etermining if max ro w count ha s been hit | |||||
| 177 | */ | |||||
| 178 | privat e static B oolean max Rows = fal se; | |||||
| 179 | ||||||
| 180 | /** | |||||
| 181 | * Auto size the sheet . We do no t wants co lumns to b e hidden i n the exce l | |||||
| 182 | * sheet. | |||||
| 183 | * | |||||
| 184 | * @param s | |||||
| 185 | * the shee t | |||||
| 186 | * @param to talColumns | |||||
| 187 | * the tota l number o f columns | |||||
| 188 | * @return t he sheet a fter auto sizing. | |||||
| 189 | * / | |||||
| 190 | pr ivate Shee t autoSize Sheet(fina l Sheet s, final int totalColu mns) { | |||||
| 191 | ||||||
| 192 | Asse rt.assertN otEmpty(s, "Sheet ca nnot be em pty!"); | |||||
| 193 | ||||||
| 194 | if ( totalColum ns < 0) { | |||||
| 195 | throw new Runtim eException ("Total co lumns cann ot be less than 0"); | |||||
| 196 | } | |||||
| 197 | // I terate thr ough all c olumns | |||||
| 198 | for (int i = 0 ; i < tota lColumns; i++) { | |||||
| 199 | // Aut o size eac h column | |||||
| 200 | try { | |||||
| 201 | s.autoSi zeColumn(i ); | |||||
| 202 | } catc h (Excepti on ex) { | |||||
| 203 | // TODO: Fix an er ror with A pache POI component with autos ize | |||||
| 204 | // BAD T EMPORARY F IX! | |||||
| 205 | //Ignore Exception | |||||
| 206 | //ex.pri ntStackTra ce(); | |||||
| 207 | } | |||||
| 208 | } | |||||
| 209 | // R eturn the sheet afte r auto siz ing | |||||
| 210 | retu rn s; | |||||
| 211 | } | |||||
| 212 | ||||||
| 213 | /* * | |||||
| 214 | * Create th e Column h eadings in the Excel Spreadshe et startin g at the | |||||
| 215 | * specified row numbe rs. | |||||
| 216 | * | |||||
| 217 | * @param sh eet | |||||
| 218 | * The exce l document sheet | |||||
| 219 | * @param ro wNumber | |||||
| 220 | * The row number to create the heading | |||||
| 221 | * @param he adings | |||||
| 222 | * The head ers like p atient fir st name, l ast name e tc. in a | |||||
| 223 | * collecti on of stri ngs. The h eaders wil l be creat ed in the same | |||||
| 224 | * order in the list. | |||||
| 225 | * @return t he excel s heet | |||||
| 226 | * / | |||||
| 227 | pr otected Sh eet create ColumnHead ings(final Sheet she et, | |||||
| 228 | final int rowNum ber, final Collectio n<String> headings) { | |||||
| 229 | Asse rt.assertN otEmpty(sh eet, "Shee t cannot b e empty!") ; | |||||
| 230 | if ( rowNumber < 0) { | |||||
| 231 | throw new Runtim eException ("Row numb er cannot be less th an 0"); | |||||
| 232 | } | |||||
| 233 | if ( NullChecke r.isNotEmp ty(heading s)) { | |||||
| 234 | // Cre ate the he ading row based on t he row num ber. | |||||
| 235 | final Row headin g = sheet. createRow( rowNumber) ; | |||||
| 236 | int i = 0; | |||||
| 237 | // Loo p through all the he adings | |||||
| 238 | for (f inal Strin g headingT ext : head ings) { | |||||
| 239 | // Creat e Font | |||||
| 240 | final Fo nt boldFon t = sheet. getWorkboo k().create Font(); | |||||
| 241 | // Make font bold | |||||
| 242 | boldFont .setBoldwe ight(Font. BOLDWEIGHT _BOLD); | |||||
| 243 | // Creat e Style fo r headers | |||||
| 244 | final Ce llStyle cs = sheet.g etWorkbook ().createC ellStyle() ; | |||||
| 245 | // Set t he Font to the style | |||||
| 246 | cs.setFo nt(boldFon t); | |||||
| 247 | cs.setAl ignment(Ce llStyle.AL IGN_CENTER ); | |||||
| 248 | // Creat e heading cell | |||||
| 249 | final Ce ll cell = heading.cr eateCell(i ); | |||||
| 250 | // Set t he bold fo nt cell st yle to the cell | |||||
| 251 | cell.set CellStyle( cs); | |||||
| 252 | cell.set CellValue( headingTex t); | |||||
| 253 | i++; | |||||
| 254 | } | |||||
| 255 | } | |||||
| 256 | // R eturn the created sh eet | |||||
| 257 | retu rn sheet; | |||||
| 258 | } | |||||
| 259 | ||||||
| 260 | /* * | |||||
| 261 | * Create th e data row s (the lis t of recor ds). The d ata rows a re expecte d in | |||||
| 262 | * a list. T he items i n the List would con tain a map <columnKey , value>. For | |||||
| 263 | * example: List Item 1: patient SSN, 66600 00001 List Item 2: | |||||
| 264 | * patientFi rstName: A sha | |||||
| 265 | * | |||||
| 266 | * @param sh eet | |||||
| 267 | * The exce l workbook sheet | |||||
| 268 | * @param st artRowNumb er | |||||
| 269 | * the star ting row n umber | |||||
| 270 | * @param he adings | |||||
| 271 | * the head ings where the key f or the dat a is pulle d from | |||||
| 272 | * @param re sults | |||||
| 273 | * the list of record s | |||||
| 274 | * @return t he sheet w ith all th e data row s created | |||||
| 275 | * / | |||||
| 276 | pr otected Sh eet create DataRows(f inal Sheet sheet, in t startRow Number, | |||||
| 277 | final Map<String , String> headings, | |||||
| 278 | final List<Map<S tring, Obj ect>> resu lts) { | |||||
| 279 | ||||||
| 280 | Asse rt.assertN otEmpty(sh eet, "Shee t cannot b e empty!") ; | |||||
| 281 | ||||||
| 282 | if ( startRowNu mber < 0) { | |||||
| 283 | throw new Runtim eException ("Row numb er cannot be less th an 0"); | |||||
| 284 | } | |||||
| 285 | ||||||
| 286 | Cr eationHelp er createH elper = sh eet.getWor kbook().ge tCreationH elper(); | |||||
| 287 | ||||||
| 288 | // Any date c olumn | |||||
| 289 | fi nal CellSt yle dateSt yle = shee t.getWorkb ook().crea teCellStyl e(); | |||||
| 290 | da teStyle.se tDataForma t(createHe lper.creat eDataForma t().getFor mat("mm/dd /yyyy")); | |||||
| 291 | // Audit time column | |||||
| 292 | fi nal CellSt yle timeSt yle = shee t.getWorkb ook().crea teCellStyl e(); | |||||
| 293 | ti meStyle.se tDataForma t(createHe lper.creat eDataForma t().getFor mat("mm/dd /yyyy hh:m m AM/PM")) ; | |||||
| 294 | ||||||
| 295 | in t emptyRow Number = s tartRowNum ber - 2; | |||||
| 296 | ||||||
| 297 | // I terate thr ough resul ts and cre ate Cell | |||||
| 298 | if ( NullChecke r.isNotEmp ty(results )) { | |||||
| 299 | // Cre ate Style | |||||
| 300 | final CellStyle cs = sheet .getWorkbo ok().creat eCellStyle (); | |||||
| 301 | // Set the Align ment | |||||
| 302 | cs.set Alignment( CellStyle. ALIGN_LEFT ); | |||||
| 303 | // Wra p the text | |||||
| 304 | //cs.s etWrapText (true); | |||||
| 305 | for (i nt i = 0; i < result s.size(); i++) { | |||||
| 306 | ||||||
| 307 | // Implementi ng check t o account for the ro w limitati on of the exporter. | |||||
| 308 | if (startRow Number == MAX_ROWS) { | |||||
| 309 | outputMa xRowNotice (sheet, em ptyRowNumb er); | |||||
| 310 | return s heet; | |||||
| 311 | } | |||||
| 312 | ||||||
| 313 | final Ro w r = shee t.createRo w(startRow Number); | |||||
| 314 | // Get t he data ro w from the results | |||||
| 315 | final Ma p<String, Object> re sultEntrie s = result s.get(i); | |||||
| 316 | int j = 0; | |||||
| 317 | // Creat e the head ing first | |||||
| 318 | for (fin al String headingKey : heading s.keySet() ) { | |||||
| 319 | String val ueStr = "" ; | |||||
| 320 | // For eve ry heading key, ex. patientSSN , get the value | |||||
| 321 | Object val ue = null; | |||||
| 322 | if (result Entries.co ntainsKey( headingKey )) { | |||||
| 323 | va lue = resu ltEntries. get(headin gKey); | |||||
| 324 | if (NullChec ker.isNotE mpty(value )) { | |||||
| 325 | // G et the str ing for Ex cel Cell | |||||
| 326 | valu eStr = val ue.toStrin g(); | |||||
| 327 | } | |||||
| 328 | } | |||||
| 329 | // Make a cell for e ach value | |||||
| 330 | final Cell c = r.cre ateCell(j) ; | |||||
| 331 | // Create Style | |||||
| 332 | //final Ce llStyle cs = sheet.g etWorkbook ().createC ellStyle() ; | |||||
| 333 | // Set the Alignment | |||||
| 334 | //cs.setAl ignment(Ce llStyle.AL IGN_LEFT); | |||||
| 335 | // Wrap th e text | |||||
| 336 | //cs.setWr apText(tru e); | |||||
| 337 | if (headin gKey.conta ins("Date" )) { | |||||
| 338 | c.setC ellType(0) ; | |||||
| 339 | c.setC ellStyle(d ateStyle); | |||||
| 340 | try { | |||||
| 341 | Da te date = dateCell.p arse((Stri ng)value); | |||||
| 342 | c. setCellVal ue(date); | |||||
| 343 | } catc h (ParseEx ception ex ) { | |||||
| 344 | c. setCellVal ue(""); | |||||
| 345 | } | |||||
| 346 | } | |||||
| 347 | else if (h eadingKey. contains(" auditTime" )) { | |||||
| 348 | c.setC ellType(0) ; | |||||
| 349 | c.setC ellStyle(t imeStyle); | |||||
| 350 | try { | |||||
| 351 | Da te date = timeStamp. parse((Str ing)value) ; | |||||
| 352 | c. setCellVal ue(date); | |||||
| 353 | } catc h (ParseEx ception ex ) { | |||||
| 354 | c. setCellVal ue(""); | |||||
| 355 | } | |||||
| 356 | } | |||||
| 357 | else if (! headingKey .toLowerCa se().conta ins("ssn") && valueS tr.length( ) > 0 && S tringUtils .isNumeric (valueStr) ) { | |||||
| 358 | c.setC ellType(0) ; | |||||
| 359 | c.setC ellStyle(c s); | |||||
| 360 | c.setC ellValue(L ong.parseL ong(valueS tr)); | |||||
| 361 | } | |||||
| 362 | else if (h eadingKey. toLowerCas e().contai ns("oid") && !NullCh ecker.isNu llOrEmpty( valueStr)) { | |||||
| 363 | c.setC ellStyle(c s); | |||||
| 364 | c.setC ellValue(R eportHelpe r.trimOrgO id(valueSt r)); | |||||
| 365 | } | |||||
| 366 | else { | |||||
| 367 | c.setC ellStyle(c s); | |||||
| 368 | c.setC ellValue(v alueStr); | |||||
| 369 | } | |||||
| 370 | // Go to t he next ce ll | |||||
| 371 | j++; | |||||
| 372 | } | |||||
| 373 | // Go to the next row | |||||
| 374 | startRow Number++; | |||||
| 375 | } | |||||
| 376 | } el se { | |||||
| 377 | // Creat e Style | |||||
| 378 | final CellStyle cs = sheet .getWorkbo ok().creat eCellStyle (); | |||||
| 379 | // Set the Align ment | |||||
| 380 | cs.set Alignment( CellStyle. ALIGN_LEFT ); | |||||
| 381 | //add a row | |||||
| 382 | final Ro w r = shee t.createRo w(startRow Number); | |||||
| 383 | //add th e cell and the value for no re cords foun d | |||||
| 384 | final Ce ll c = r.c reateCell( 0); | |||||
| 385 | c.setCel lValue("No records w ere found" ); | |||||
| 386 | } | |||||
| 387 | // T he sheet w ith the ro ws created | |||||
| 388 | retu rn sheet; | |||||
| 389 | } | |||||
| 390 | ||||||
| 391 | /* * | |||||
| 392 | * Create op tional row s if neede d like sel ected opti ons Ex. st art date, end | |||||
| 393 | * date, tot al etc. | |||||
| 394 | * | |||||
| 395 | * @param sh eet | |||||
| 396 | * the work book sheet | |||||
| 397 | * @param st artNumber | |||||
| 398 | * the numb er where t o start cr eating the optional rows | |||||
| 399 | * @param op tionalRows | |||||
| 400 | * the key and the li st of data columns E x. <key Co lumn> <val ue1 | |||||
| 401 | * Column> <value2 Co lumn> | |||||
| 402 | * @return t he workboo k sheet | |||||
| 403 | * / | |||||
| 404 | pr otected Sh eet create OptionalRo ws(final S heet sheet , int star tNumber, | |||||
| 405 | final Map<String , List<Obj ect>> opti onalRows) { | |||||
| 406 | Asse rt.assertN otEmpty(sh eet, "Shee t cannot b e empty!") ; | |||||
| 407 | ||||||
| 408 | if ( startNumbe r < 0) { | |||||
| 409 | throw new Runtim eException ( | |||||
| 410 | "Starting row number cannot be less than 0"); | |||||
| 411 | } | |||||
| 412 | ||||||
| 413 | in t emptyRow Number = s tartNumber - 2; | |||||
| 414 | ||||||
| 415 | // I terate thr ough resul ts and cre ate Cell | |||||
| 416 | if ( NullChecke r.isNotEmp ty(optiona lRows)) { | |||||
| 417 | // Sta rt with th e row numb er that ca me into th is method | |||||
| 418 | for (f inal Entry <String, L ist<Object >> entry : optionalR ows | |||||
| 419 | .entrySet( )) { | |||||
| 420 | ||||||
| 421 | //Im plementing check to account fo r the row limitation of the ex porter. | |||||
| 422 | if ( startNumbe r == MAX_R OWS) { | |||||
| 423 | outputMaxR owNotice(s heet, empt yRowNumber ); | |||||
| 424 | return she et; | |||||
| 425 | } | |||||
| 426 | ||||||
| 427 | // Creat e the row with the s tarting ro w number | |||||
| 428 | final Ro w r = shee t.createRo w(startNum ber); | |||||
| 429 | // Main Column | |||||
| 430 | final Ce llStyle ma inColumnSt yle = shee t.getWorkb ook() | |||||
| 431 | .c reateCellS tyle(); | |||||
| 432 | // Set t he Alignme nt | |||||
| 433 | mainColu mnStyle.se tAlignment (CellStyle .ALIGN_LEF T); | |||||
| 434 | // Creat e Font | |||||
| 435 | final Fo nt boldFon t = sheet. getWorkboo k().create Font(); | |||||
| 436 | // Make font bold | |||||
| 437 | boldFont .setBoldwe ight(Font. BOLDWEIGHT _BOLD); | |||||
| 438 | mainColu mnStyle.se tFont(bold Font); | |||||
| 439 | //mainCo lumnStyle. setWrapTex t(true); | |||||
| 440 | // Creat e the key in the fir st column | |||||
| 441 | final Ce ll c = r.c reateCell( 0); | |||||
| 442 | c.setCel lValue(ent ry.getKey( ) + ":"); | |||||
| 443 | c.setCel lStyle(mai nColumnSty le); | |||||
| 444 | // Creat e list of data colum ns with va lues | |||||
| 445 | int j = 1; | |||||
| 446 | for (fin al Object value : en try.getVal ue()) { | |||||
| 447 | final Cell col = r.c reateCell( j); | |||||
| 448 | final Cell Style cs = sheet.get Workbook() .createCel lStyle(); | |||||
| 449 | // Set the Alignment | |||||
| 450 | cs.setAlig nment(Cell Style.ALIG N_LEFT); | |||||
| 451 | //cs.setWr apText(tru e); | |||||
| 452 | col.setCel lStyle(cs) ; | |||||
| 453 | if (NullCh ecker.isNo tEmpty(val ue)) { | |||||
| 454 | if (en try.getKey ().contain s("Total") ) { | |||||
| 455 | co l.setCellT ype(0); | |||||
| 456 | co l.setCellV alue(Integ er.parseIn t(value.to String())) ; | |||||
| 457 | } | |||||
| 458 | else { | |||||
| 459 | co l.setCellV alue(value .toString( )); | |||||
| 460 | } | |||||
| 461 | } else { | |||||
| 462 | // Default t o N/A if i t's not a total row | |||||
| 463 | if (!entry.g etKey().co ntains("To tal")) { | |||||
| 464 | co l.setCellV alue("n/a" ); | |||||
| 465 | } | |||||
| 466 | } | |||||
| 467 | // Next da ta column | |||||
| 468 | j++; | |||||
| 469 | } | |||||
| 470 | // Next row | |||||
| 471 | startNum ber++; | |||||
| 472 | } | |||||
| 473 | } | |||||
| 474 | // R eturn exce l sheet | |||||
| 475 | retu rn sheet; | |||||
| 476 | } | |||||
| 477 | ||||||
| 478 | /* * | |||||
| 479 | * A conveni ence metho d to creat e an excel sheet wit h a sheet name and | |||||
| 480 | * number. | |||||
| 481 | * | |||||
| 482 | * @param sh eetName | |||||
| 483 | * the shee t name | |||||
| 484 | * @param sh eetNumber | |||||
| 485 | * the shee t number ( cannot be less than 0) | |||||
| 486 | * @return t he created sheet | |||||
| 487 | * / | |||||
| 488 | pr ivate Shee t createSh eet(final String she etName, fi nal int sh eetNumber) { | |||||
| 489 | ||||||
| 490 | Asse rt.assertN otEmpty(sh eetName, " Sheet name cannot be empty!"); | |||||
| 491 | ||||||
| 492 | if ( sheetNumbe r < 0) { | |||||
| 493 | throw new Runtim eException ("Sheet nu mber canno t be less than 0"); | |||||
| 494 | } | |||||
| 495 | ||||||
| 496 | // C reate Work book | |||||
| 497 | fina l Workbook wb = new HSSFWorkbo ok(); | |||||
| 498 | // c reate a ne w sheet | |||||
| 499 | fina l Sheet s = wb.creat eSheet(); | |||||
| 500 | // S et Sheet n ame | |||||
| 501 | wb.s etSheetNam e(sheetNum ber, sheet Name); | |||||
| 502 | // R eturn crea ted sheet | |||||
| 503 | retu rn s; | |||||
| 504 | } | |||||
| 505 | ||||||
| 506 | /* * | |||||
| 507 | * Create th e title of the repor t in the e xcel sheet based on the row an d | |||||
| 508 | * center is based on the number of column s. | |||||
| 509 | * | |||||
| 510 | * @param s | |||||
| 511 | * the shee t to creat e the titl e | |||||
| 512 | * @param ro wNumber | |||||
| 513 | * the row number to create the title | |||||
| 514 | * @param co lSize | |||||
| 515 | * the colu mn sizes t o center t he title | |||||
| 516 | * @param ti tle | |||||
| 517 | * the titl e string | |||||
| 518 | * @return t he sheet w ith the ti tle create d | |||||
| 519 | * / | |||||
| 520 | pr otected Sh eet create Title(fina l Sheet s, final int rowNumber , | |||||
| 521 | final int colSiz e, final S tring titl e) { | |||||
| 522 | ||||||
| 523 | Asse rt.assertN otEmpty(s, "Sheet ca nnot be em pty!"); | |||||
| 524 | ||||||
| 525 | if ( rowNumber < 0) { | |||||
| 526 | throw new Runtim eException ("Sheet nu mber canno t be less than 0"); | |||||
| 527 | } | |||||
| 528 | ||||||
| 529 | fina l Row titl eRow = s.c reateRow(r owNumber); | |||||
| 530 | fina l Font sup erBoldFont = s.getWo rkbook().c reateFont( ); | |||||
| 531 | // M ake font b old | |||||
| 532 | supe rBoldFont. setBoldwei ght(Font.B OLDWEIGHT_ BOLD); | |||||
| 533 | supe rBoldFont. setFontHei ghtInPoint s(this.tit leFontSize ); | |||||
| 534 | // C reate Styl e | |||||
| 535 | fina l CellStyl e cs = s.g etWorkbook ().createC ellStyle() ; | |||||
| 536 | // S et the Fon t to the s tyle | |||||
| 537 | cs.s etFont(sup erBoldFont ); | |||||
| 538 | cs.s etAlignmen t(CellStyl e.ALIGN_CE NTER); | |||||
| 539 | //cs .setWrapTe xt(true); | |||||
| 540 | // C reate head ing cell | |||||
| 541 | fina l Cell cel l = titleR ow.createC ell(0); | |||||
| 542 | // S et the bol d font cel l style to the cell | |||||
| 543 | cell .setCellSt yle(cs); | |||||
| 544 | cell .setCellVa lue(title) ; | |||||
| 545 | // M erge to th e column s ize, so th e title lo oks center ed | |||||
| 546 | s.ad dMergedReg ion(new Ce llRangeAdd ress(0, 0, 0, colSiz e)); | |||||
| 547 | retu rn s; | |||||
| 548 | } | |||||
| 549 | ||||||
| 550 | /* * | |||||
| 551 | * The main export to excel meth od. This m ethod take s a sheet name and | |||||
| 552 | * creates a default w ork book w ith the ti tle, heade rs, data r ows and | |||||
| 553 | * optional rows. | |||||
| 554 | * | |||||
| 555 | * @param sh eetName | |||||
| 556 | * the name of the sh eet to cre ate | |||||
| 557 | * @param he adingText | |||||
| 558 | * the head ing text ( title) | |||||
| 559 | * @param re portHeadin gMap | |||||
| 560 | * the head ers for th e data row s. The key element o f the resu lt | |||||
| 561 | * set and the column beading v alue. | |||||
| 562 | * @param re sults | |||||
| 563 | * the data set resul t map | |||||
| 564 | * @param he adingParam eters | |||||
| 565 | * the para meters tha t are belo w the titl e of the r eport like | |||||
| 566 | * start da te, end da te etc. | |||||
| 567 | * @param op tionalRows | |||||
| 568 | * Optional rows that are neede d like the Grand Tot al, Total | |||||
| 569 | * count et c. | |||||
| 570 | * @return t he excel w orkbook ob ject | |||||
| 571 | * / | |||||
| 572 | pu blic Workb ook export ToExcel(fi nal String sheetName , | |||||
| 573 | final String hea dingText, | |||||
| 574 | final Map<String , String> reportHead ingMap, | |||||
| 575 | final List<Map<S tring, Obj ect>> resu lts, | |||||
| 576 | final Map<String , List<Obj ect>> head ingParamet ers, | |||||
| 577 | final Map<String , List<Obj ect>> opti onalRows) { | |||||
| 578 | int row = 1; | |||||
| 579 | ||||||
| 580 | As sert.asser tNotEmpty( sheetName, "Sheet na me cannot be empty!" ); | |||||
| 581 | ||||||
| 582 | // Create fi rst Sheet - index 0 | |||||
| 583 | fi nal Sheet s = this.c reateSheet (sheetName , 0); | |||||
| 584 | ||||||
| 585 | // Create Ti tle - Row 0 | |||||
| 586 | Ma p<String, List<Objec t>> title = new Hash Map<String , List<Obj ect>>(); | |||||
| 587 | Li st<Object> titleText = new Arr ayList<Obj ect>(); | |||||
| 588 | ti tleText.ad d(headingT ext); | |||||
| 589 | ti tle.put("R eport", ti tleText); | |||||
| 590 | th is.createO ptionalRow s(s, 0, ti tle); | |||||
| 591 | ||||||
| 592 | // Create Da te row - R ow 1 | |||||
| 593 | Ma p<String, List<Objec t>> genera ted = new HashMap<St ring, List <Object>>( ); | |||||
| 594 | Li st<Object> dateText = new Arra yList<Obje ct>(); | |||||
| 595 | da teText.add (new Simpl eDateForma t("MM/dd/y yyy").form at(new Dat e())); | |||||
| 596 | ge nerated.pu t("Date Ge nerated", dateText); | |||||
| 597 | th is.createO ptionalRow s(s, 1, ge nerated); | |||||
| 598 | ||||||
| 599 | // Create fi lters - Ro w 2 | |||||
| 600 | th is.createO ptionalRow s(s, 2, he adingParam eters); | |||||
| 601 | ||||||
| 602 | // Create co lumn headi ngs | |||||
| 603 | ro w = 3 + he adingParam eters.size (); | |||||
| 604 | if (results ! = null && results.si ze() > 0){ | |||||
| 605 | this.cre ateColumnH eadings(s, row, repo rtHeadingM ap.values( )); | |||||
| 606 | } | |||||
| 607 | ||||||
| 608 | // Create da ta rows | |||||
| 609 | th is.createD ataRows(s, row + 1, reportHead ingMap, re sults); | |||||
| 610 | ||||||
| 611 | // Optional Rows after the resul ts with a few rows a fter if we aren't at and won't exceed th e max row count. | |||||
| 612 | if (results ! = null){ | |||||
| 613 | this.cre ateOptiona lRows(s, r ow + resul ts.size() + 2, optio nalRows); | |||||
| 614 | } else { | |||||
| 615 | //add 1 for No Res ults Found row | |||||
| 616 | this.cre ateOptiona lRows(s, r ow + 3, op tionalRows ); | |||||
| 617 | } | |||||
| 618 | ||||||
| 619 | // Do not au tosize due to perfom ance | |||||
| 620 | // Autosize all Column s | |||||
| 621 | // if (report HeadingMap .values(). size() > 1 ) { | |||||
| 622 | // this.a utoSizeShe et(s, repo rtHeadingM ap.values( ).size()); | |||||
| 623 | // } else { | |||||
| 624 | // this.a utoSizeShe et(s, 2); | |||||
| 625 | // } | |||||
| 626 | ||||||
| 627 | in t numOfCol s = report HeadingMap .values(). size() > 1 ? reportH eadingMap. values().s ize() : 2; | |||||
| 628 | for (int i = 0 ; i < numO fCols; i++ ) { | |||||
| 629 | try { | |||||
| 630 | s.se tColumnWid th(i, 19 * 256); | |||||
| 631 | } catc h (Excepti on ex) { | |||||
| 632 | // Ignor e Exceptio n | |||||
| 633 | } | |||||
| 634 | } | |||||
| 635 | ||||||
| 636 | // return th e workbook | |||||
| 637 | re turn s.get Workbook() ; | |||||
| 638 | } | |||||
| 639 | ||||||
| 640 | @R equired | |||||
| 641 | pu blic void setStreamA pplication Type(final String st reamApplic ationType) { | |||||
| 642 | this .streamApp licationTy pe = strea mApplicati onType; | |||||
| 643 | } | |||||
| 644 | ||||||
| 645 | @R equired | |||||
| 646 | pu blic void setStreamC acheContro l(final St ring strea mCacheCont rol) { | |||||
| 647 | this .streamCac heControl = streamCa cheControl ; | |||||
| 648 | } | |||||
| 649 | ||||||
| 650 | @R equired | |||||
| 651 | pu blic void setTitleFo ntSize(fin al short t itleFontSi ze) { | |||||
| 652 | this .titleFont Size = tit leFontSize ; | |||||
| 653 | } | |||||
| 654 | ||||||
| 655 | /* * | |||||
| 656 | * Write the excel to a HTTP str eam. | |||||
| 657 | * @param re portType | |||||
| 658 | * the name of th e report b eing expor ted | |||||
| 659 | * @param wb | |||||
| 660 | * the work book | |||||
| 661 | * @param re sponse | |||||
| 662 | * the HTTP ServletRes ponse obje ct to writ e to its o utput stre am | |||||
| 663 | * @throws S ervletExce ption | |||||
| 664 | * if an e rror (IO) occurs | |||||
| 665 | * / | |||||
| 666 | pu blic void writeExcel ToStream(S tring repo rtType, fi nal Workbo ok wb, | |||||
| 667 | final HttpServle tResponse response) throws Ser vletExcept ion { | |||||
| 668 | ||||||
| 669 | Asse rt.assertN otEmpty(wb , "Workboo k cannot b e empty!") ; | |||||
| 670 | Asse rt.assertN otEmpty(re sponse, "H TTP Respon se cannot be empty!" ); | |||||
| 671 | Da te d = new Date(); | |||||
| 672 | St ring forma ttedDate = df.format (d); | |||||
| 673 | // O utput Exce l to Brows er | |||||
| 674 | // G et the app lication t ype from S pring - ty picaly | |||||
| 675 | // a pplication /vnd.ms-ex cel | |||||
| 676 | resp onse.setCo ntentType( this.strea mApplicati onType); | |||||
| 677 | // S et the con tent dispo sition fil ename to s end to the browser | |||||
| 678 | resp onse.setHe ader(Excel Exporter.E XCEL_CONTE NT_DISPOSI TION, | |||||
| 679 | "attachm ent; filen ame=\"" + reportType + "_" + f ormattedDa te + ".xls " + "\""); | |||||
| 680 | // S et the cac he control in the he ader (typi cally no-c ache | |||||
| 681 | resp onse.setHe ader(Excel Exporter.E XCEL_CACHE _CONTROL, | |||||
| 682 | this.str eamCacheCo ntrol); | |||||
| 683 | try { | |||||
| 684 | // Get the outpu t stream | |||||
| 685 | final ServletOut putStream os = respo nse.getOut putStream( ); | |||||
| 686 | Assert .assertNot Empty(os, "Output St ream canno t be null! "); | |||||
| 687 | // Wri te the res ponse | |||||
| 688 | wb.wri te(os); | |||||
| 689 | // Clo se the out put stream | |||||
| 690 | try { | |||||
| 691 | // Close stream | |||||
| 692 | os.close (); | |||||
| 693 | } catc h (final I OException ex) { | |||||
| 694 | // Ignor e.. Pass i t on to th e applicat ion server to handle the | |||||
| 695 | // threa d | |||||
| 696 | } | |||||
| 697 | } ca tch (final IOExcepti on ex) { | |||||
| 698 | throw new Servle tException (ex); | |||||
| 699 | } | |||||
| 700 | } | |||||
| 701 | ||||||
| 702 | /** | |||||
| 703 | * Get the E xcel docum ent as a b yte array. | |||||
| 704 | * | |||||
| 705 | * @param wb | |||||
| 706 | * the work book | |||||
| 707 | * @re turn | |||||
| 708 | * @throws S ervletExce ption | |||||
| 709 | * if an e rror (IO) occurs | |||||
| 710 | * / | |||||
| 711 | pu blic byte[ ] getExcel AsBytes(fi nal Workbo ok wb) thr ows Servle tException { | |||||
| 712 | ||||||
| 713 | Asse rt.assertN otEmpty(wb , "Workboo k cannot b e empty!") ; | |||||
| 714 | da te = new D ate(); | |||||
| 715 | fo rmattedDat e = df.for mat(date); | |||||
| 716 | By teArrayOut putStream byteStream = new Byt eArrayOutp utStream() ; | |||||
| 717 | by te[] docBy tes = null ; | |||||
| 718 | ||||||
| 719 | try { | |||||
| 720 | // Wri te the res ponse | |||||
| 721 | wb.wri te(byteStr eam); | |||||
| 722 | // Get d ocument as byte arra y | |||||
| 723 | docBytes = byteStr eam.toByte Array(); | |||||
| 724 | // Clo se the out put stream | |||||
| 725 | try { | |||||
| 726 | // Close stream | |||||
| 727 | byteStre am.close() ; | |||||
| 728 | } catc h (final I OException ex) { | |||||
| 729 | // Ignor e.. Pass i t on to th e applicat ion server to handle the | |||||
| 730 | // threa d | |||||
| 731 | } | |||||
| 732 | } ca tch (final IOExcepti on ex) { | |||||
| 733 | throw new Servle tException (ex); | |||||
| 734 | } | |||||
| 735 | ||||||
| 736 | re turn docBy tes; | |||||
| 737 | } | |||||
| 738 | ||||||
| 739 | privat e void out putMaxRowN otice(fina l Sheet sh eet, int e mptyRowNum ber) { | |||||
| 740 | fi nal Row r = sheet.cr eateRow(em ptyRowNumb er); | |||||
| 741 | fi nal Cell c = r.creat eCell(0); | |||||
| 742 | fi nal CellSt yle cs = s heet.getWo rkbook().c reateCellS tyle(); | |||||
| 743 | fi nal Font f ont = shee t.getWorkb ook().crea teFont(); | |||||
| 744 | ||||||
| 745 | fo nt.setColo r(IndexedC olors.RED. getIndex() ); | |||||
| 746 | ||||||
| 747 | cs .setFont(f ont); | |||||
| 748 | // cs.setWrap Text(false ); | |||||
| 749 | ||||||
| 750 | c. setCellSty le(cs); | |||||
| 751 | c. setCellVal ue("Note: Report has been trun cated to f it " + MAX _ROWS + " rows."); | |||||
| 752 | ||||||
| 753 | ma xRows = tr ue; | |||||
| 754 | } | |||||
| 755 | ||||||
| 756 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.