org.ttzero.excel.reader.MergedSheetSubStream Maven / Gradle / Ivy
/*
* Copyright (c) 2017-2020, [email protected] All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ttzero.excel.reader;
import org.ttzero.excel.entity.e3.Block;
import org.ttzero.excel.entity.e3.GlobalsSetting;
import org.ttzero.excel.entity.e3.IgnoreParser;
import org.ttzero.excel.entity.e3.IndexParser;
import org.ttzero.excel.entity.e3.MergedCellsParser;
import org.ttzero.excel.entity.e3.ParserIdentifier;
import org.ttzero.excel.entity.e3.SheetInfo;
import org.ttzero.excel.entity.e3.SheetSubStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author guanquan.wang at 2020-04-08 11:57
*/
public class MergedSheetSubStream extends SheetSubStream {
// A merge cells grid
private Grid mergeCells;
private List list;
private Block privateBlock;
private boolean hasMergedCell;
public MergedSheetSubStream(GlobalsSetting gs, SheetInfo sheet) {
super(gs, sheet);
list = new ArrayList<>();
}
@Override
public void get(Block block) {
super.get(block);
// Merged
if (!eof && hasMergedCell && firstRow != null) {
for (int i = firstRow.fc; i < firstRow.lc; i++)
mergeCells.merge(firstRow.index, firstRow.cells[i]);
}
}
/**
* Parse whitelist record
*
* @param id the record id
* @param block block data
* @return true if break parse
*/
@Override
protected boolean parseWhiteListRecord(int id, Block block) {
switch (id) {
// 4.7 Cell Table and Row Blocks
case ParserIdentifier.INDEX:
index = IndexParser.get(block);
break;
// 5.35 DIMENSION
case ParserIdentifier.DIMENSION:
block.ready();
// Index to first used row
int fr = block.nextInt() + 1;
// Index to last used row, increased by 1
int lr = block.nextInt();
// Index to first used column
short fc = (short) (block.nextShort() + 1);
// Index to last used column, increased by 1
short lc = block.nextShort();
range = new Dimension(fr, fc, lr, lc);
block.commit();
break;
// 4.7 Cell Table and Row Blocks
case ParserIdentifier.ROW:
if (privateBlock == null) {
this.privateBlock = block.deepClone();
this.privateBlock.cacheIdentifier();
}
IgnoreParser.get(block);
break;
// 5.67 MERGEDCELLS
case ParserIdentifier.MERGEDCELLS:
list.addAll(Arrays.asList(MergedCellsParser.get(block)));
break;
default:
IgnoreParser.get(block);
}
return false;
}
/**
* Do something before parse RowBlock
*
* @return boolean value, abort if returns false
*/
@Override
protected boolean beforeParseRowBlock() {
if (!list.isEmpty()) {
mergeCells = GridFactory.create(list);
hasMergedCell = true;
list = null; // Free
}
this.block = privateBlock;
if (this.block != null)
this.block.getContext().sectorTable.moveTo(this.block.currentSecID());
return super.beforeParseRowBlock();
}
/**
* iterator rows
*
* @return Row
*/
@Override
public BIFF8Row nextRow() {
BIFF8Row row = super.nextRow();
if (hasMergedCell && row != null) {
for (int i = row.fc; i < row.lc; i++)
mergeCells.merge(row.index, row.cells[i]);
}
return row;
}
}