1 28 package net.sf.jasperreports.engine.export; 29 30 import java.io.File ; 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.io.OutputStream ; 34 import java.util.ArrayList ; 35 import java.util.Collection ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 import java.util.Map ; 40 41 import net.sf.jasperreports.engine.JRAbstractExporter; 42 import net.sf.jasperreports.engine.JRAlignment; 43 import net.sf.jasperreports.engine.JRException; 44 import net.sf.jasperreports.engine.JRExporterParameter; 45 import net.sf.jasperreports.engine.JRFont; 46 import net.sf.jasperreports.engine.JRPrintElement; 47 import net.sf.jasperreports.engine.JRPrintEllipse; 48 import net.sf.jasperreports.engine.JRPrintFrame; 49 import net.sf.jasperreports.engine.JRPrintImage; 50 import net.sf.jasperreports.engine.JRPrintLine; 51 import net.sf.jasperreports.engine.JRPrintPage; 52 import net.sf.jasperreports.engine.JRPrintRectangle; 53 import net.sf.jasperreports.engine.JRPrintText; 54 import net.sf.jasperreports.engine.JRTextElement; 55 import net.sf.jasperreports.engine.JasperPrint; 56 import net.sf.jasperreports.engine.base.JRBasePrintPage; 57 import net.sf.jasperreports.engine.base.JRBasePrintText; 58 import net.sf.jasperreports.engine.util.JRStyledText; 59 import net.sf.jasperreports.engine.util.JRStyledTextParser; 60 61 import org.xml.sax.SAXException ; 62 63 67 public abstract class JRXlsAbstractExporter extends JRAbstractExporter 68 { 69 70 protected static class TextAlignHolder 71 { 72 public final short horizontalAlignment; 73 public final short verticalAlignment; 74 public final short rotation; 75 76 public TextAlignHolder(short horizontalAlignment, short verticalAlignment, short rotation) 77 { 78 this.horizontalAlignment = horizontalAlignment; 79 this.verticalAlignment = verticalAlignment; 80 this.rotation = rotation; 81 } 82 } 83 84 87 protected int pageHeight = 0; 88 89 92 protected List loadedFonts = new ArrayList (); 93 94 97 protected boolean isOnePagePerSheet = false; 98 99 protected boolean isRemoveEmptySpace = false; 100 101 protected boolean isWhitePageBackground = true; 102 103 protected boolean isAutoDetectCellType = true; 104 protected boolean isDetectCellType = false; 105 106 protected boolean isFontSizeFixEnabled = false; 107 108 protected String [] sheetNames = null; 109 110 113 protected JRStyledTextParser styledTextParser = new JRStyledTextParser(); 114 115 protected JRExportProgressMonitor progressMonitor = null; 116 117 protected int reportIndex = 0; 118 119 120 protected Map fontMap = null; 121 122 private int skippedRows = 0; 123 124 127 protected JRFont defaultFont = null; 128 129 132 protected int sheetIndex = 0; 133 134 138 protected Map sheetNamesMap = null; 139 140 143 protected JRFont getDefaultFont() 144 { 145 return defaultFont; 146 } 147 148 149 152 public void exportReport() throws JRException 153 { 154 progressMonitor = (JRExportProgressMonitor)parameters.get(JRExporterParameter.PROGRESS_MONITOR); 155 156 157 setOffset(); 158 159 try 160 { 161 162 setExportContext(); 163 164 165 setInput(); 166 167 168 if (!isModeBatch) 169 { 170 setPageRange(); 171 } 172 173 setParameters(); 174 175 OutputStream os = (OutputStream )parameters.get(JRExporterParameter.OUTPUT_STREAM); 176 if (os != null) 177 { 178 exportReportToStream(os); 179 } 180 else 181 { 182 File destFile = (File )parameters.get(JRExporterParameter.OUTPUT_FILE); 183 if (destFile == null) 184 { 185 String fileName = (String )parameters.get(JRExporterParameter.OUTPUT_FILE_NAME); 186 if (fileName != null) 187 { 188 destFile = new File (fileName); 189 } 190 else 191 { 192 throw new JRException("No output specified for the exporter."); 193 } 194 } 195 196 try 197 { 198 os = new FileOutputStream (destFile); 199 exportReportToStream(os); 200 os.flush(); 201 } 202 catch (IOException e) 203 { 204 throw new JRException("Error trying to export to file : " + destFile, e); 205 } 206 finally 207 { 208 if (os != null) 209 { 210 try 211 { 212 os.close(); 213 } 214 catch(IOException e) 215 { 216 } 217 } 218 } 219 } 220 } 221 finally 222 { 223 resetExportContext(); 224 } 225 } 226 227 protected void setParameters() 228 { 229 Boolean isOnePagePerSheetParameter = (Boolean )parameters.get(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET); 230 if (isOnePagePerSheetParameter != null) 231 { 232 isOnePagePerSheet = isOnePagePerSheetParameter.booleanValue(); 233 } 234 235 Boolean isRemoveEmptySpaceParameter = (Boolean )parameters.get(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS); 236 if (isRemoveEmptySpaceParameter != null) 237 { 238 isRemoveEmptySpace = isRemoveEmptySpaceParameter.booleanValue(); 239 } 240 241 Boolean isWhitePageBackgroundParameter = (Boolean )parameters.get(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND); 242 if (isWhitePageBackgroundParameter != null) 243 { 244 isWhitePageBackground = isWhitePageBackgroundParameter.booleanValue(); 245 setBackground(); 246 } 247 248 Boolean isAutoDetectCellTypeParameter = (Boolean )parameters.get(JRXlsAbstractExporterParameter.IS_AUTO_DETECT_CELL_TYPE); 249 if (isAutoDetectCellTypeParameter != null) 250 { 251 isAutoDetectCellType = isAutoDetectCellTypeParameter.booleanValue(); 252 } 253 254 Boolean isDetectCellTypeParameter = (Boolean ) parameters.get(JRXlsAbstractExporterParameter.IS_DETECT_CELL_TYPE); 255 isDetectCellType = isDetectCellTypeParameter != null && isDetectCellTypeParameter.booleanValue(); 256 257 Boolean isFontSizeFixEnabledParameter = (Boolean ) this.parameters.get(JRXlsAbstractExporterParameter.IS_FONT_SIZE_FIX_ENABLED); 258 if (isFontSizeFixEnabledParameter != null) 259 { 260 isFontSizeFixEnabled = isFontSizeFixEnabledParameter.booleanValue(); 261 } 262 263 sheetNames = (String [])parameters.get(JRXlsAbstractExporterParameter.SHEET_NAMES); 264 265 fontMap = (Map ) parameters.get(JRExporterParameter.FONT_MAP); 266 } 267 268 protected abstract void setBackground(); 269 270 protected void exportReportToStream(OutputStream os) throws JRException 271 { 272 openWorkbook(os); 273 sheetNamesMap = new HashMap (); 274 275 for(reportIndex = 0; reportIndex < jasperPrintList.size(); reportIndex++) 276 { 277 jasperPrint = (JasperPrint)jasperPrintList.get(reportIndex); 278 defaultFont = new JRBasePrintText(jasperPrint.getDefaultStyleProvider()); 279 280 List pages = jasperPrint.getPages(); 281 if (pages != null && pages.size() > 0) 282 { 283 if (isModeBatch) 284 { 285 startPageIndex = 0; 286 endPageIndex = pages.size() - 1; 287 } 288 289 if (isOnePagePerSheet) 290 { 291 pageHeight = jasperPrint.getPageHeight(); 292 293 for(int pageIndex = startPageIndex; pageIndex <= endPageIndex; pageIndex++) 294 { 295 if (Thread.currentThread().isInterrupted()) 296 { 297 throw new JRException("Current thread interrupted."); 298 } 299 300 JRPrintPage page = (JRPrintPage)pages.get(pageIndex); 301 302 if (sheetNames != null && sheetIndex < sheetNames.length) 303 { 304 createSheet(getSheetName(sheetNames[sheetIndex])); 305 } 306 else 307 { 308 createSheet("Page " + (sheetIndex + 1)); 309 } 310 311 sheetIndex++; 313 314 315 exportPage(null, page); 316 } 317 } 318 else 319 { 320 pageHeight = jasperPrint.getPageHeight() * (endPageIndex - startPageIndex + 1); 321 322 List alterYs = new ArrayList (); 323 JRPrintPage allPages = new JRBasePrintPage(); 324 Collection elements = null; 325 JRPrintElement element = null; 326 for(int pageIndex = startPageIndex; pageIndex <= endPageIndex; pageIndex++) 327 { 328 if (Thread.currentThread().isInterrupted()) 329 { 330 throw new JRException("Current thread interrupted."); 331 } 332 333 JRPrintPage page = (JRPrintPage)pages.get(pageIndex); 334 335 elements = page.getElements(); 336 if (elements != null && elements.size() > 0) 337 { 338 for(Iterator it = elements.iterator(); it.hasNext();) 339 { 340 element = (JRPrintElement)it.next(); 341 allPages.addElement(element); 342 343 alterYs.add(new Integer (element.getY() + jasperPrint.getPageHeight() * pageIndex)); 344 } 345 } 346 } 347 348 if (sheetNames != null && sheetIndex < sheetNames.length) 349 { 350 createSheet(getSheetName(sheetNames[sheetIndex])); 351 } 352 else 353 { 354 createSheet(getSheetName(jasperPrint.getName())); 355 } 356 357 sheetIndex++; 359 360 361 exportPage(alterYs, allPages); 362 } 363 } 364 } 365 366 closeWorkbook(os); 367 } 368 369 372 protected void exportPage(List alterYs, JRPrintPage page) throws JRException 373 { 374 JRGridLayout layout = new JRGridLayout(page.getElements(), alterYs, 375 jasperPrint.getPageWidth(), pageHeight, 376 globalOffsetX, globalOffsetY, getExporterElements(), true, true, false, null); 377 378 JRExporterGridCell grid[][] = layout.getGrid(); 379 boolean isRowNotEmpty[] = layout.getIsRowNotEmpty(); 380 List xCuts = layout.getXCuts(); 381 382 int width = 0; 383 for(int i = 1; i < xCuts.size(); i++) 384 { 385 width = ((Integer )xCuts.get(i)).intValue() - ((Integer )xCuts.get(i - 1)).intValue(); 386 setColumnWidth((short)(i - 1), (short)(width * 43)); 387 } 388 389 skippedRows = 0; 390 for(int y = 0; y < grid.length; y++) 391 { 392 int rowIndex = y - skippedRows; 393 394 if (isRowNotEmpty[y] || !isRemoveEmptySpace) 395 { 396 JRExporterGridCell[] gridRow = grid[y]; 397 398 int emptyCellColSpan = 0; 399 int emptyCellWidth = 0; 400 int lastRowHeight = JRGridLayout.getRowHeight(gridRow); 401 402 setRowHeight(rowIndex, lastRowHeight); 403 404 for(int x = 0; x < gridRow.length; x++) 405 { 406 setCell(x, rowIndex); 407 408 JRExporterGridCell gridCell = gridRow[x]; 409 if(gridCell.element != null) 410 { 411 if (emptyCellColSpan > 0) 412 { 413 if (emptyCellColSpan > 1) 414 { 415 } 418 emptyCellColSpan = 0; 419 emptyCellWidth = 0; 420 } 421 422 JRPrintElement element = gridCell.element; 423 424 if (element instanceof JRPrintLine) 425 { 426 exportLine((JRPrintLine)element, gridCell, x, rowIndex); 427 } 428 else if (element instanceof JRPrintRectangle) 429 { 430 exportRectangle(element, gridCell, x, rowIndex); 431 } 432 else if (element instanceof JRPrintEllipse) 433 { 434 exportRectangle(element, gridCell, x, rowIndex); 435 } 436 else if (element instanceof JRPrintImage) 437 { 438 exportImage((JRPrintImage) element, gridCell, x, rowIndex); 439 } 440 else if (element instanceof JRPrintText) 441 { 442 exportText((JRPrintText)element, gridCell, x, rowIndex); 443 } 444 else if (element instanceof JRPrintFrame) 445 { 446 exportFrame((JRPrintFrame) element, gridCell, x, y); } 448 449 x += gridCell.colSpan - 1; 450 } 451 else 452 { 453 emptyCellColSpan++; 454 emptyCellWidth += gridCell.width; 455 addBlankCell(gridCell, x, rowIndex); 456 } 457 } 458 459 if (emptyCellColSpan > 0) 460 { 461 if (emptyCellColSpan > 1) 462 { 463 } 466 } 467 } 468 else 469 { 470 skippedRows++; 471 } 479 } 480 481 if (progressMonitor != null) 482 { 483 progressMonitor.afterPageExport(); 484 } 485 } 486 487 490 protected JRStyledText getStyledText(JRPrintText textElement) 491 { 492 JRStyledText styledText = null; 493 494 String text = textElement.getText(); 495 if (text != null) 496 { 497 if (textElement.isStyledText()) 498 { 499 try 500 { 501 styledText = styledTextParser.parse(null, text); 502 } 503 catch (SAXException e) 504 { 505 } 507 } 508 509 if (styledText == null) 510 { 511 styledText = new JRStyledText(); 512 styledText.append(text); 513 styledText.addRun(new JRStyledText.Run(null, 0, text.length())); 514 } 515 } 516 517 return styledText; 518 } 519 520 protected static TextAlignHolder getTextAlignHolder(JRPrintText textElement) 521 { 522 short horizontalAlignment; 523 short verticalAlignment; 524 short rotation = textElement.getRotation(); 525 526 switch (textElement.getRotation()) 527 { 528 case JRTextElement.ROTATION_LEFT : 529 { 530 switch (textElement.getHorizontalAlignment()) 531 { 532 case JRAlignment.HORIZONTAL_ALIGN_LEFT : 533 { 534 verticalAlignment = JRAlignment.VERTICAL_ALIGN_BOTTOM; 535 break; 536 } 537 case JRAlignment.HORIZONTAL_ALIGN_CENTER : 538 { 539 verticalAlignment = JRAlignment.VERTICAL_ALIGN_MIDDLE; 540 break; 541 } 542 case JRAlignment.HORIZONTAL_ALIGN_RIGHT : 543 { 544 verticalAlignment = JRAlignment.VERTICAL_ALIGN_TOP; 545 break; 546 } 547 case JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED : 548 { 549 verticalAlignment = JRAlignment.VERTICAL_ALIGN_JUSTIFIED; 550 break; 551 } 552 default : 553 { 554 verticalAlignment = JRAlignment.VERTICAL_ALIGN_BOTTOM; 555 } 556 } 557 558 switch (textElement.getVerticalAlignment()) 559 { 560 case JRAlignment.VERTICAL_ALIGN_TOP : 561 { 562 horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_LEFT; 563 break; 564 } 565 case JRAlignment.VERTICAL_ALIGN_MIDDLE : 566 { 567 horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_CENTER; 568 break; 569 } 570 case JRAlignment.VERTICAL_ALIGN_BOTTOM : 571 { 572 horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_RIGHT; 573 break; 574 } 575 default : 576 { 577 horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_LEFT; 578 } 579 } 580 581 break; 582 } 583 case JRTextElement.ROTATION_RIGHT : 584 { 585 switch (textElement.getHorizontalAlignment()) 586 { 587 case JRAlignment.HORIZONTAL_ALIGN_LEFT : 588 { 589 verticalAlignment = JRAlignment.VERTICAL_ALIGN_TOP; 590 break; 591 } 592 case JRAlignment.HORIZONTAL_ALIGN_CENTER : 593 { 594 verticalAlignment = JRAlignment.VERTICAL_ALIGN_MIDDLE; 595 break; 596 } 597 case JRAlignment.HORIZONTAL_ALIGN_RIGHT : 598 { 599 verticalAlignment = JRAlignment.VERTICAL_ALIGN_BOTTOM; 600 break; 601 } 602 case JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED : 603 { 604 verticalAlignment = JRAlignment.VERTICAL_ALIGN_JUSTIFIED; 605 break; 606 } 607 default : 608 { 609 verticalAlignment = JRAlignment.VERTICAL_ALIGN_TOP; 610 } 611 } 612 613 switch (textElement.getVerticalAlignment()) 614 { 615 case JRAlignment.VERTICAL_ALIGN_TOP : 616 { 617 horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_RIGHT; 618 break; 619 } 620 case JRAlignment.VERTICAL_ALIGN_MIDDLE : 621 { 622 horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_CENTER; 623 break; 624 } 625 case JRAlignment.VERTICAL_ALIGN_BOTTOM : 626 { 627 horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_LEFT; 628 break; 629 } 630 default : 631 { 632 horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_RIGHT; 633 } 634 } 635 636 break; 637 } 638 case JRTextElement.ROTATION_UPSIDE_DOWN: 639 case JRTextElement.ROTATION_NONE : 640 default : 641 { 642 horizontalAlignment = textElement.getHorizontalAlignment(); 643 verticalAlignment = textElement.getVerticalAlignment(); 644 } 645 } 646 647 return new TextAlignHolder(horizontalAlignment, verticalAlignment, rotation); 648 } 649 650 653 private String getSheetName(String sheetName) 654 { 655 if(!sheetNamesMap.containsKey(sheetName)) 657 { 658 sheetNamesMap.put(sheetName, new Integer (1)); 660 return sheetName; 661 } 662 663 int currentIndex = ((Integer )sheetNamesMap.get(sheetName)).intValue() + 1; 664 sheetNamesMap.put(sheetName, new Integer (currentIndex)); 665 return sheetName + " " + currentIndex; 666 } 667 668 protected abstract JRGridLayout.ExporterElements getExporterElements(); 669 670 protected abstract void openWorkbook(OutputStream os) throws JRException; 671 672 protected abstract void createSheet(String name); 673 674 protected abstract void closeWorkbook(OutputStream os) throws JRException; 675 676 protected abstract void setColumnWidth (short index, short width); 677 678 protected abstract void setRowHeight(int rowIndex, int lastRowHeight) throws JRException; 679 680 protected abstract void setCell(int colIndex, int rowIndex); 681 682 protected abstract void addBlankCell(JRExporterGridCell gridCell, int colIndex, int rowIndex) throws JRException; 683 684 protected abstract void exportText(JRPrintText text, JRExporterGridCell cell, int colIndex, int rowIndex) throws JRException; 685 686 protected abstract void exportImage(JRPrintImage image, JRExporterGridCell cell, int colIndex, int rowIndex) throws JRException; 687 688 protected abstract void exportRectangle(JRPrintElement element, JRExporterGridCell cell, int colIndex, int rowIndex) throws JRException; 689 690 protected abstract void exportLine(JRPrintLine line, JRExporterGridCell cell, int colIndex, int rowIndex) throws JRException; 691 692 protected abstract void exportFrame(JRPrintFrame frame, JRExporterGridCell cell, int colIndex, int rowIndex) throws JRException; 693 } 694 | Popular Tags |