compoundfile.java

来自「jexcelapi_2_4, JXL的API, JXL是JAVA读取EXCEL的」· Java 代码 · 共 553 行 · 第 1/2 页

JAVA
553
字号
                               headerBlock, 
                               NUM_EXTENSION_BLOCK_POS);
    
    // Set the root start block
    IntegerHelper.getFourBytes(rootStartBlock,
                               headerBlock,
                               ROOT_START_BLOCK_POS);

    // Set the block numbers for the BBD.  Set the BBD running 
    // after the excel data and summary information
    int pos = BIG_BLOCK_DEPOT_BLOCKS_POS;

    // See how many blocks fit into the header
    int blocksToWrite = Math.min(numBigBlockDepotBlocks, 
                                 (BIG_BLOCK_SIZE - 
                                  BIG_BLOCK_DEPOT_BLOCKS_POS)/4);
    int extensionBlock = 0;
    int blocksWritten = 0;

    for (int i = 0 ; i < blocksToWrite; i++)
    {
      IntegerHelper.getFourBytes(bbdStartBlock + i, 
                                 headerBlock,
                                 pos);
      pos += 4;
      blocksWritten++;
    }
    
    // Pad out the rest of the header with blanks
    for (int i = pos; i < BIG_BLOCK_SIZE; i++)
    {
      headerBlock[i] = (byte) 0xff;
    }

    out.write(headerBlock);

    // Write out the extension blocks
    pos = 0;

    for (int extBlock = 0; extBlock < numExtensionBlocks; extBlock++)
    {
      blocksToWrite = Math.min(numBigBlockDepotBlocks - blocksWritten, 
                               BIG_BLOCK_SIZE/4 -1);

      for(int j = 0 ; j < blocksToWrite; j++)
      {
        IntegerHelper.getFourBytes(bbdStartBlock + blocksWritten + j, 
                                   extensionBlockData,
                                   pos);
        pos += 4;
      }

      blocksWritten += blocksToWrite;

      // Indicate the next block, or the termination of the chain
      int nextBlock = (blocksWritten == numBigBlockDepotBlocks) ? 
                              -2 : extBlock+1 ;
      IntegerHelper.getFourBytes(nextBlock, extensionBlockData, pos);
      pos +=4;
    }

    if (numExtensionBlocks > 0)
    {
      // Pad out the rest of the extension block with blanks
      for (int i = pos; i < extensionBlockData.length; i++)
      {
        extensionBlockData[i] = (byte) 0xff;
      }

      out.write(extensionBlockData);
    }
  }

  /**
   * Checks that the data can fit into the current BBD block.  If not,
   * then it moves on to the next block
   * 
   * @exception IOException 
   */
  private void checkBbdPos() throws IOException
  {
    if (bbdPos >= BIG_BLOCK_SIZE)
    {
      // Write out the extension block.  This will simply be the next block
      out.write(bigBlockDepot);
      
      // Create a new block
      bigBlockDepot = new byte[BIG_BLOCK_SIZE];
      bbdPos = 0;
    }
  }

  /**
   * Writes out the Big Block Depot
   * 
   * @exception IOException 
   */
  private void writeBigBlockDepot() throws IOException
  {
    // This is after the excel data
    bigBlockDepot = new byte[BIG_BLOCK_SIZE];
    bbdPos = 0;

    // Write out the extension blocks, indicating them as special blocks
    for (int i = 0 ; i < numExtensionBlocks; i++)
    {
      IntegerHelper.getFourBytes(-3, bigBlockDepot, bbdPos);
      bbdPos += 4;
      checkBbdPos();
    }

    int blocksToWrite = excelDataBlocks - 1;
    int blockNumber   = excelDataStartBlock + 1;
    
    while (blocksToWrite > 0)
    {
      int bbdBlocks = Math.min(blocksToWrite, (BIG_BLOCK_SIZE - bbdPos)/4);

      // Set the exceldata
      for (int i = 0 ; i < bbdBlocks; i++)
      {
        IntegerHelper.getFourBytes(blockNumber, bigBlockDepot, bbdPos);
        bbdPos +=4 ;
        blockNumber++;
      }
      
      blocksToWrite -= bbdBlocks;
      checkBbdPos();
    }

    // Write the end of the block chain for the excel data
    IntegerHelper.getFourBytes(-2, bigBlockDepot, bbdPos);
    bbdPos += 4;
    checkBbdPos();

    // The excel data has been written.  Now write out the rest of it

    // Write the block chain for the summary information
    int summaryInfoBlock = excelDataStartBlock + excelDataBlocks;
    for (int i = summaryInfoBlock; i < summaryInfoBlock + 7; i++)
    {
      IntegerHelper.getFourBytes(i + 1, bigBlockDepot, bbdPos);
      bbdPos +=4 ;
      checkBbdPos();
    } 

    // Write the end of the block chain for the summary
    IntegerHelper.getFourBytes(-2, bigBlockDepot, bbdPos);
    bbdPos += 4;
    checkBbdPos();

    // Write the block chain for the document summary information
    for (int i = summaryInfoBlock + 8; i < summaryInfoBlock + 15; i++)
    {
      IntegerHelper.getFourBytes(i + 1, bigBlockDepot, bbdPos);
      bbdPos +=4 ;
      checkBbdPos();
    } 

    // Write the end of the block chain for the document summary
    IntegerHelper.getFourBytes(-2, bigBlockDepot, bbdPos);
    bbdPos += 4;
    checkBbdPos();

    // The Big Block Depot immediately follows the excel data.  Denote these
    // as a special block
    for (int i = 0; i < numBigBlockDepotBlocks; i++)
    {
      IntegerHelper.getFourBytes(-3, bigBlockDepot, bbdPos);
      bbdPos += 4;
      checkBbdPos();
    }

    // Set the block chain for the root entry just one block
    IntegerHelper.getFourBytes(-2, bigBlockDepot, bbdPos);
    bbdPos += 4;
    checkBbdPos();

    // Pad out the remainder of the block
    if (bbdPos != 0)
    {
      for (int i = bbdPos; i < BIG_BLOCK_SIZE; i++)
      {
        bigBlockDepot[i] = (byte) 0xff;
      }
      out.write(bigBlockDepot);
    }
  }

  /**
   * Writes out the property sets
   * 
   * @exception IOException 
   */
  private void writePropertySets() throws IOException
  {
    byte[] propertySetStorage = new byte[BIG_BLOCK_SIZE];

    int pos = 0;

    // Set the workbook property set
    PropertyStorage ps = new PropertyStorage("Root Entry");
    ps.setType(5);
    ps.setStartBlock(-2);
    ps.setSize(0);
    ps.setPrevious(-1);
    ps.setNext(-1);
    ps.setDirectory(2);

    System.arraycopy(ps.data, 0, 
                     propertySetStorage, pos, 
                     PROPERTY_STORAGE_BLOCK_SIZE);
    pos += PROPERTY_STORAGE_BLOCK_SIZE;


    // Set the workbook property set
    ps = new PropertyStorage("Workbook");
    ps.setType(2);
    ps.setStartBlock(excelDataStartBlock);
      // start the excel data after immediately after this block
    ps.setSize(requiredSize);
      // alway use a big block stream - none of that messing around
      // with small blocks
    ps.setPrevious(-1);
    ps.setNext(-1);
    ps.setDirectory(-1);

    System.arraycopy(ps.data, 0, 
                     propertySetStorage, pos, 
                     PROPERTY_STORAGE_BLOCK_SIZE);
    pos += PROPERTY_STORAGE_BLOCK_SIZE;


    // Set the summary information
    ps = new PropertyStorage("\u0005SummaryInformation");
    ps.setType(2);
    ps.setStartBlock(excelDataStartBlock + excelDataBlocks);
    ps.setSize(SMALL_BLOCK_THRESHOLD);
    ps.setPrevious(1);
    ps.setNext(3);
    ps.setDirectory(-1);

    System.arraycopy(ps.data, 0, 
                     propertySetStorage, pos, 
                     PROPERTY_STORAGE_BLOCK_SIZE);
    pos += PROPERTY_STORAGE_BLOCK_SIZE;

    // Set the summary information
    ps = new PropertyStorage("\u0005DocumentSummaryInformation");
    ps.setType(2);
    ps.setStartBlock(excelDataStartBlock + excelDataBlocks+8);
    ps.setSize(SMALL_BLOCK_THRESHOLD);
    ps.setPrevious(-1);
    ps.setNext(-1);
    ps.setDirectory(-1);

    System.arraycopy(ps.data, 0, 
                     propertySetStorage, pos, 
                     PROPERTY_STORAGE_BLOCK_SIZE);
    pos += PROPERTY_STORAGE_BLOCK_SIZE;
    
    out.write(propertySetStorage);
  }

}










⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?