1 19 20 package jxl.read.biff; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 25 import jxl.Sheet; 26 import jxl.Cell; 27 import jxl.CellType; 28 import jxl.LabelCell; 29 import jxl.Hyperlink; 30 import jxl.Range; 31 import jxl.SheetSettings; 32 import jxl.WorkbookSettings; 33 import jxl.CellView; 34 import jxl.Image; 35 import jxl.biff.Type; 36 import jxl.biff.FormattingRecords; 37 import jxl.biff.EmptyCell; 38 import jxl.biff.WorkspaceInformationRecord; 39 import jxl.biff.drawing.Chart; 40 import jxl.biff.drawing.DrawingGroupObject; 41 import jxl.biff.drawing.Drawing; 42 import jxl.format.CellFormat; 43 44 52 public class SheetImpl implements Sheet 53 { 54 57 private File excelFile; 58 61 private SSTRecord sharedStrings; 62 63 66 private BOFRecord sheetBof; 67 68 71 private BOFRecord workbookBof; 72 73 76 private FormattingRecords formattingRecords; 77 78 81 private String name; 82 83 86 private int numRows; 87 88 91 private int numCols; 92 93 96 private Cell[][] cells; 97 98 101 private int startPosition; 102 103 106 private ColumnInfoRecord[] columnInfos; 107 108 111 private RowRecord[] rowRecords; 112 113 116 private ArrayList rowProperties; 117 118 122 private ArrayList columnInfosArray; 123 124 127 private ArrayList sharedFormulas; 128 129 132 private ArrayList hyperlinks; 133 134 137 private ArrayList charts; 138 139 142 private ArrayList drawings; 143 144 148 private ArrayList images; 149 150 153 private DataValidation dataValidation; 154 155 158 private Range[] mergedCells; 159 160 163 private boolean columnInfosInitialized; 164 165 168 private boolean rowRecordsInitialized; 169 170 173 private boolean nineteenFour; 174 175 178 private WorkspaceInformationRecord workspaceOptions; 179 180 183 private boolean hidden; 184 185 188 private PLSRecord plsRecord; 189 190 193 private ButtonPropertySetRecord buttonPropertySet; 194 195 198 private SheetSettings settings; 199 200 203 private int[] rowBreaks; 204 205 209 private WorkbookParser workbook; 210 211 214 private WorkbookSettings workbookSettings; 215 216 228 SheetImpl(File f, 229 SSTRecord sst, 230 FormattingRecords fr, 231 BOFRecord sb, 232 BOFRecord wb, 233 boolean nf, 234 WorkbookParser wp) 235 throws BiffException 236 { 237 excelFile = f; 238 sharedStrings = sst; 239 formattingRecords = fr; 240 sheetBof = sb; 241 workbookBof = wb; 242 columnInfosArray = new ArrayList (); 243 sharedFormulas = new ArrayList (); 244 hyperlinks = new ArrayList (); 245 rowProperties = new ArrayList (10); 246 columnInfosInitialized = false; 247 rowRecordsInitialized = false; 248 nineteenFour = nf; 249 workbook = wp; 250 workbookSettings = workbook.getSettings(); 251 252 startPosition = f.getPos(); 254 255 if (sheetBof.isChart()) 256 { 257 startPosition -= (sheetBof.getLength() + 4); 259 } 260 261 Record r = null; 262 int bofs = 1; 263 264 while (bofs >= 1) 265 { 266 r = f.next(); 267 268 if (r.getCode() == Type.EOF.value) 270 { 271 bofs--; 272 } 273 274 if (r.getCode() == Type.BOF.value) 275 { 276 bofs++; 277 } 278 } 279 } 280 281 288 public Cell getCell(int column, int row) 289 { 290 if (cells == null) 293 { 294 readSheet(); 295 } 296 297 Cell c = cells[row][column]; 298 299 if (c == null) 300 { 301 c = new EmptyCell(column, row); 302 cells[row][column] = c; 303 } 304 305 return c; 306 } 307 308 317 public Cell findCell(String contents) 318 { 319 Cell cell = null; 320 boolean found = false; 321 322 for (int i = 0; i < getRows() && !found; i++) 323 { 324 Cell[] row = getRow(i); 325 for (int j = 0; j < row.length && !found; j++) 326 { 327 if (row[j].getContents().equals(contents)) 328 { 329 cell = row[j]; 330 found = true; 331 } 332 } 333 } 334 335 return cell; 336 } 337 338 350 public LabelCell findLabelCell(String contents) 351 { 352 LabelCell cell = null; 353 boolean found = false; 354 355 for (int i = 0; i < getRows() && !found; i++) 356 { 357 Cell[] row = getRow(i); 358 for (int j = 0; j < row.length && !found; j++) 359 { 360 if ((row[j].getType() == CellType.LABEL || 361 row[j].getType() == CellType.STRING_FORMULA) && 362 row[j].getContents().equals(contents)) 363 { 364 cell = (LabelCell) row[j]; 365 found = true; 366 } 367 } 368 } 369 370 return cell; 371 } 372 373 378 public int getRows() 379 { 380 if (cells == null) 383 { 384 readSheet(); 385 } 386 387 return numRows; 388 } 389 390 395 public int getColumns() 396 { 397 if (cells == null) 400 { 401 readSheet(); 402 } 403 404 return numCols; 405 } 406 407 414 public Cell[] getRow(int row) 415 { 416 if (cells == null) 419 { 420 readSheet(); 421 } 422 423 boolean found = false; 425 int col = numCols - 1; 426 while (col >= 0 && !found) 427 { 428 if (cells[row][col] != null) 429 { 430 found = true; 431 } 432 else 433 { 434 col--; 435 } 436 } 437 438 Cell[] c = new Cell[col + 1]; 440 441 for (int i = 0; i <= col; i++) 442 { 443 c[i] = getCell(i, row); 444 } 445 return c; 446 } 447 448 455 public Cell[] getColumn(int col) 456 { 457 if (cells == null) 460 { 461 readSheet(); 462 } 463 464 boolean found = false; 466 int row = numRows - 1; 467 while (row >= 0 && !found) 468 { 469 if (cells[row][col] != null) 470 { 471 found = true; 472 } 473 else 474 { 475 row--; 476 } 477 } 478 479 Cell[] c = new Cell[row + 1]; 481 482 for (int i = 0; i <= row; i++) 483 { 484 c[i] = getCell(col, i); 485 } 486 return c; 487 } 488 489 494 public String getName() 495 { 496 return name; 497 } 498 499 504 final void setName(String s) 505 { 506 name = s; 507 } 508 509 515 public boolean isHidden() 516 { 517 return hidden; 518 } 519 520 527 public ColumnInfoRecord getColumnInfo(int col) 528 { 529 if (!columnInfosInitialized) 530 { 531 Iterator i = columnInfosArray.iterator(); 533 ColumnInfoRecord cir = null; 534 while (i.hasNext()) 535 { 536 cir = (ColumnInfoRecord) i.next(); 537 538 int startcol = Math.max(0, cir.getStartColumn()); 539 int endcol = Math.min(columnInfos.length - 1, cir.getEndColumn()); 540 541 for (int c = startcol; c <= endcol; c++) 542 { 543 columnInfos[c] = cir; 544 } 545 546 if (endcol < startcol) 547 { 548 columnInfos[startcol] = cir; 549 } 550 } 551 552 columnInfosInitialized = true; 553 } 554 555 return col < columnInfos.length ? columnInfos[col] : null; 556 } 557 558 563 public ColumnInfoRecord[] getColumnInfos() 564 { 565 ColumnInfoRecord[] infos = new ColumnInfoRecord[columnInfosArray.size()]; 567 for (int i = 0; i < columnInfosArray.size(); i++) 568 { 569 infos[i] = (ColumnInfoRecord) columnInfosArray.get(i); 570 } 571 572 return infos; 573 } 574 575 580 final void setHidden(boolean h) 581 { 582 hidden = h; 583 } 584 585 589 final void clear() 590 { 591 cells = null; 592 mergedCells = null; 593 columnInfosArray.clear(); 594 sharedFormulas.clear(); 595 hyperlinks.clear(); 596 columnInfosInitialized = false; 597 598 if (!workbookSettings.getGCDisabled()) 599 { 600 System.gc(); 601 } 602 } 603 604 607 final void readSheet() 608 { 609 if (!sheetBof.isWorksheet()) 613 { 614 numRows = 0; 615 numCols = 0; 616 cells = new Cell[0][0]; 617 } 619 620 SheetReader reader = new SheetReader(excelFile, 621 sharedStrings, 622 formattingRecords, 623 sheetBof, 624 workbookBof, 625 nineteenFour, 626 workbook, 627 startPosition, 628 this); 629 reader.read(); 630 631 numRows = reader.getNumRows(); 633 numCols = reader.getNumCols(); 634 cells = reader.getCells(); 635 rowProperties = reader.getRowProperties(); 636 columnInfosArray = reader.getColumnInfosArray(); 637 hyperlinks = reader.getHyperlinks(); 638 charts = reader.getCharts(); 639 drawings = reader.getDrawings(); 640 dataValidation = reader.getDataValidation(); 641 mergedCells = reader.getMergedCells(); 642 settings = reader.getSettings(); 643 settings.setHidden(hidden); 644 rowBreaks = reader.getRowBreaks(); 645 workspaceOptions = reader.getWorkspaceOptions(); 646 plsRecord = reader.getPLS(); 647 buttonPropertySet = reader.getButtonPropertySet(); 648 649 reader = null; 650 651 if (!workbookSettings.getGCDisabled()) 652 { 653 System.gc(); 654 } 655 656 if (columnInfosArray.size() > 0) 657 { 658 ColumnInfoRecord cir = (ColumnInfoRecord) 659 columnInfosArray.get(columnInfosArray.size() - 1); 660 columnInfos = new ColumnInfoRecord[cir.getEndColumn() + 1]; 661 } 662 else 663 { 664 columnInfos = new ColumnInfoRecord[0]; 665 } 666 } 667 668 673 public Hyperlink[] getHyperlinks() 674 { 675 Hyperlink[] hl = new Hyperlink[hyperlinks.size()]; 676 677 for (int i = 0; i < hyperlinks.size(); i++) 678 { 679 hl[i] = (Hyperlink) hyperlinks.get(i); 680 } 681 682 return hl; 683 } 684 685 690 public Range[] getMergedCells() 691 { 692 if (mergedCells == null) 693 { 694 return new Range[0]; 695 } 696 697 return mergedCells; 698 } 699 700 705 public RowRecord[] getRowProperties() 706 { 707 RowRecord[] rp = new RowRecord[rowProperties.size()]; 708 for (int i = 0; i < rp.length; i++) 709 { 710 rp[i] = (RowRecord) rowProperties.get(i); 711 } 712 713 return rp; 714 } 715 716 721 public DataValidation getDataValidation() 722 { 723 return dataValidation; 724 } 725 726 733 RowRecord getRowInfo(int r) 734 { 735 if (!rowRecordsInitialized) 736 { 737 rowRecords = new RowRecord[getRows()]; 738 Iterator i = rowProperties.iterator(); 739 740 int rownum = 0; 741 RowRecord rr = null; 742 while (i.hasNext()) 743 { 744 rr = (RowRecord) i.next(); 745 rownum = rr.getRowNumber(); 746 if (rownum < rowRecords.length) 747 { 748 rowRecords[rownum] = rr; 749 } 750 } 751 752 rowRecordsInitialized = true; 753 } 754 755 return rowRecords[r]; 756 } 757 758 763 public final int[] getRowPageBreaks() 764 { 765 return rowBreaks; 766 } 767 768 773 public final Chart[] getCharts() 774 { 775 Chart[] ch = new Chart[charts.size()]; 776 777 for (int i = 0; i < ch.length; i++) 778 { 779 ch[i] = (Chart) charts.get(i); 780 } 781 return ch; 782 } 783 784 789 public final DrawingGroupObject[] getDrawings() 790 { 791 DrawingGroupObject[] dr = new DrawingGroupObject[drawings.size()]; 792 dr = (DrawingGroupObject[]) drawings.toArray(dr); 793 return dr; 794 } 795 796 802 public boolean isProtected() 803 { 804 return settings.isProtected(); 805 } 806 807 813 public WorkspaceInformationRecord getWorkspaceOptions() 814 { 815 return workspaceOptions; 816 } 817 818 823 public SheetSettings getSettings() 824 { 825 return settings; 826 } 827 828 832 WorkbookParser getWorkbook() 833 { 834 return workbook; 835 } 836 837 844 public CellFormat getColumnFormat(int col) 845 { 846 CellView cv = getColumnView(col); 847 return cv.getFormat(); 848 } 849 850 857 public int getColumnWidth(int col) 858 { 859 return getColumnView(col).getSize() / 256; 860 } 861 862 869 public CellView getColumnView(int col) 870 { 871 ColumnInfoRecord cir = getColumnInfo(col); 872 CellView cv = new CellView(); 873 874 if (cir != null) 875 { 876 cv.setDimension(cir.getWidth() / 256); cv.setSize(cir.getWidth()); 878 cv.setHidden(cir.getHidden()); 879 cv.setFormat(formattingRecords.getXFRecord(cir.getXFIndex())); 880 } 881 else 882 { 883 cv.setDimension(settings.getDefaultColumnWidth() / 256); cv.setSize(settings.getDefaultColumnWidth()); 885 } 886 887 return cv; 888 } 889 890 898 public int getRowHeight(int row) 899 { 900 return getRowView(row).getDimension(); 901 } 902 903 910 public CellView getRowView(int row) 911 { 912 RowRecord rr = getRowInfo(row); 913 914 CellView cv = new CellView(); 915 916 if (rr != null) 917 { 918 cv.setDimension(rr.getRowHeight()); cv.setSize(rr.getRowHeight()); 920 cv.setHidden(rr.isCollapsed()); 921 } 922 else 923 { 924 cv.setDimension(settings.getDefaultRowHeight()); 925 cv.setSize(settings.getDefaultRowHeight()); } 927 928 return cv; 929 } 930 931 932 937 public BOFRecord getSheetBof() 938 { 939 return sheetBof; 940 } 941 942 948 public BOFRecord getWorkbookBof() 949 { 950 return workbookBof; 951 } 952 953 959 public PLSRecord getPLS() 960 { 961 return plsRecord; 962 } 963 964 969 public ButtonPropertySetRecord getButtonPropertySet() 970 { 971 return buttonPropertySet; 972 } 973 974 979 public int getNumberOfImages() 980 { 981 if (images == null) 982 { 983 initializeImages(); 984 } 985 986 return images.size(); 987 } 988 989 995 public Image getDrawing(int i) 996 { 997 if (images == null) 998 { 999 initializeImages(); 1000 } 1001 1002 return (Image) images.get(i); 1003 } 1004 1005 1008 private void initializeImages() 1009 { 1010 if (images != null) 1011 { 1012 return; 1013 } 1014 1015 images = new ArrayList (); 1016 DrawingGroupObject[] dgos = getDrawings(); 1017 1018 for (int i = 0; i < dgos.length; i++) 1019 { 1020 if (dgos[i] instanceof Drawing) 1021 { 1022 images.add(dgos[i]); 1023 } 1024 } 1025 } 1026 1027} 1028 1029 1030 1031 1032 1033 1034 | Popular Tags |