All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.anyline.office.xlsx.entity.XSheet Maven / Gradle / Ivy

There is a newer version: 8.7.2-20250110
Show newest version
/*
 * Copyright 2006-2023 www.anyline.org
 *
 * 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.anyline.office.xlsx.entity;

import org.dom4j.Document;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class XSheet {
    private XWorkBook book;
    private Document doc;
    private Element root;
    private String name;
    private List rows = new ArrayList<>();
    public XSheet(){}
    public XSheet(XWorkBook book, Document doc, String name){
        this.name = name;
        this.book = book;
        this.doc = doc;
        load();
    }
    public void load(){
        if(null == doc){
            return;
        }
        root = doc.getRootElement();
        Element data = root.element("sheetData");
        if(null == data){
            return;
        }
        List rows = data.elements("row");
        int index = 0;
        for(Element row:rows){
            XRow xr = new XRow(book, this, row, index++);
            this.rows.add(xr);
        }
    }
    public XWorkBook book(){
        return book;
    }
    public XSheet book(XWorkBook book){
        this.book = book;
        return this;
    }
    public Document doc(){
        return doc;
    }
    public XSheet doc(Document doc){
        this.doc = doc;
        return this;
    }
    public String name(){
        return name;
    }
    public XSheet doc(String name){
        this.name = name;
        return this;
    }
    public List rows(){
        return rows;
    }
    /**
     * 解析标签
     * 注意有跨行的情况
     */
    public void parseTag(){
        //行解析跨行
        //再解析行内
        for(XRow row:rows){
            row.parseTag();
        }
    }

    /**
     * 插入行 注意插入行后 index 之后所有行与单元格需要重新计算r属性 如果插入量大 应该在插入完成后一次生调整
     * @param index 插入位置 下标从0开始 如果index<0 index=rows.size+index -1:表示最后一行
     * @param values 行内数据
     * @param template 模板行 如果null则以最后index上一行作模板(如果index是0则以index行作模板)
     * @return XRow
     */
    public XRow insert(int index, XRow template, List values){
        int size = values.size();
        if(size == 0){
            return null;
        }
        XRow row = XRow.build(index, book, this, template, values);
        return row;
    }

    /**
     * 插入行 以上一行为模板
     * @param index 插入位置 下标从0开始 如果index<0 index=rows.size+index -1:表示最后一行
     * @param values 行内数据
     * @return XRow
     */
    public XRow insert(int index, List values){
        return insert(index, null, values);
    }
    /**
     * 追加行
     * @param template 模板行 如果null则以最后一行作模板
     * @param values 行内数据
     * @return XRow
     */
    public XRow append(XRow template, List values){
        return insert(-1, template, values);
    }

    /**
     * 追加行,以最后一行作模板
     * @param values 行内数据
     * @return XRow
     */
    public XRow append(List values){
        XRow template = rows.get(rows.size()-1);
        return append(template, values);
    }
    public void replace(boolean parse, LinkedHashMap replaces){
        for(XRow row:rows){
            row.replace(parse, replaces);
        }
    }


}